api.py 2.27 KB
Newer Older
Jamesie Pic's avatar
Jamesie Pic committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from rest_framework import (
    filters,
    viewsets,
)

from representatives.serializers import (
    ConstituencySerializer,
    GroupSerializer,
    MandateSerializer,
    RepresentativeDetailSerializer,
    RepresentativeSerializer,
)

from .models import (
    Constituency,
    Group,
    Mandate,
    Representative,
)


class RepresentativeViewSet(viewsets.ReadOnlyModelViewSet):
    """
    API endpoint that allows representatives to be viewed.
    """
    queryset = Representative.objects.all()
Jamesie Pic's avatar
PEP8    
Jamesie Pic committed
27
28
29
30
31
    filter_backends = (
        filters.DjangoFilterBackend,
        filters.SearchFilter,
        filters.OrderingFilter
    )
Jamesie Pic's avatar
Jamesie Pic committed
32
33
34
35
36
37
    filter_fields = {
        'active': ['exact'],
        'slug': ['exact', 'icontains'],
        'id': ['exact'],
        'remote_id': ['exact'],
        'first_name': ['exact', 'icontains'],
Jamesie Pic's avatar
PEP8    
Jamesie Pic committed
38
        'last_name': ['exact', 'icontains'],
Jamesie Pic's avatar
Jamesie Pic committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
        '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')

    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.
    """
    queryset = Mandate.objects.all()
    serializer_class = MandateSerializer

Jamesie Pic's avatar
PEP8    
Jamesie Pic committed
63
64
65
66
67
    filter_backends = (
        filters.DjangoFilterBackend,
        filters.SearchFilter,
        filters.OrderingFilter
    )
Jamesie Pic's avatar
Jamesie Pic committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
    filter_fields = {
        'id': ['exact'],
        'group__name': ['exact', 'icontains'],
        'group__abbreviation': ['exact'],
    }
    search_fields = ('group__name', 'group__abbreviation')
    # ordering_fields = ()


class ConstituencyViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = Constituency.objects.all()
    serializer_class = ConstituencySerializer


class GroupViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = Group.objects.all()
    serializer_class = GroupSerializer