Skip to content

Commit

Permalink
Use HyperlinkedModelSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
jpic committed May 4, 2016
1 parent 3c107a3 commit 364c634
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 99 deletions.
13 changes: 0 additions & 13 deletions representatives_votes/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,6 @@ class DossierViewSet(viewsets.ReadOnlyModelViewSet):

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):
"""
Expand Down Expand Up @@ -123,9 +116,3 @@ class VoteViewSet(viewsets.ReadOnlyModelViewSet):
'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)
92 changes: 6 additions & 86 deletions representatives_votes/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,10 @@
from rest_framework import serializers


class VoteSerializer(serializers.ModelSerializer):
class VoteSerializer(serializers.HyperlinkedModelSerializer):
"""
Vote serializer
"""
proposal = serializers.CharField(
source='proposal.fingerprint'
)
representative = serializers.CharField(
source='representative.fingerprint',
allow_null=True
)

class Meta:
model = models.Vote
fields = (
Expand All @@ -27,44 +19,13 @@ class Meta:
'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 ProposalSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = models.Proposal
fields = (
'id',
'fingerprint',
'dossier',
'dossier_title',
'dossier_reference',
'title',
'description',
'reference',
Expand All @@ -76,61 +37,25 @@ class Meta:
'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):
class DossierSerializer(serializers.HyperlinkedModelSerializer):
""" Base dossier serializer """
proposals = ProposalSerializer(many=True)

class Meta:
model = models.Dossier
fields = (
'id',
'fingerprint',
'title',
'reference',
'text',
'link',
'url',
'proposals',
)


Expand All @@ -139,8 +64,3 @@ class DossierDetailSerializer(DossierSerializer):
Dossier serializer that includes proposals and votes.
"""
proposals = ProposalDetailSerializer(many=True)

class Meta(DossierSerializer.Meta):
fields = DossierSerializer.Meta.fields + (
'proposals',
)

0 comments on commit 364c634

Please sign in to comment.