serializers.py 3.84 KB
Newer Older
Jamesie Pic's avatar
Jamesie Pic committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# coding: utf-8

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
    )
Jamesie Pic's avatar
PEP8    
Jamesie Pic committed
19

Jamesie Pic's avatar
Jamesie Pic committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    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
    )
Jamesie Pic's avatar
PEP8    
Jamesie Pic committed
59

Jamesie Pic's avatar
Jamesie Pic committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
    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):
Jamesie Pic's avatar
PEP8    
Jamesie Pic committed
80
81
        validated_data = super(ProposalSerializer, self).to_internal_value(
            data)
Jamesie Pic's avatar
Jamesie Pic committed
82
83
84
85
86
        validated_data['dossier'] = models.Dossier.objects.get(
            fingerprint=validated_data['dossier']['fingerprint']
        )
        validated_data['votes'] = data['votes']
        return validated_data
Jamesie Pic's avatar
PEP8    
Jamesie Pic committed
87

Jamesie Pic's avatar
Jamesie Pic committed
88
89
90
91
92
93
94
    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)
Jamesie Pic's avatar
PEP8    
Jamesie Pic committed
95

Jamesie Pic's avatar
Jamesie Pic committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
    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)
Jamesie Pic's avatar
PEP8    
Jamesie Pic committed
115

Jamesie Pic's avatar
Jamesie Pic committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
    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):
    """
Jamesie Pic's avatar
PEP8    
Jamesie Pic committed
139
140
141
    Dossier serializer that includes proposals and votes.
    """
    proposals = ProposalDetailSerializer(many=True)
Jamesie Pic's avatar
Jamesie Pic committed
142
143
144
145
146

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