api.py 2.49 KB
Newer Older
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
88
89
90
91
92
93
94
from rest_framework import (
    filters,
    viewsets,
)

from rql_filter.backend import RQLFilterBackend

from representatives.api import DefaultWebPagination

from .models import (
    DossierScore,
    RepresentativeScore,
    VoteScore
)

from .serializers import (
    DossierScoreSerializer,
    RepresentativeScoreSerializer,
    VoteScoreSerializer
)


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,
        RQLFilterBackend
    )
    filter_fields = {
        'dossier': ['exact'],
        'representative': ['exact'],
        'score': ['exact', 'gte', 'lte']
    }
    search_fields = ('dossier', 'representative')
    ordering_fields = ('representative', 'dossier')
    pagination_class = DefaultWebPagination
    serializer_class = DossierScoreSerializer


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


class VoteScoreViewSet(viewsets.ReadOnlyModelViewSet):
    """
    API endpoint to view votes with their score impact.
    This endpoint only shows votes that have a matching recommendation.
    """
    queryset = VoteScore.objects.select_related(
        'vote__representative',
        'vote__proposal',
        'vote__proposal__dossier',
        'vote__proposal__recommendation'
    ).filter(
        vote__proposal__recommendation__isnull=False
    )

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

    filter_fields = {
        'vote__representative': ['exact'],
        'vote__proposal': ['exact'],
        'vote__proposal__dossier': ['exact']
    }

    pagination_class = DefaultWebPagination
    serializer_class = VoteScoreSerializer