serializers.py 4.63 KB
Newer Older
luxcem's avatar
luxcem committed
1 2
# coding: utf-8

3
from rest_framework import serializers
luxcem's avatar
test  
luxcem committed
4

luxcem's avatar
luxcem committed
5 6 7
import representatives.models as models


Nicolas Joyard's avatar
Nicolas Joyard committed
8
class CountrySerializer(serializers.HyperlinkedModelSerializer):
9

luxcem's avatar
luxcem committed
10 11
    class Meta:
        model = models.Country
Nicolas Joyard's avatar
Nicolas Joyard committed
12 13 14 15
        fields = ('id', 'url', 'name', 'code')
        extra_kwargs = {
            'url': {'view_name': 'api-country-detail'}
        }
luxcem's avatar
luxcem committed
16

luxcem's avatar
luxcem committed
17

Nicolas Joyard's avatar
Nicolas Joyard committed
18
class ChamberSerializer(serializers.HyperlinkedModelSerializer):
Nicolas Joyard's avatar
Nicolas Joyard committed
19 20 21

    class Meta:
        model = models.Chamber
Nicolas Joyard's avatar
Nicolas Joyard committed
22 23 24 25 26
        fields = ('id', 'url', 'name', 'abbreviation', 'country')
        extra_kwargs = {
            'url': {'view_name': 'api-chamber-detail'},
            'country': {'view_name': 'api-country-detail'}
        }
Nicolas Joyard's avatar
Nicolas Joyard committed
27 28


Nicolas Joyard's avatar
Nicolas Joyard committed
29
class EmailSerializer(serializers.HyperlinkedModelSerializer):
30

luxcem's avatar
luxcem committed
31 32 33 34
    class Meta:
        model = models.Email
        fields = ('email', 'kind')

luxcem's avatar
luxcem committed
35

luxcem's avatar
luxcem committed
36
class WebsiteSerializer(serializers.ModelSerializer):
37

luxcem's avatar
luxcem committed
38 39 40 41
    class Meta:
        model = models.WebSite
        fields = ('url', 'kind')

luxcem's avatar
luxcem committed
42
    def validate_url(self, value):
43 44
        # Don’t validate url, because it could break import of not proper
        # formed url
luxcem's avatar
luxcem committed
45
        return value
luxcem's avatar
luxcem committed
46

luxcem's avatar
luxcem committed
47

luxcem's avatar
luxcem committed
48
class PhoneSerializer(serializers.ModelSerializer):
49

luxcem's avatar
luxcem committed
50 51
    class Meta:
        model = models.Phone
luxcem's avatar
luxcem committed
52
        fields = ('number', 'kind')
luxcem's avatar
luxcem committed
53

luxcem's avatar
luxcem committed
54 55 56
    def validate_phone(self, value):
        return value

luxcem's avatar
luxcem committed
57

luxcem's avatar
luxcem committed
58 59
class AddressSerializer(serializers.ModelSerializer):
    country = CountrySerializer()
60

luxcem's avatar
luxcem committed
61 62
    class Meta:
        model = models.Address
luxcem's avatar
luxcem committed
63 64
        fields = ('country', 'city', 'street',
                  'number', 'postcode', 'floor',
Jamesie Pic's avatar
Jamesie Pic committed
65
                  'office_number', 'kind',
66
                  )
luxcem's avatar
luxcem committed
67

luxcem's avatar
luxcem committed
68 69 70 71 72 73

class ContactField(serializers.Serializer):
    emails = EmailSerializer(many=True)
    phones = PhoneSerializer(many=True)
    websites = WebsiteSerializer(many=True)
    address = AddressSerializer(many=True)
luxcem's avatar
luxcem committed
74

luxcem's avatar
luxcem committed
75 76 77 78 79 80 81 82 83
    def get_attribute(self, obj):
        return {
            'emails': obj.email_set.all(),
            'websites': obj.website_set.all(),
            'phones': obj.phone_set.all(),
            'address': obj.address_set.all(),
        }


Nicolas Joyard's avatar
Nicolas Joyard committed
84
class ConstituencySerializer(serializers.HyperlinkedModelSerializer):
85

Arnaud Fabre's avatar
Arnaud Fabre committed
86 87
    class Meta:
        model = models.Constituency
Nicolas Joyard's avatar
Nicolas Joyard committed
88 89 90 91
        fields = ('id', 'url', 'name')
        extra_kwargs = {
            'url': {'view_name': 'api-constituency-detail'}
        }
Arnaud Fabre's avatar
Arnaud Fabre committed
92 93 94


class GroupSerializer(serializers.ModelSerializer):
95

Arnaud Fabre's avatar
Arnaud Fabre committed
96 97
    class Meta:
        model = models.Group
Nicolas Joyard's avatar
Nicolas Joyard committed
98 99 100 101
        fields = ('id', 'url', 'name', 'abbreviation', 'kind')
        extra_kwargs = {
            'url': {'view_name': 'api-group-detail'}
        }
102

Arnaud Fabre's avatar
Arnaud Fabre committed
103

Nicolas Joyard's avatar
Nicolas Joyard committed
104
class MandateSerializer(serializers.HyperlinkedModelSerializer):
luxcem's avatar
luxcem committed
105

luxcem's avatar
luxcem committed
106 107 108 109
    class Meta:
        model = models.Mandate
        fields = (
            'id',
Nicolas Joyard's avatar
Nicolas Joyard committed
110
            'url',
luxcem's avatar
luxcem committed
111
            'representative',
Arnaud Fabre's avatar
Arnaud Fabre committed
112 113
            'group',
            'constituency',
luxcem's avatar
luxcem committed
114 115 116 117 118
            'role',
            'begin_date',
            'end_date',
        )

Nicolas Joyard's avatar
Nicolas Joyard committed
119 120 121 122 123 124 125
        extra_kwargs = {
            'url': {'view_name': 'api-mandate-detail'},
            'group': {'view_name': 'api-group-detail'},
            'constituency': {'view_name': 'api-constituency-detail'},
            'representative': {'view_name': 'api-representative-detail'}
        }

Arnaud Fabre's avatar
Arnaud Fabre committed
126 127 128
    def to_internal_value(self, data):
        data = super(MandateSerializer, self).to_internal_value(data)
        data['group'] = models.Group.objects.get(
Nicolas Joyard's avatar
Nicolas Joyard committed
129
            id=data['group']['id']
Arnaud Fabre's avatar
Arnaud Fabre committed
130 131
        )
        data['constituency'] = models.Constituency.objects.get(
Nicolas Joyard's avatar
Nicolas Joyard committed
132
            id=data['constituency']['id']
Arnaud Fabre's avatar
Arnaud Fabre committed
133 134
        )
        data['representative'] = models.Representative.objects.get(
Nicolas Joyard's avatar
Nicolas Joyard committed
135
            id=data['representative']['id']
Arnaud Fabre's avatar
Arnaud Fabre committed
136 137
        )
        return data
luxcem's avatar
luxcem committed
138 139


Arnaud Fabre's avatar
Arnaud Fabre committed
140 141 142
class MandateDetailSerializer(MandateSerializer):
    group = GroupSerializer()
    constituency = ConstituencySerializer()
luxcem's avatar
luxcem committed
143

luxcem's avatar
luxcem committed
144
    class Meta(MandateSerializer.Meta):
Arnaud Fabre's avatar
Arnaud Fabre committed
145 146
        fields = (
            'id',
Nicolas Joyard's avatar
Nicolas Joyard committed
147
            'url',
Arnaud Fabre's avatar
Arnaud Fabre committed
148 149 150 151 152 153
            'group',
            'constituency',
            'role',
            'begin_date',
            'end_date',
        )
luxcem's avatar
luxcem committed
154 155


Nicolas Joyard's avatar
Nicolas Joyard committed
156
class RepresentativeSerializer(serializers.HyperlinkedModelSerializer):
Arnaud Fabre's avatar
Arnaud Fabre committed
157
    contacts = ContactField()
158

159 160
    mandates = MandateDetailSerializer(many=True)

luxcem's avatar
luxcem committed
161 162 163 164
    class Meta:
        model = models.Representative
        fields = (
            'id',
Nicolas Joyard's avatar
Nicolas Joyard committed
165
            'url',
luxcem's avatar
luxcem committed
166 167 168 169 170 171 172 173 174 175 176
            'slug',
            'first_name',
            'last_name',
            'full_name',
            'gender',
            'birth_place',
            'birth_date',
            'photo',
            'active',
            'cv',
            'contacts',
177
            'mandates'
luxcem's avatar
luxcem committed
178
        )
Nicolas Joyard's avatar
Nicolas Joyard committed
179 180 181
        extra_kwargs = {
            'url': {'view_name': 'api-representative-detail'},
        }
luxcem's avatar
luxcem committed
182

luxcem's avatar
luxcem committed
183

Arnaud Fabre's avatar
Arnaud Fabre committed
184
class RepresentativeDetailSerializer(RepresentativeSerializer):
185
    pass