api.py 2.71 KB
Newer Older
Jamesie Pic's avatar
Jamesie Pic committed
1 2 3 4 5 6 7 8 9 10 11
from .models import (
    Dossier,
    Proposal,
    Vote
)

from rest_framework import (
    filters,
    viewsets,
)

12 13
from representatives.api import DefaultWebPagination

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


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

28
    pagination_class = DefaultWebPagination
Jamesie Pic's avatar
Jamesie Pic committed
29 30 31
    queryset = Dossier.objects.all()
    serializer_class = DossierSerializer

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

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

Jamesie Pic's avatar
PEP8  
Jamesie Pic committed
43 44 45 46 47 48
    search_fields = (
        'title',
        'reference',
        'text',
        'proposals__title'
    )
Jamesie Pic's avatar
Jamesie Pic committed
49

Jamesie Pic's avatar
Jamesie Pic committed
50
    ordering_fields = ['reference']
Jamesie Pic's avatar
Jamesie Pic committed
51 52 53

    def retrieve(self, request, pk=None):
        self.serializer_class = DossierDetailSerializer
Nicolas Joyard's avatar
Nicolas Joyard committed
54 55
        self.queryset = self.queryset.prefetch_related('proposals',
                                                       'documents')
Jamesie Pic's avatar
Jamesie Pic committed
56 57 58 59 60 61 62 63
        return super(DossierViewSet, self).retrieve(request, pk)


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

64
    pagination_class = DefaultWebPagination
Jamesie Pic's avatar
Jamesie Pic committed
65
    queryset = Proposal.objects.all()
Jamesie Pic's avatar
Jamesie Pic committed
66 67
    serializer_class = ProposalSerializer

Jamesie Pic's avatar
PEP8  
Jamesie Pic committed
68 69 70 71 72
    filter_backends = (
        filters.DjangoFilterBackend,
        filters.SearchFilter,
        filters.OrderingFilter
    )
Jamesie Pic's avatar
Jamesie Pic committed
73 74 75 76 77 78 79 80 81

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

Jamesie Pic's avatar
PEP8  
Jamesie Pic committed
82 83
    search_fields = (
        'title',
Nicolas Joyard's avatar
Nicolas Joyard committed
84
        'reference',
Jamesie Pic's avatar
PEP8  
Jamesie Pic committed
85 86 87 88
        'dossier__title',
        'dossier__reference'
    )

Jamesie Pic's avatar
Jamesie Pic committed
89
    ordering_fields = ['reference']
Jamesie Pic's avatar
Jamesie Pic committed
90 91 92 93 94 95 96 97 98 99 100

    def retrieve(self, request, pk=None):
        self.serializer_class = ProposalDetailSerializer
        return super(ProposalViewSet, self).retrieve(request, pk)


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

101
    pagination_class = DefaultWebPagination
Jamesie Pic's avatar
Jamesie Pic committed
102
    queryset = Vote.objects.select_related('representative', 'proposal')
Jamesie Pic's avatar
Jamesie Pic committed
103 104
    serializer_class = VoteSerializer

Jamesie Pic's avatar
PEP8  
Jamesie Pic committed
105 106 107 108 109
    filter_backends = (
        filters.DjangoFilterBackend,
        filters.SearchFilter,
        filters.OrderingFilter
    )
Jamesie Pic's avatar
Jamesie Pic committed
110 111 112 113 114 115

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