views.py 1.63 KB
Newer Older
1
from rest_framework import viewsets
Aymeric Barantal's avatar
Aymeric Barantal committed
2
from rest_framework.response import Response
3
from picampaign.campaign.models import Campaign, CampaignContact, Argumentary
Aymeric Barantal's avatar
Aymeric Barantal committed
4
from picampaign.campaign.serializers import (CampaignSerializer,
5
6
                                             CampaignContactSerializer,
                                             ArgumentarySerializer)
Aymeric Barantal's avatar
Aymeric Barantal committed
7

Aymeric Barantal's avatar
Aymeric Barantal committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

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)
28
29
30
31
32
33
34
35
36
37
38
39
40


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)
41
42
43
44
45

    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)