Commit 578c69eb authored by Jamesie Pic's avatar Jamesie Pic

Added API

Code by @luxcem, left almost untouched.
parent e798ad5e
from .models import (
Dossier,
Proposal,
Vote
)
from rest_framework import (
filters,
viewsets,
)
from representatives_votes.serializers import (
DossierDetailSerializer,
DossierSerializer,
ProposalDetailSerializer,
ProposalSerializer,
VoteSerializer,
)
class DossierViewSet(viewsets.ReadOnlyModelViewSet):
"""
API endpoint that allows dossiers to be viewed.
"""
queryset = Dossier.objects.all()
serializer_class = DossierSerializer
filter_backends = (filters.DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
filter_fields = {
'fingerprint': ['exact'],
'title': ['exact', 'icontains'],
'reference': ['exact', 'icontains'],
}
search_fields = ('title', 'fingerprint', 'reference', 'text', 'proposals__title')
ordering_fields = ('id', 'reference')
def list(self, request):
return super(DossierViewSet, self).list(request)
def retrieve(self, request, pk=None):
self.serializer_class = DossierDetailSerializer
return super(DossierViewSet, self).retrieve(request, pk)
class ProposalViewSet(viewsets.ReadOnlyModelViewSet):
"""
API endpoint that allows proposals to be viewed.
"""
queryset = Proposal.objects.all()
serializer_class = ProposalSerializer
filter_backends = (filters.DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
filter_fields = {
'fingerprint': ['exact'],
'dossier__fingerprint': ['exact'],
'title': ['exact', 'icontains'],
'description': ['icontains'],
'reference': ['exact', 'icontains'],
'datetime': ['exact', 'gte', 'lte'],
'kind': ['exact'],
}
search_fields = ('title', 'fingerprint', 'reference',
'dossier__fingerprint', 'dossier__title',
'dossier__reference')
ordering_fields = ('id', 'reference')
def list(self, request):
return super(ProposalViewSet, self).list(request)
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.
"""
queryset = Vote.objects.all()
serializer_class = VoteSerializer
filter_backends = (filters.DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
filter_fields = {
'proposal__fingerprint': ['exact'],
'position': ['exact'],
'representative_name': ['exact', 'icontains'],
'representative': ['exact']
}
def list(self, request):
return super(VoteViewSet, self).list(request)
def retrieve(self, request, pk=None):
return super(VoteViewSet, self).retrieve(request, pk)
# 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
class VoteSerializer(serializers.ModelSerializer):
"""
Vote serializer
"""
proposal = serializers.CharField(
source='proposal.fingerprint'
)
representative = serializers.CharField(
source='representative.fingerprint',
allow_null=True
)
class Meta:
model = models.Vote
fields = (
'id',
'proposal',
'representative',
'representative_name',
'position'
)
def to_internal_value(self, data):
data = super(VoteSerializer, self).to_internal_value(data)
data['proposal'] = models.Proposal.objects.get(
fingerprint=data['proposal']['fingerprint']
)
if data['representative']['fingerprint']:
data['representative'] = Representative.objects.get(
fingerprint=data['representative']['fingerprint']
)
else:
data['representative'] = None
return data
class ProposalSerializer(serializers.ModelSerializer):
dossier = serializers.CharField(
source='dossier.fingerprint'
)
dossier_title = serializers.CharField(
source='dossier.title',
read_only=True
)
dossier_reference = serializers.CharField(
source='dossier.reference',
read_only=True
)
class Meta:
model = models.Proposal
fields = (
'id',
'fingerprint',
'dossier',
'dossier_title',
'dossier_reference',
'title',
'description',
'reference',
'datetime',
'kind',
'total_abstain',
'total_against',
'total_for',
'url',
)
def to_internal_value(self, 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)
if serializer.is_valid():
serializer.save()
else:
raise Exception(serializer.errors)
def create(self, validated_data):
votes_data = validated_data.pop('votes')
proposal = models.Proposal.objects.create(
**validated_data
)
self._create_votes(votes_data, proposal)
return proposal
def update(self, instance, validated_data):
validated_data.pop('votes')
for attr, value in validated_data.iteritems():
setattr(instance, attr, value)
instance.save()
return instance
class ProposalDetailSerializer(ProposalSerializer):
""" Proposal serializer that includes votes """
votes = VoteSerializer(many=True)
class Meta(ProposalSerializer.Meta):
fields = ProposalSerializer.Meta.fields + (
'votes',
)
class DossierSerializer(serializers.ModelSerializer):
""" Base dossier serializer """
class Meta:
model = models.Dossier
fields = (
'id',
'fingerprint',
'title',
'reference',
'text',
'link',
'url',
)
class DossierDetailSerializer(DossierSerializer):
"""
Dossier serializer that includes proposals
and votes
"""
proposals = ProposalDetailSerializer(
many = True,
)
class Meta(DossierSerializer.Meta):
fields = DossierSerializer.Meta.fields + (
'proposals',
)
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