Commit c959aff1 authored by okhin's avatar okhin

Merge branch 'api' into 'master'

Api

First version of REST API with minimal requirements

Valid routes are:
GET /campaings (should not be used)
GET /campaigns/<id>
GET /campaigns/<id>/contacts
GET /campaigns/<id>/arguments
GET /campaigns/<id>/arguments/<lang>
GET /campaigns/<id>/categories

POST /campaigns/<id>/feedbacks/

Some clean on data model and specially missing organization foreign keys

See merge request !3
parents 38467ccc 639465b4
from django.contrib import admin
from picampaign.campaign.models import (Campaign, Contact, CampaignContact,
from picampaign.campaign.models import (Campaign, CampaignContact,
Argumentary)
admin.site.register(Campaign)
admin.site.register(Contact)
class InlineContact(admin.TabularInline):
model = CampaignContact
class CampaignAdmin(admin.ModelAdmin):
inlines = [InlineContact]
admin.site.register(Campaign, CampaignAdmin)
admin.site.register(CampaignContact)
admin.site.register(Argumentary)
......@@ -2,21 +2,11 @@ from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.conf import settings
from picampaign.organization.models import Organization
from picampaign.contact.models import Contact
LANGUAGES = map(lambda (k, v): (k, _(v)), settings.LANGUAGES)
class Contact(models.Model):
"""Contact model. Person to be called by users"""
id = models.AutoField(primary_key=True)
first_name = models.CharField(max_length=64)
last_name = models.CharField(max_length=64)
phone = models.CharField(max_length=32)
twitter = models.CharField(max_length=64, blank=True)
mail = models.CharField(max_length=255, blank=True)
def __unicode__(self):
return '%s %s' % (self.first_name, self.last_name)
LANGUAGES = map(lambda (k, v): (k, _(v)), settings.LANGUAGES)
class Campaign(models.Model):
......@@ -24,6 +14,7 @@ class Campaign(models.Model):
id = models.AutoField(primary_key=True)
title = models.CharField(max_length=255)
description = models.CharField(max_length=512, blank=True)
organization = models.ForeignKey(Organization)
start_date = models.DateField()
end_date = models.DateField()
default_lang = models.CharField(max_length=5,
......@@ -56,3 +47,6 @@ class CampaignContact(models.Model):
def __unicode__(self):
return 'Contact %s on %s' % (self.contact, self.campaign.title)
def all_groups(self):
return [x.name for x in self.contact.groups.all()]
from picampaign.campaign.models import (Campaign, CampaignContact,
Argumentary)
from rest_framework import serializers
class ArgumentarySerializer(serializers.ModelSerializer):
class Meta:
model = Argumentary
fields = ('lang', 'text')
class CampaignSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Campaign
fields = ('id', 'title', 'description', 'start_date', 'end_date')
class CampaignContactSerializer(serializers.HyperlinkedModelSerializer):
first_name = serializers.Field(source='contact.first_name')
last_name = serializers.Field(source='contact.last_name')
phone = serializers.Field(source='contact.phone')
groups = serializers.Field(source='all_groups')
contact_id = serializers.Field(source='contact.id')
class Meta:
model = CampaignContact
fields = ('id', 'weight', 'contact_id',
'first_name', 'last_name', 'phone', 'groups')
from django.shortcuts import render
from rest_framework import viewsets
from rest_framework.response import Response
from picampaign.campaign.models import Campaign, CampaignContact, Argumentary
from picampaign.campaign.serializers import (CampaignSerializer,
CampaignContactSerializer,
ArgumentarySerializer)
# Create your views here.
class CampaignViewSet(viewsets.ReadOnlyModelViewSet):
"""
API endpoint for campaign view
"""
queryset = Campaign.objects.all()
serializer_class = CampaignSerializer
class CampaignContactViewSet(viewsets.ReadOnlyModelViewSet):
"""
API endpoint to view contacts related to a campaign
"""
queryset = CampaignContact.objects.all()
serializer_class = CampaignContactSerializer
def list(self, request, campaign_pk=None):
contacts = self.queryset.filter(campaign=campaign_pk)
serializer = self.serializer_class(contacts.all(), many=True)
return Response(serializer.data)
class ArgumentaryViewSet(viewsets.ReadOnlyModelViewSet):
"""
API endpoint to view contacts related to a campaign
"""
queryset = Argumentary.objects.all()
serializer_class = ArgumentarySerializer
def list(self, request, campaign_pk=None):
argumentaries = self.queryset.filter(campaign=campaign_pk)
serializer = self.serializer_class(argumentaries.all(), many=True)
return Response(serializer.data)
def retrieve(self, request, pk=None, campaign_pk=None):
argument = self.queryset.get(lang=pk, campaign=campaign_pk)
serializer = self.serializer_class(argument)
return Response(serializer.data)
from django.contrib import admin
from picampaign.contact.models import Contact
admin.site.register(Contact)
from django.db import models
class Contact(models.Model):
"""Contact model. Person to be called by users"""
id = models.AutoField(primary_key=True)
first_name = models.CharField(max_length=64)
last_name = models.CharField(max_length=64)
phone = models.CharField(max_length=32)
twitter = models.CharField(max_length=64, blank=True)
mail = models.CharField(max_length=255, blank=True)
def __unicode__(self):
return '%s %s' % (self.first_name, self.last_name)
from django.test import TestCase
# Create your tests here.
from django.shortcuts import render
# Create your views here.
from picampaign.feedback.models import Feedback
from rest_framework import serializers
class FeedbackSerializer(serializers.ModelSerializer):
class Meta:
model = Feedback
fields = ('comment', 'callee')
from django.shortcuts import render
from rest_framework import viewsets
from rest_framework.response import Response
# Create your views here.
from picampaign.feedback.serializers import FeedbackSerializer
from picampaign.feedback.models import Feedback, FeedbackCategory
from picampaign.campaign.models import CampaignContact
class FeedbackViewSet(viewsets.ViewSet):
queryset = Feedback.objects.all()
serializer_class = FeedbackSerializer
def create(self, request, campaign_pk=None):
serializer = FeedbackSerializer(request.DATA)
callee = CampaignContact.objects.get(id=request.DATA['callee'])
category = FeedbackCategory.objects.get(id=request.DATA['category'])
serializer.data['callee'] = callee
serializer.data['category'] = category
feedback = Feedback.objects.create(**serializer.data)
return Response(feedback.id)
from django.db import models
from django.contrib.auth.models import User
from picampaign.campaign.models import Contact
from picampaign.contact.models import Contact
class Organization(models.Model):
......@@ -16,11 +16,12 @@ class Organization(models.Model):
class Group(models.Model):
"""Group model to qualify contacts"""
id = models.AutoField(primary_key=True)
organisation = models.ForeignKey(Organization)
organization = models.ForeignKey(Organization)
name = models.CharField(max_length=64)
type = models.CharField(max_length=255, blank=True)
media = models.CharField(max_length=255, blank=True)
contacts = models.ManyToManyField(Contact, null=True, blank=True)
contacts = models.ManyToManyField(Contact, null=True, blank=True,
related_name='groups')
def __unicode__(self):
return self.name
......@@ -29,6 +30,7 @@ class Group(models.Model):
class FeedbackCategory(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=64)
organization = models.ForeignKey(Organization)
def __unicode__(self):
return self.name
from picampaign.organization.models import FeedbackCategory
from rest_framework import serializers
class CategorySerializer(serializers.ModelSerializer):
class Meta:
model = FeedbackCategory
fields = ('id', 'name')
from django.shortcuts import render
from rest_framework import viewsets
from rest_framework.response import Response
# Create your views here.
from picampaign.organization.models import FeedbackCategory
from picampaign.organization.serializers import CategorySerializer
class CategoryViewSet(viewsets.ViewSet):
queryset = FeedbackCategory.objects.all()
serializer_class = CategorySerializer
def list(self, request, campaign_pk=None):
# XXX need an organization in request to filter upon
categories = self.queryset.all()
serializer = self.serializer_class(categories, many=True)
return Response(serializer.data)
......@@ -36,6 +36,8 @@ INSTALLED_APPS = (
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'picampaign.contact',
'picampaign.campaign',
'picampaign.organization',
'picampaign.feedback',
......@@ -84,3 +86,8 @@ USE_TZ = True
# https://docs.djangoproject.com/en/1.7/howto/static-files/
STATIC_URL = '/static/'
#REST_FRAMEWORK = {
# 'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAdminUser',),
# 'PAGINATE_BY': 10
#}
from django.conf.urls import patterns, include, url
from django.contrib import admin
from rest_framework_nested import routers
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'campaign.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
from picampaign.campaign.views import (CampaignViewSet, CampaignContactViewSet,
ArgumentaryViewSet)
from picampaign.feedback.views import FeedbackViewSet
from picampaign.organization.views import CategoryViewSet
router = routers.SimpleRouter()
router.register(r'campaigns', CampaignViewSet)
campaign_router = routers.NestedSimpleRouter(router, r'campaigns',
lookup='campaign')
campaign_router.register(r'contacts', CampaignContactViewSet)
campaign_router.register(r'arguments', ArgumentaryViewSet)
campaign_router.register(r'categories', CategoryViewSet)
campaign_router.register(r'feedbacks', FeedbackViewSet)
url(r'^admin/', include(admin.site.urls)),
)
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^', include(router.urls)),
url(r'^', include(campaign_router.urls)))
......@@ -7,6 +7,8 @@ README = open(os.path.join(here, 'README')).read()
requires = [
'django',
'djangorestframework',
'drf-nested-routers',
]
setup(name='picampaign',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment