Commit ae8a230a authored by James Pic's avatar James Pic

Merge pull request #12 from political-memory/api

API release
parents eebec1ea 842da8b4
......@@ -26,7 +26,11 @@ class DossierViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Dossier.objects.all()
serializer_class = DossierSerializer
filter_backends = (filters.DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
filter_backends = (
filters.DjangoFilterBackend,
filters.SearchFilter,
filters.OrderingFilter
)
filter_fields = {
'fingerprint': ['exact'],
......@@ -34,9 +38,15 @@ class DossierViewSet(viewsets.ReadOnlyModelViewSet):
'reference': ['exact', 'icontains'],
}
search_fields = ('title', 'fingerprint', 'reference', 'text', 'proposals__title')
ordering_fields = ('id', 'reference')
search_fields = (
'title',
'fingerprint',
'reference',
'text',
'proposals__title'
)
ordering_fields = ('id', 'reference')
def list(self, request):
return super(DossierViewSet, self).list(request)
......@@ -51,10 +61,14 @@ class ProposalViewSet(viewsets.ReadOnlyModelViewSet):
API endpoint that allows proposals to be viewed.
"""
queryset = Proposal.objects.all()
queryset = Proposal.objects.select_related('dossier')
serializer_class = ProposalSerializer
filter_backends = (filters.DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
filter_backends = (
filters.DjangoFilterBackend,
filters.SearchFilter,
filters.OrderingFilter
)
filter_fields = {
'fingerprint': ['exact'],
......@@ -66,9 +80,14 @@ class ProposalViewSet(viewsets.ReadOnlyModelViewSet):
'kind': ['exact'],
}
search_fields = ('title', 'fingerprint', 'reference',
'dossier__fingerprint', 'dossier__title',
'dossier__reference')
search_fields = (
'title',
'fingerprint', 'reference',
'dossier__fingerprint',
'dossier__title',
'dossier__reference'
)
ordering_fields = ('id', 'reference')
def list(self, request):
......@@ -84,10 +103,14 @@ class VoteViewSet(viewsets.ReadOnlyModelViewSet):
API endpoint that allows proposals to be viewed.
"""
queryset = Vote.objects.all()
queryset = Vote.objects.select_related('representative', 'proposal')
serializer_class = VoteSerializer
filter_backends = (filters.DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
filter_backends = (
filters.DjangoFilterBackend,
filters.SearchFilter,
filters.OrderingFilter
)
filter_fields = {
'proposal__fingerprint': ['exact'],
......
# coding: utf-8
# This file is part of toutatis.
#
# toutatis is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or any later version.
#
# toutatis is distributed in the hope that it will
# be useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU General Affero Public
# License along with django-representatives.
# If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2015 Arnaud Fabre <af@laquadrature.net>
import representatives_votes.models as models
from representatives.models import Representative
from rest_framework import serializers
......@@ -34,7 +16,7 @@ class VoteSerializer(serializers.ModelSerializer):
source='representative.fingerprint',
allow_null=True
)
class Meta:
model = models.Vote
fields = (
......@@ -74,7 +56,7 @@ class ProposalSerializer(serializers.ModelSerializer):
source='dossier.reference',
read_only=True
)
class Meta:
model = models.Proposal
fields = (
......@@ -95,13 +77,14 @@ class ProposalSerializer(serializers.ModelSerializer):
)
def to_internal_value(self, data):
validated_data = super(ProposalSerializer, self).to_internal_value(data)
validated_data = super(ProposalSerializer, self).to_internal_value(
data)
validated_data['dossier'] = models.Dossier.objects.get(
fingerprint=validated_data['dossier']['fingerprint']
)
validated_data['votes'] = data['votes']
return validated_data
def _create_votes(self, votes_data, proposal):
for vote in votes_data:
serializer = VoteSerializer(data=vote)
......@@ -109,7 +92,7 @@ class ProposalSerializer(serializers.ModelSerializer):
serializer.save()
else:
raise Exception(serializer.errors)
def create(self, validated_data):
votes_data = validated_data.pop('votes')
proposal = models.Proposal.objects.create(
......@@ -129,7 +112,7 @@ class ProposalSerializer(serializers.ModelSerializer):
class ProposalDetailSerializer(ProposalSerializer):
""" Proposal serializer that includes votes """
votes = VoteSerializer(many=True)
class Meta(ProposalSerializer.Meta):
fields = ProposalSerializer.Meta.fields + (
'votes',
......@@ -152,13 +135,10 @@ class DossierSerializer(serializers.ModelSerializer):
class DossierDetailSerializer(DossierSerializer):
"""
Dossier serializer that includes proposals
and votes
"""
proposals = ProposalDetailSerializer(
many = True,
)
Dossier serializer that includes proposals and votes.
"""
proposals = ProposalDetailSerializer(many=True)
class Meta(DossierSerializer.Meta):
fields = DossierSerializer.Meta.fields + (
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment