api.py 3.26 KB
Newer Older
1 2 3 4 5
from rest_framework import (
    filters,
    viewsets,
)

6 7
from rql_filter.backend import RQLFilterBackend

8 9 10
from representatives.api import DefaultWebPagination

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

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


25 26 27 28 29 30 31 32
class DossierScoreViewSet(viewsets.ReadOnlyModelViewSet):
    """
    API endpoint to view representative score contribution for each dossier
    """
    queryset = DossierScore.objects.all()
    filter_backends = (
        filters.DjangoFilterBackend,
        filters.SearchFilter,
33 34
        filters.OrderingFilter,
        RQLFilterBackend
35 36 37 38 39 40 41 42 43 44 45 46 47
    )
    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


48 49 50 51 52 53 54 55
class RecommendationViewSet(viewsets.ReadOnlyModelViewSet):
    """
    API endpoint that allows recommendations to be viewed.
    """
    queryset = Recommendation.objects.select_related('proposal')
    filter_backends = (
        filters.DjangoFilterBackend,
        filters.SearchFilter,
56 57
        filters.OrderingFilter,
        RQLFilterBackend
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    )
    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,
80 81
        filters.OrderingFilter,
        RQLFilterBackend
82 83 84 85 86 87 88 89 90 91 92
    )
    filter_fields = {
        'representative': ['exact'],
        'score': ['exact', 'gte', 'lte']
    }
    search_fields = ('representative', 'score')
    ordering_fields = ('representative', 'score')
    pagination_class = DefaultWebPagination
    serializer_class = RepresentativeScoreSerializer


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

    filter_backends = (
        filters.DjangoFilterBackend,
        filters.SearchFilter,
110 111
        filters.OrderingFilter,
        RQLFilterBackend
112 113 114
    )

    filter_fields = {
Nicolas Joyard's avatar
Nicolas Joyard committed
115 116 117
        'representative': ['exact'],
        'proposal': ['exact'],
        'proposal__dossier': ['exact']
118 119 120
    }

    pagination_class = DefaultWebPagination
121
    serializer_class = VoteScoreSerializer