api.py 3.82 KB
Newer Older
Jamesie Pic's avatar
Jamesie Pic committed
1
2
from django.db import models

Jamesie Pic's avatar
Jamesie Pic committed
3
4
from rest_framework import (
    filters,
5
6
    pagination,
    renderers,
Jamesie Pic's avatar
Jamesie Pic committed
7
8
9
10
    viewsets,
)

from representatives.serializers import (
Nicolas Joyard's avatar
Nicolas Joyard committed
11
    ChamberSerializer,
Jamesie Pic's avatar
Jamesie Pic committed
12
    ConstituencySerializer,
Nicolas Joyard's avatar
Nicolas Joyard committed
13
    CountrySerializer,
Jamesie Pic's avatar
Jamesie Pic committed
14
15
16
17
18
19
20
    GroupSerializer,
    MandateSerializer,
    RepresentativeDetailSerializer,
    RepresentativeSerializer,
)

from .models import (
Jamesie Pic's avatar
Jamesie Pic committed
21
    Address,
Nicolas Joyard's avatar
Nicolas Joyard committed
22
    Chamber,
Jamesie Pic's avatar
Jamesie Pic committed
23
    Constituency,
Nicolas Joyard's avatar
Nicolas Joyard committed
24
    Country,
Jamesie Pic's avatar
Jamesie Pic committed
25
26
    Group,
    Mandate,
Jamesie Pic's avatar
Jamesie Pic committed
27
    Phone,
Jamesie Pic's avatar
Jamesie Pic committed
28
29
30
31
    Representative,
)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
class DefaultWebPagination(pagination.PageNumberPagination):
    default_web_page_size = 10

    def get_page_size(self, request):
        web = isinstance(request.accepted_renderer,
                         renderers.BrowsableAPIRenderer)
        size = pagination.PageNumberPagination.get_page_size(self, request)

        if web and not size:
            return self.default_web_page_size

        return size


Jamesie Pic's avatar
Jamesie Pic committed
46
47
48
49
50
class RepresentativeViewSet(viewsets.ReadOnlyModelViewSet):
    """
    API endpoint that allows representatives to be viewed.
    """
    queryset = Representative.objects.all()
Jamesie Pic's avatar
PEP8    
Jamesie Pic committed
51
52
53
54
55
    filter_backends = (
        filters.DjangoFilterBackend,
        filters.SearchFilter,
        filters.OrderingFilter
    )
Jamesie Pic's avatar
Jamesie Pic committed
56
57
58
59
60
    filter_fields = {
        'active': ['exact'],
        'slug': ['exact', 'icontains'],
        'id': ['exact'],
        'first_name': ['exact', 'icontains'],
Jamesie Pic's avatar
PEP8    
Jamesie Pic committed
61
        'last_name': ['exact', 'icontains'],
Jamesie Pic's avatar
Jamesie Pic committed
62
63
64
65
66
67
68
        'full_name': ['exact', 'icontains'],
        'gender': ['exact'],
        'birth_place': ['exact'],
        'birth_date': ['exact', 'gte', 'lte'],
    }
    search_fields = ('first_name', 'last_name', 'slug')
    ordering_fields = ('id', 'birth_date', 'last_name', 'full_name')
69
    pagination_class = DefaultWebPagination
Jamesie Pic's avatar
Jamesie Pic committed
70

Jamesie Pic's avatar
Jamesie Pic committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
    def get_queryset(self):
        qs = super(RepresentativeViewSet, self).get_queryset()
        qs = qs.prefetch_related(
            'email_set',
            'website_set',
            models.Prefetch(
                'address_set',
                queryset=Address.objects.select_related('country')
            ),
            models.Prefetch(
                'phone_set',
                queryset=Phone.objects.select_related('address__country')
            ),
            'mandates',
        )
        return qs

Jamesie Pic's avatar
Jamesie Pic committed
88
89
90
91
92
93
94
95
96
97
98
99
100
    def list(self, request):
        self.serializer_class = RepresentativeSerializer
        return super(RepresentativeViewSet, self).list(request)

    def retrieve(self, request, pk=None):
        self.serializer_class = RepresentativeDetailSerializer
        return super(RepresentativeViewSet, self).retrieve(request, pk)


class MandateViewSet(viewsets.ReadOnlyModelViewSet):
    """
    API endpoint that allows mandates to be viewed.
    """
101
102
    pagination_class = DefaultWebPagination
    queryset = Mandate.objects.select_related('representative')
Jamesie Pic's avatar
Jamesie Pic committed
103
104
    serializer_class = MandateSerializer

Jamesie Pic's avatar
PEP8    
Jamesie Pic committed
105
106
107
108
109
    filter_backends = (
        filters.DjangoFilterBackend,
        filters.SearchFilter,
        filters.OrderingFilter
    )
Jamesie Pic's avatar
Jamesie Pic committed
110
111
112
113
114
115
    filter_fields = {
        'id': ['exact'],
        'group__name': ['exact', 'icontains'],
        'group__abbreviation': ['exact'],
    }
    search_fields = ('group__name', 'group__abbreviation')
Jamesie Pic's avatar
Jamesie Pic committed
116

Jamesie Pic's avatar
Jamesie Pic committed
117
118

class ConstituencyViewSet(viewsets.ReadOnlyModelViewSet):
119
    pagination_class = DefaultWebPagination
Jamesie Pic's avatar
Jamesie Pic committed
120
121
122
123
124
    queryset = Constituency.objects.all()
    serializer_class = ConstituencySerializer


class GroupViewSet(viewsets.ReadOnlyModelViewSet):
125
    pagination_class = DefaultWebPagination
Jamesie Pic's avatar
Jamesie Pic committed
126
127
    queryset = Group.objects.all()
    serializer_class = GroupSerializer
Nicolas Joyard's avatar
Nicolas Joyard committed
128
129
130
131
132
133


class ChamberViewSet(viewsets.ReadOnlyModelViewSet):
    pagination_class = DefaultWebPagination
    queryset = Chamber.objects.all()
    serializer_class = ChamberSerializer
Nicolas Joyard's avatar
Nicolas Joyard committed
134
135
136
137
138
139


class CountryViewSet(viewsets.ReadOnlyModelViewSet):
    pagination_class = DefaultWebPagination
    queryset = Country.objects.all()
    serializer_class = CountrySerializer