Commit 48395d25 authored by Aymeric Barantal's avatar Aymeric Barantal

first version of REST api.

/campaigns/<id> and campaigns/<id>/contacts work
parent 38467ccc
from picampaign.campaign.models import Campaign, CampaignContact, Contact
from rest_framework import serializers
class ContactSerializer(serializers.ModelSerializer):
class Meta:
model = Contact
fields = ('id', 'first_name', 'last_name', 'phone')
class CampaignSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Campaign
fields = ('id', 'title', 'description', 'start_date', 'end_date')
class CampaignContactSerializer(serializers.HyperlinkedModelSerializer):
contact = ContactSerializer(many=False)
class Meta:
model = CampaignContact
fields = ('id', 'weight', 'contact')
from django.shortcuts import render
from rest_framework import viewsets, status
from rest_framework.response import Response
from picampaign.campaign.models import Campaign, CampaignContact
from picampaign.campaign.serializers import (CampaignSerializer,
CampaignContactSerializer)
# 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)
# if serializer.is_valid():
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
......@@ -36,6 +36,7 @@ INSTALLED_APPS = (
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'picampaign.campaign',
'picampaign.organization',
'picampaign.feedback',
......@@ -84,3 +85,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
router = routers.SimpleRouter()
router.register(r'campaigns', CampaignViewSet)
campaign_router = routers.NestedSimpleRouter(router, r'campaigns',
lookup='campaign')
campaign_router.register(r'contacts', CampaignContactViewSet)
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