Commit 1fbbd7f4 authored by Aymeric Barantal's avatar Aymeric Barantal

add campaigns/<id>/arguments API

parent 48395d25
from picampaign.campaign.models import Campaign, CampaignContact, Contact
from picampaign.campaign.models import (Campaign, CampaignContact, Contact,
Argumentary)
from rest_framework import serializers
......@@ -9,6 +10,13 @@ class ContactSerializer(serializers.ModelSerializer):
fields = ('id', 'first_name', 'last_name', 'phone')
class ArgumentarySerializer(serializers.ModelSerializer):
class Meta:
model = Argumentary
fields = ('lang', 'text')
class CampaignSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
......
from rest_framework import viewsets, status
from rest_framework import viewsets
from rest_framework.response import Response
from picampaign.campaign.models import Campaign, CampaignContact
from picampaign.campaign.models import Campaign, CampaignContact, Argumentary
from picampaign.campaign.serializers import (CampaignSerializer,
CampaignContactSerializer)
CampaignContactSerializer,
ArgumentarySerializer)
class CampaignViewSet(viewsets.ReadOnlyModelViewSet):
......@@ -23,6 +24,17 @@ class CampaignContactViewSet(viewsets.ReadOnlyModelViewSet):
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)
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)
......@@ -2,7 +2,8 @@ from django.conf.urls import patterns, include, url
from django.contrib import admin
from rest_framework_nested import routers
from picampaign.campaign.views import CampaignViewSet, CampaignContactViewSet
from picampaign.campaign.views import (CampaignViewSet, CampaignContactViewSet,
ArgumentaryViewSet)
router = routers.SimpleRouter()
router.register(r'campaigns', CampaignViewSet)
......@@ -10,6 +11,7 @@ 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)
urlpatterns = patterns('',
......
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