api.py 3.11 KB
Newer Older
1
2
3
4
5
6
7
8
from rest_framework import (
    filters,
    viewsets,
)

from representatives.api import DefaultWebPagination

from .models import (
9
    DossierScore,
10
11
    Recommendation,
    RepresentativeScore,
12
    VoteScore
13
14
15
)

from .serializers import (
16
    DossierScoreSerializer,
17
18
    RecommendationSerializer,
    RepresentativeScoreSerializer,
19
    VoteScoreSerializer
20
21
22
)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class DossierScoreViewSet(viewsets.ReadOnlyModelViewSet):
    """
    API endpoint to view representative score contribution for each dossier
    """
    queryset = DossierScore.objects.all()
    filter_backends = (
        filters.DjangoFilterBackend,
        filters.SearchFilter,
        filters.OrderingFilter
    )
    filter_fields = {
        'id': ['exact'],
        'dossier': ['exact'],
        'representative': ['exact'],
        'score': ['exact', 'gte', 'lte']
    }
    search_fields = ('dossier', 'representative')
    ordering_fields = ('representative', 'dossier')
    pagination_class = DefaultWebPagination
    serializer_class = DossierScoreSerializer


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
class RecommendationViewSet(viewsets.ReadOnlyModelViewSet):
    """
    API endpoint that allows recommendations to be viewed.
    """
    queryset = Recommendation.objects.select_related('proposal')
    filter_backends = (
        filters.DjangoFilterBackend,
        filters.SearchFilter,
        filters.OrderingFilter
    )
    filter_fields = {
        'id': ['exact'],
        'recommendation': ['exact'],
        'title': ['exact', 'icontains'],
        'description': ['exact', 'icontains'],
        'weight': ['exact', 'gte', 'lte']
    }
    search_fields = ('title', 'description')
    ordering_fields = ('id', 'weight', 'title')
    pagination_class = DefaultWebPagination
    serializer_class = RecommendationSerializer


class RepresentativeScoreViewSet(viewsets.ReadOnlyModelViewSet):
    """
    API endpoint to view representative scores
    """
    queryset = RepresentativeScore.objects.select_related('representative')
    filter_backends = (
        filters.DjangoFilterBackend,
        filters.SearchFilter,
        filters.OrderingFilter
    )
    filter_fields = {
        'representative': ['exact'],
        'score': ['exact', 'gte', 'lte']
    }
    search_fields = ('representative', 'score')
    ordering_fields = ('representative', 'score')
    pagination_class = DefaultWebPagination
    serializer_class = RepresentativeScoreSerializer


88
class VoteScoreViewSet(viewsets.ReadOnlyModelViewSet):
89
90
91
92
    """
    API endpoint to view votes with their score impact.
    This endpoint only shows votes that have a matching recommendation.
    """
93
    queryset = VoteScore.objects.select_related(
94
95
        'representative',
        'proposal',
Nicolas Joyard's avatar
Nicolas Joyard committed
96
        'proposal__dossier',
97
98
99
100
101
102
103
104
105
106
107
108
        'proposal__recommendation'
    ).filter(
        proposal__recommendation__isnull=False
    )

    filter_backends = (
        filters.DjangoFilterBackend,
        filters.SearchFilter,
        filters.OrderingFilter
    )

    filter_fields = {
Nicolas Joyard's avatar
Nicolas Joyard committed
109
110
111
        'representative': ['exact'],
        'proposal': ['exact'],
        'proposal__dossier': ['exact']
112
113
114
    }

    pagination_class = DefaultWebPagination
115
    serializer_class = VoteScoreSerializer