api.py 3.15 KB
Newer Older
1
2
from django.db import models

Jamesie Pic's avatar
Jamesie Pic committed
3
from .models import (
4
    Document,
Jamesie Pic's avatar
Jamesie Pic committed
5
6
7
8
9
10
11
12
13
14
    Dossier,
    Proposal,
    Vote
)

from rest_framework import (
    filters,
    viewsets,
)

15
16
from representatives.api import DefaultWebPagination

Jamesie Pic's avatar
Jamesie Pic committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from representatives_votes.serializers import (
    DossierDetailSerializer,
    DossierSerializer,
    ProposalDetailSerializer,
    ProposalSerializer,
    VoteSerializer,
)


class DossierViewSet(viewsets.ReadOnlyModelViewSet):
    """
    API endpoint that allows dossiers to be viewed.
    """

31
    pagination_class = DefaultWebPagination
32
    queryset = Dossier.objects.order_by('id')
Jamesie Pic's avatar
Jamesie Pic committed
33
34
    serializer_class = DossierSerializer

Jamesie Pic's avatar
PEP8    
Jamesie Pic committed
35
36
37
38
39
    filter_backends = (
        filters.DjangoFilterBackend,
        filters.SearchFilter,
        filters.OrderingFilter
    )
Jamesie Pic's avatar
Jamesie Pic committed
40
41
42
43
44
45

    filter_fields = {
        'title': ['exact', 'icontains'],
        'reference': ['exact', 'icontains'],
    }

Jamesie Pic's avatar
PEP8    
Jamesie Pic committed
46
47
48
49
50
51
    search_fields = (
        'title',
        'reference',
        'text',
        'proposals__title'
    )
Jamesie Pic's avatar
Jamesie Pic committed
52
53
54

    def retrieve(self, request, pk=None):
        self.serializer_class = DossierDetailSerializer
55
56
57
58
59
60
61
62
63
64
        self.queryset = self.queryset.prefetch_related(
            models.Prefetch(
                'proposals',
                queryset=Proposal.objects.order_by('id')
            ),
            models.Prefetch(
                'documents',
                queryset=Document.objects.order_by('id')
            )
        )
Jamesie Pic's avatar
Jamesie Pic committed
65
66
67
68
69
70
71
72
        return super(DossierViewSet, self).retrieve(request, pk)


class ProposalViewSet(viewsets.ReadOnlyModelViewSet):
    """
    API endpoint that allows proposals to be viewed.
    """

73
    pagination_class = DefaultWebPagination
74
    queryset = Proposal.objects.order_by('id')
Jamesie Pic's avatar
Jamesie Pic committed
75
76
    serializer_class = ProposalSerializer

Jamesie Pic's avatar
PEP8    
Jamesie Pic committed
77
78
79
80
81
    filter_backends = (
        filters.DjangoFilterBackend,
        filters.SearchFilter,
        filters.OrderingFilter
    )
Jamesie Pic's avatar
Jamesie Pic committed
82
83
84
85
86
87
88
89
90

    filter_fields = {
        'title': ['exact', 'icontains'],
        'description': ['icontains'],
        'reference': ['exact', 'icontains'],
        'datetime': ['exact', 'gte', 'lte'],
        'kind': ['exact'],
    }

Jamesie Pic's avatar
PEP8    
Jamesie Pic committed
91
92
    search_fields = (
        'title',
Nicolas Joyard's avatar
Nicolas Joyard committed
93
        'reference',
Jamesie Pic's avatar
PEP8    
Jamesie Pic committed
94
95
96
97
        'dossier__title',
        'dossier__reference'
    )

Jamesie Pic's avatar
Jamesie Pic committed
98
99
    def retrieve(self, request, pk=None):
        self.serializer_class = ProposalDetailSerializer
100
101
102
103
104
105
        self.queryset = self.queryset.prefetch_related(
            models.Prefetch(
                'votes',
                queryset=Vote.objects.order_by('representative_id')
            )
        )
Jamesie Pic's avatar
Jamesie Pic committed
106
107
108
109
110
111
112
113
        return super(ProposalViewSet, self).retrieve(request, pk)


class VoteViewSet(viewsets.ReadOnlyModelViewSet):
    """
    API endpoint that allows proposals to be viewed.
    """

114
    pagination_class = DefaultWebPagination
115
116
    queryset = Vote.objects.select_related('representative', 'proposal') \
                           .order_by('proposal_id', 'representative__slug')
Jamesie Pic's avatar
Jamesie Pic committed
117
118
    serializer_class = VoteSerializer

Jamesie Pic's avatar
PEP8    
Jamesie Pic committed
119
120
121
122
123
    filter_backends = (
        filters.DjangoFilterBackend,
        filters.SearchFilter,
        filters.OrderingFilter
    )
Jamesie Pic's avatar
Jamesie Pic committed
124
125
126
127
128
129

    filter_fields = {
        'position': ['exact'],
        'representative_name': ['exact', 'icontains'],
        'representative': ['exact']
    }