api.py 4.71 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
    viewsets,
)

10 11
from rql_filter.backend import RQLFilterBackend

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

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

35 36
from .filters import ActiveMandateQueryFilterBackend

Jamesie Pic's avatar
Jamesie Pic committed
37

38 39 40 41 42 43 44 45 46 47 48 49 50 51
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
52 53 54 55
class RepresentativeViewSet(viewsets.ReadOnlyModelViewSet):
    """
    API endpoint that allows representatives to be viewed.
    """
56
    queryset = Representative.objects.order_by('slug')
Jamesie Pic's avatar
PEP8  
Jamesie Pic committed
57 58 59
    filter_backends = (
        filters.DjangoFilterBackend,
        filters.SearchFilter,
60
        filters.OrderingFilter,
61 62
        RQLFilterBackend,
        ActiveMandateQueryFilterBackend
Jamesie Pic's avatar
PEP8  
Jamesie Pic committed
63
    )
Jamesie Pic's avatar
Jamesie Pic committed
64 65 66 67 68
    filter_fields = {
        'active': ['exact'],
        'slug': ['exact', 'icontains'],
        'id': ['exact'],
        'first_name': ['exact', 'icontains'],
Jamesie Pic's avatar
PEP8  
Jamesie Pic committed
69
        'last_name': ['exact', 'icontains'],
Jamesie Pic's avatar
Jamesie Pic committed
70 71 72 73 74 75
        'full_name': ['exact', 'icontains'],
        'gender': ['exact'],
        'birth_place': ['exact'],
        'birth_date': ['exact', 'gte', 'lte'],
    }
    search_fields = ('first_name', 'last_name', 'slug')
76
    pagination_class = DefaultWebPagination
Jamesie Pic's avatar
Jamesie Pic committed
77

Jamesie Pic's avatar
Jamesie Pic committed
78 79 80
    def get_queryset(self):
        qs = super(RepresentativeViewSet, self).get_queryset()
        qs = qs.prefetch_related(
81 82 83 84 85 86 87 88
            models.Prefetch(
                'email_set',
                queryset=Email.objects.order_by('id')
            ),
            models.Prefetch(
                'website_set',
                queryset=WebSite.objects.order_by('id')
            ),
Jamesie Pic's avatar
Jamesie Pic committed
89 90 91
            models.Prefetch(
                'address_set',
                queryset=Address.objects.select_related('country')
92
                                        .order_by('id')
Jamesie Pic's avatar
Jamesie Pic committed
93 94 95 96
            ),
            models.Prefetch(
                'phone_set',
                queryset=Phone.objects.select_related('address__country')
97
                                      .order_by('id')
Jamesie Pic's avatar
Jamesie Pic committed
98
            ),
99 100 101 102
            models.Prefetch(
                'mandates',
                queryset=Mandate.objects.order_by('id')
            )
Jamesie Pic's avatar
Jamesie Pic committed
103 104 105
        )
        return qs

Jamesie Pic's avatar
Jamesie Pic committed
106 107 108 109 110 111 112 113 114 115 116 117 118
    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.
    """
119
    pagination_class = DefaultWebPagination
120 121
    queryset = Mandate.objects.select_related('representative') \
                              .order_by('representative_id', 'id')
Jamesie Pic's avatar
Jamesie Pic committed
122 123
    serializer_class = MandateSerializer

Jamesie Pic's avatar
PEP8  
Jamesie Pic committed
124 125 126
    filter_backends = (
        filters.DjangoFilterBackend,
        filters.SearchFilter,
127 128
        filters.OrderingFilter,
        RQLFilterBackend
Jamesie Pic's avatar
PEP8  
Jamesie Pic committed
129
    )
Jamesie Pic's avatar
Jamesie Pic committed
130 131 132 133 134 135
    filter_fields = {
        'id': ['exact'],
        'group__name': ['exact', 'icontains'],
        'group__abbreviation': ['exact'],
    }
    search_fields = ('group__name', 'group__abbreviation')
Jamesie Pic's avatar
Jamesie Pic committed
136

Jamesie Pic's avatar
Jamesie Pic committed
137 138

class ConstituencyViewSet(viewsets.ReadOnlyModelViewSet):
139
    pagination_class = DefaultWebPagination
140
    queryset = Constituency.objects.order_by('id')
Jamesie Pic's avatar
Jamesie Pic committed
141 142
    serializer_class = ConstituencySerializer

143 144 145 146
    filter_backends = (
        RQLFilterBackend,
    )

Jamesie Pic's avatar
Jamesie Pic committed
147 148

class GroupViewSet(viewsets.ReadOnlyModelViewSet):
149
    pagination_class = DefaultWebPagination
150
    queryset = Group.objects.order_by('id')
Jamesie Pic's avatar
Jamesie Pic committed
151
    serializer_class = GroupSerializer
Nicolas Joyard's avatar
Nicolas Joyard committed
152

153 154 155 156
    filter_backends = (
        RQLFilterBackend,
    )

Nicolas Joyard's avatar
Nicolas Joyard committed
157 158 159

class ChamberViewSet(viewsets.ReadOnlyModelViewSet):
    pagination_class = DefaultWebPagination
160
    queryset = Chamber.objects.order_by('id')
Nicolas Joyard's avatar
Nicolas Joyard committed
161
    serializer_class = ChamberSerializer
Nicolas Joyard's avatar
Nicolas Joyard committed
162

163 164 165 166
    filter_backends = (
        RQLFilterBackend,
    )

Nicolas Joyard's avatar
Nicolas Joyard committed
167 168 169

class CountryViewSet(viewsets.ReadOnlyModelViewSet):
    pagination_class = DefaultWebPagination
170
    queryset = Country.objects.order_by('id')
Nicolas Joyard's avatar
Nicolas Joyard committed
171
    serializer_class = CountrySerializer
172 173 174 175

    filter_backends = (
        RQLFilterBackend,
    )