serializers.py 1.56 KB
Newer Older
Jamesie Pic's avatar
Jamesie Pic committed
1 2 3
# coding: utf-8

import representatives_votes.models as models
Jamesie Pic's avatar
Jamesie Pic committed
4

Jamesie Pic's avatar
Jamesie Pic committed
5 6 7
from rest_framework import serializers


Jamesie Pic's avatar
Jamesie Pic committed
8
class VoteSerializer(serializers.HyperlinkedModelSerializer):
Jamesie Pic's avatar
Jamesie Pic committed
9 10 11 12 13 14 15 16 17 18 19 20
    """
    Vote serializer
    """
    class Meta:
        model = models.Vote
        fields = (
            'proposal',
            'representative',
            'representative_name',
            'position'
        )

Jamesie Pic's avatar
PEP8  
Jamesie Pic committed
21

Jamesie Pic's avatar
Jamesie Pic committed
22
class ProposalSerializer(serializers.HyperlinkedModelSerializer):
Jamesie Pic's avatar
Jamesie Pic committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
    class Meta:
        model = models.Proposal
        fields = (
            'dossier',
            'title',
            'description',
            'reference',
            'datetime',
            'kind',
            'total_abstain',
            'total_against',
            'total_for',
            'url',
        )


class ProposalDetailSerializer(ProposalSerializer):
    """ Proposal serializer that includes votes """
Jamesie Pic's avatar
Jamesie Pic committed
41

Jamesie Pic's avatar
Jamesie Pic committed
42
    votes = VoteSerializer(many=True)
Jamesie Pic's avatar
PEP8  
Jamesie Pic committed
43

Jamesie Pic's avatar
Jamesie Pic committed
44 45 46
    class Meta:
        model = models.Proposal
        fields = ProposalSerializer.Meta.fields + ('votes',)
Jamesie Pic's avatar
Jamesie Pic committed
47 48


Jamesie Pic's avatar
Jamesie Pic committed
49
class DossierSerializer(serializers.HyperlinkedModelSerializer):
Jamesie Pic's avatar
Jamesie Pic committed
50
    """ Base dossier serializer """
Jamesie Pic's avatar
Jamesie Pic committed
51

Jamesie Pic's avatar
Jamesie Pic committed
52 53 54 55 56 57 58 59 60 61 62 63 64
    class Meta:
        model = models.Dossier
        fields = (
            'title',
            'reference',
            'text',
            'link',
            'url',
        )


class DossierDetailSerializer(DossierSerializer):
    """
Jamesie Pic's avatar
PEP8  
Jamesie Pic committed
65 66
    Dossier serializer that includes proposals and votes.
    """
Jamesie Pic's avatar
Jamesie Pic committed
67

Jamesie Pic's avatar
Jamesie Pic committed
68 69 70 71 72
    proposals = ProposalSerializer(many=True)

    class Meta:
        model = models.Dossier
        field = DossierSerializer.Meta.fields + ('proposals',)