serializers.py 23.3 KB
Newer Older
Gabriel Detraz's avatar
Gabriel Detraz committed
1
# -*- mode: python; coding: utf-8 -*-
Maël Kervella's avatar
Maël Kervella committed
2 3 4 5
# Re2o est un logiciel d'administration développé initiallement au rezometz. Il
# se veut agnostique au réseau considéré, de manière à être installable en
# quelques clics.
#
6
# Copyright © 2018 Maël Kervella
Maël Kervella's avatar
Maël Kervella committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

22
"""Defines the serializers of the API
Maël Kervella's avatar
Maël Kervella committed
23
"""
Maël Kervella's avatar
Maël Kervella committed
24 25

from rest_framework import serializers
26

Maël Kervella's avatar
Maël Kervella committed
27 28 29 30 31 32 33
import cotisations.models as cotisations
import machines.models as machines
import preferences.models as preferences
import topologie.models as topologie
import users.models as users


34 35
# The namespace used for the API. It must match the namespace used in the
# urlpatterns to include the API URLs.
Maël Kervella's avatar
Maël Kervella committed
36 37 38 39
API_NAMESPACE = 'api'


class NamespacedHRField(serializers.HyperlinkedRelatedField):
40 41 42
    """A `rest_framework.serializers.HyperlinkedRelatedField` subclass to
    automatically prefix view names with the API namespace.
    """
Maël Kervella's avatar
Maël Kervella committed
43 44 45 46 47 48 49
    def __init__(self, view_name=None, **kwargs):
        if view_name is not None:
            view_name = '%s:%s' % (API_NAMESPACE, view_name)
        super(NamespacedHRField, self).__init__(view_name=view_name, **kwargs)


class NamespacedHIField(serializers.HyperlinkedIdentityField):
50 51 52
    """A `rest_framework.serializers.HyperlinkedIdentityField` subclass to
    automatically prefix view names with teh API namespace.
    """
Maël Kervella's avatar
Maël Kervella committed
53 54 55 56 57 58 59
    def __init__(self, view_name=None, **kwargs):
        if view_name is not None:
            view_name = '%s:%s' % (API_NAMESPACE, view_name)
        super(NamespacedHIField, self).__init__(view_name=view_name, **kwargs)


class NamespacedHMSerializer(serializers.HyperlinkedModelSerializer):
60 61 62
    """A `rest_framework.serializers.HyperlinkedModelSerializer` subclass to
    automatically prefix view names with the API namespace.
    """
Maël Kervella's avatar
Maël Kervella committed
63 64
    serializer_related_field = NamespacedHRField
    serializer_url_field = NamespacedHIField
Maël Kervella's avatar
Maël Kervella committed
65

Maël Kervella's avatar
Maël Kervella committed
66

67
# COTISATIONS
68

Maël Kervella's avatar
Maël Kervella committed
69 70

class FactureSerializer(NamespacedHMSerializer):
71 72
    """Serialize `cotisations.models.Facture` objects.
    """
73
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
74
        model = cotisations.Facture
75 76 77 78
        fields = ('user', 'paiement', 'banque', 'cheque', 'date', 'valid',
                  'control', 'prix_total', 'name', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
79
class VenteSerializer(NamespacedHMSerializer):
80 81
    """Serialize `cotisations.models.Vente` objects.
    """
82
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
83
        model = cotisations.Vente
84 85 86 87
        fields = ('facture', 'number', 'name', 'prix', 'duration',
                  'type_cotisation', 'prix_total', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
88
class ArticleSerializer(NamespacedHMSerializer):
89 90
    """Serialize `cotisations.models.Article` objects.
    """
91
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
92
        model = cotisations.Article
93 94 95 96
        fields = ('name', 'prix', 'duration', 'type_user',
                  'type_cotisation', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
97
class BanqueSerializer(NamespacedHMSerializer):
98 99
    """Serialize `cotisations.models.Banque` objects.
    """
100
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
101
        model = cotisations.Banque
102 103 104
        fields = ('name', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
105
class PaiementSerializer(NamespacedHMSerializer):
106 107
    """Serialize `cotisations.models.Paiement` objects.
    """
108
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
109
        model = cotisations.Paiement
110 111 112
        fields = ('moyen', 'type_paiement', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
113
class CotisationSerializer(NamespacedHMSerializer):
114 115
    """Serialize `cotisations.models.Cotisation` objects.
    """
116
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
117
        model = cotisations.Cotisation
118 119 120 121
        fields = ('vente', 'type_cotisation', 'date_start', 'date_end',
                  'api_url')


122
# MACHINES
Maël Kervella's avatar
Maël Kervella committed
123 124


Maël Kervella's avatar
Maël Kervella committed
125
class MachineSerializer(NamespacedHMSerializer):
126 127
    """Serialize `machines.models.Machine` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
128
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
129
        model = machines.Machine
Maël Kervella's avatar
Maël Kervella committed
130 131 132
        fields = ('user', 'name', 'active', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
133
class MachineTypeSerializer(NamespacedHMSerializer):
134 135
    """Serialize `machines.models.MachineType` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
136
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
137
        model = machines.MachineType
Maël Kervella's avatar
Maël Kervella committed
138 139 140
        fields = ('type', 'ip_type', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
141
class IpTypeSerializer(NamespacedHMSerializer):
142 143
    """Serialize `machines.models.IpType` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
144
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
145
        model = machines.IpType
Maël Kervella's avatar
Maël Kervella committed
146 147 148 149 150
        fields = ('type', 'extension', 'need_infra', 'domaine_ip_start',
                  'domaine_ip_stop', 'prefix_v6', 'vlan', 'ouverture_ports',
                  'api_url')


Maël Kervella's avatar
Maël Kervella committed
151
class VlanSerializer(NamespacedHMSerializer):
152 153
    """Serialize `machines.models.Vlan` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
154
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
155
        model = machines.Vlan
Maël Kervella's avatar
Maël Kervella committed
156 157 158
        fields = ('vlan_id', 'name', 'comment', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
159
class NasSerializer(NamespacedHMSerializer):
160 161
    """Serialize `machines.models.Nas` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
162
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
163
        model = machines.Nas
Maël Kervella's avatar
Maël Kervella committed
164 165 166 167
        fields = ('name', 'nas_type', 'machine_type', 'port_access_mode',
                  'autocapture_mac', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
168
class SOASerializer(NamespacedHMSerializer):
169 170
    """Serialize `machines.models.SOA` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
171
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
172
        model = machines.SOA
Maël Kervella's avatar
Maël Kervella committed
173 174 175 176
        fields = ('name', 'mail', 'refresh', 'retry', 'expire', 'ttl',
                  'api_url')


Maël Kervella's avatar
Maël Kervella committed
177
class ExtensionSerializer(NamespacedHMSerializer):
178 179
    """Serialize `machines.models.Extension` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
180
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
181
        model = machines.Extension
Maël Kervella's avatar
Maël Kervella committed
182 183 184 185
        fields = ('name', 'need_infra', 'origin', 'origin_v6', 'soa',
                  'api_url')


Maël Kervella's avatar
Maël Kervella committed
186
class MxSerializer(NamespacedHMSerializer):
187 188
    """Serialize `machines.models.Mx` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
189
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
190
        model = machines.Mx
Maël Kervella's avatar
Maël Kervella committed
191 192 193
        fields = ('zone', 'priority', 'name', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
194
class NsSerializer(NamespacedHMSerializer):
195 196
    """Serialize `machines.models.Ns` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
197
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
198
        model = machines.Ns
Maël Kervella's avatar
Maël Kervella committed
199 200 201
        fields = ('zone', 'ns', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
202
class TxtSerializer(NamespacedHMSerializer):
203 204
    """Serialize `machines.models.Txt` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
205
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
206
        model = machines.Txt
Maël Kervella's avatar
Maël Kervella committed
207 208 209
        fields = ('zone', 'field1', 'field2', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
210
class SrvSerializer(NamespacedHMSerializer):
211 212
    """Serialize `machines.models.Srv` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
213
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
214
        model = machines.Srv
Maël Kervella's avatar
Maël Kervella committed
215 216 217 218
        fields = ('service', 'protocole', 'extension', 'ttl', 'priority',
                  'weight', 'port', 'target', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
219
class InterfaceSerializer(NamespacedHMSerializer):
220 221
    """Serialize `machines.models.Interface` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
222
    mac_address = serializers.CharField()
Maël Kervella's avatar
Maël Kervella committed
223 224 225
    active = serializers.BooleanField(source='is_active')

    class Meta:
Maël Kervella's avatar
Maël Kervella committed
226
        model = machines.Interface
Maël Kervella's avatar
Maël Kervella committed
227 228 229 230
        fields = ('ipv4', 'mac_address', 'machine', 'type', 'details',
                  'port_lists', 'active', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
231
class Ipv6ListSerializer(NamespacedHMSerializer):
232 233
    """Serialize `machines.models.Ipv6List` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
234
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
235
        model = machines.Ipv6List
236
        fields = ('ipv6', 'interface', 'slaac_ip', 'api_url')
Maël Kervella's avatar
Maël Kervella committed
237 238


Maël Kervella's avatar
Maël Kervella committed
239
class DomainSerializer(NamespacedHMSerializer):
240 241
    """Serialize `machines.models.Domain` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
242
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
243
        model = machines.Domain
Maël Kervella's avatar
Maël Kervella committed
244 245 246 247
        fields = ('interface_parent', 'name', 'extension', 'cname',
                  'api_url')


Maël Kervella's avatar
Maël Kervella committed
248
class IpListSerializer(NamespacedHMSerializer):
249 250
    """Serialize `machines.models.IpList` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
251
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
252
        model = machines.IpList
Maël Kervella's avatar
Maël Kervella committed
253 254 255
        fields = ('ipv4', 'ip_type', 'need_infra', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
256
class ServiceSerializer(NamespacedHMSerializer):
257 258
    """Serialize `machines.models.Service` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
259
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
260
        model = machines.Service
Maël Kervella's avatar
Maël Kervella committed
261 262 263 264
        fields = ('service_type', 'min_time_regen', 'regular_time_regen',
                  'servers', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
265
class ServiceLinkSerializer(NamespacedHMSerializer):
266 267
    """Serialize `machines.models.Service_link` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
268
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
269
        model = machines.Service_link
Maël Kervella's avatar
Maël Kervella committed
270 271
        fields = ('service', 'server', 'last_regen', 'asked_regen',
                  'need_regen', 'api_url')
Maël Kervella's avatar
Maël Kervella committed
272 273 274
        extra_kwargs = {
            'api_url': {'view_name': 'servicelink-detail'}
        }
Maël Kervella's avatar
Maël Kervella committed
275 276


Maël Kervella's avatar
Maël Kervella committed
277
class OuverturePortListSerializer(NamespacedHMSerializer):
278 279
    """Serialize `machines.models.OuverturePortList` objects.
    """
280 281 282 283 284
    tcp_ports_in = NamespacedHRField(view_name='ouvertureport-detail', many=True, read_only=True)
    udp_ports_in = NamespacedHRField(view_name='ouvertureport-detail', many=True, read_only=True)
    tcp_ports_out = NamespacedHRField(view_name='ouvertureport-detail', many=True, read_only=True)
    udp_ports_out = NamespacedHRField(view_name='ouvertureport-detail', many=True, read_only=True)

Maël Kervella's avatar
Maël Kervella committed
285
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
286
        model = machines.OuverturePortList
Maël Kervella's avatar
Maël Kervella committed
287 288 289 290
        fields = ('name', 'tcp_ports_in', 'udp_ports_in', 'tcp_ports_out',
                  'udp_ports_out', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
291
class OuverturePortSerializer(NamespacedHMSerializer):
292 293
    """Serialize `machines.models.OuverturePort` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
294
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
295
        model = machines.OuverturePort
Maël Kervella's avatar
Maël Kervella committed
296 297 298
        fields = ('begin', 'end', 'port_list', 'protocole', 'io', 'api_url')


299
# PREFERENCES
300 301


Maël Kervella's avatar
Maël Kervella committed
302
class OptionalUserSerializer(NamespacedHMSerializer):
303 304
    """Serialize `preferences.models.OptionalUser` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
305 306 307 308 309 310 311 312 313 314
    tel_mandatory = serializers.BooleanField(source='is_tel_mandatory')

    class Meta:
        model = preferences.OptionalUser
        fields = ('tel_mandatory', 'user_solde', 'solde_negatif', 'max_solde',
                  'min_online_payment', 'gpg_fingerprint',
                  'all_can_create_club', 'self_adhesion', 'shell_default')


class OptionalMachineSerializer(NamespacedHMSerializer):
315 316
    """Serialize `preferences.models.OptionalMachine` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
317 318 319 320 321 322 323 324
    class Meta:
        model = preferences.OptionalMachine
        fields = ('password_machine', 'max_lambdauser_interfaces',
                  'max_lambdauser_aliases', 'ipv6_mode', 'create_machine',
                  'ipv6')


class OptionalTopologieSerializer(NamespacedHMSerializer):
325 326
    """Serialize `preferences.models.OptionalTopologie` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
327 328 329 330 331 332 333
    class Meta:
        model = preferences.OptionalTopologie
        fields = ('radius_general_policy', 'vlan_decision_ok',
                  'vlan_decision_nok')


class GeneralOptionSerializer(NamespacedHMSerializer):
334 335
    """Serialize `preferences.models.GeneralOption` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
336 337 338 339 340 341 342 343
    class Meta:
        model = preferences.GeneralOption
        fields = ('general_message', 'search_display_page',
                  'pagination_number', 'pagination_large_number',
                  'req_expire_hrs', 'site_name', 'email_from', 'GTU_sum_up',
                  'GTU')


344
class HomeServiceSerializer(NamespacedHMSerializer):
345 346
    """Serialize `preferences.models.Service` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
347 348 349
    class Meta:
        model = preferences.Service
        fields = ('name', 'url', 'description', 'image', 'api_url')
350 351 352
        extra_kwargs = {
            'api_url': {'view_name': 'homeservice-detail'}
        }
Maël Kervella's avatar
Maël Kervella committed
353 354 355


class AssoOptionSerializer(NamespacedHMSerializer):
356 357
    """Serialize `preferences.models.AssoOption` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
358 359 360 361 362 363 364 365
    class Meta:
        model = preferences.AssoOption
        fields = ('name', 'siret', 'adresse1', 'adresse2', 'contact',
                  'telephone', 'pseudo', 'utilisateur_asso', 'payment',
                  'payment_id', 'payment_pass', 'description')


class HomeOptionSerializer(NamespacedHMSerializer):
366 367
    """Serialize `preferences.models.HomeOption` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
368 369 370 371 372 373
    class Meta:
        model = preferences.HomeOption
        fields = ('facebook_url', 'twitter_url', 'twitter_account_name')


class MailMessageOptionSerializer(NamespacedHMSerializer):
374 375
    """Serialize `preferences.models.MailMessageOption` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
376 377 378
    class Meta:
        model = preferences.MailMessageOption
        fields = ('welcome_mail_fr', 'welcome_mail_en')
379 380


Maël Kervella's avatar
Maël Kervella committed
381

382
# TOPOLOGIE
Maël Kervella's avatar
Maël Kervella committed
383 384


Maël Kervella's avatar
Maël Kervella committed
385
class StackSerializer(NamespacedHMSerializer):
386 387
    """Serialize `topologie.models.Stack` objects
    """
Maël Kervella's avatar
Maël Kervella committed
388
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
389
        model = topologie.Stack
Maël Kervella's avatar
Maël Kervella committed
390 391 392 393
        fields = ('name', 'stack_id', 'details', 'member_id_min',
                  'member_id_max', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
394
class AccessPointSerializer(NamespacedHMSerializer):
395 396
    """Serialize `topologie.models.AccessPoint` objects
    """
Maël Kervella's avatar
Maël Kervella committed
397
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
398
        model = topologie.AccessPoint
Maël Kervella's avatar
Maël Kervella committed
399 400 401
        fields = ('user', 'name', 'active', 'location', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
402
class SwitchSerializer(NamespacedHMSerializer):
403 404
    """Serialize `topologie.models.Switch` objects
    """
Maël Kervella's avatar
Maël Kervella committed
405 406
    port_amount = serializers.IntegerField(source='number')
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
407
        model = topologie.Switch
Maël Kervella's avatar
Maël Kervella committed
408 409
        fields = ('user', 'name', 'active', 'port_amount', 'stack',
                  'stack_member_id', 'model', 'switchbay', 'api_url')
Maël Kervella's avatar
Maël Kervella committed
410 411


412 413 414 415 416 417 418 419
class ServerSerializer(NamespacedHMSerializer):
    """Serialize `topologie.models.Server` objects
    """
    class Meta:
        model = topologie.Server
        fields = ('user', 'name', 'active', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
420
class ModelSwitchSerializer(NamespacedHMSerializer):
421 422
    """Serialize `topologie.models.ModelSwitch` objects
    """
Maël Kervella's avatar
Maël Kervella committed
423
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
424
        model = topologie.ModelSwitch
Maël Kervella's avatar
Maël Kervella committed
425 426 427
        fields = ('reference', 'constructor', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
428
class ConstructorSwitchSerializer(NamespacedHMSerializer):
429 430
    """Serialize `topologie.models.ConstructorSwitch` objects
    """
Maël Kervella's avatar
Maël Kervella committed
431
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
432
        model = topologie.ConstructorSwitch
Maël Kervella's avatar
Maël Kervella committed
433 434 435
        fields = ('name', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
436
class SwitchBaySerializer(NamespacedHMSerializer):
437 438
    """Serialize `topologie.models.SwitchBay` objects
    """
Maël Kervella's avatar
Maël Kervella committed
439
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
440
        model = topologie.SwitchBay
Maël Kervella's avatar
Maël Kervella committed
441 442 443
        fields = ('name', 'building', 'info', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
444
class BuildingSerializer(NamespacedHMSerializer):
445 446
    """Serialize `topologie.models.Building` objects
    """
Maël Kervella's avatar
Maël Kervella committed
447
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
448
        model = topologie.Building
Maël Kervella's avatar
Maël Kervella committed
449 450 451
        fields = ('name', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
452
class SwitchPortSerializer(NamespacedHMSerializer):
453 454
    """Serialize `topologie.models.Port` objects
    """
Maël Kervella's avatar
Maël Kervella committed
455
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
456
        model = topologie.Port
Maël Kervella's avatar
Maël Kervella committed
457 458
        fields = ('switch', 'port', 'room', 'machine_interface', 'related',
                  'radius', 'vlan_force', 'details', 'api_url')
Maël Kervella's avatar
Maël Kervella committed
459 460 461 462
        extra_kwargs = {
            'related': {'view_name': 'switchport-detail'},
            'api_url': {'view_name': 'switchport-detail'}
        }
Maël Kervella's avatar
Maël Kervella committed
463 464


Maël Kervella's avatar
Maël Kervella committed
465
class RoomSerializer(NamespacedHMSerializer):
466 467
    """Serialize `topologie.models.Room` objects
    """
Maël Kervella's avatar
Maël Kervella committed
468
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
469
        model = topologie.Room
Maël Kervella's avatar
Maël Kervella committed
470 471 472
        fields = ('name', 'details', 'api_url')


473
# USERS
474

Maël Kervella's avatar
Maël Kervella committed
475

Maël Kervella's avatar
Maël Kervella committed
476
class UserSerializer(NamespacedHMSerializer):
477 478
    """Serialize `users.models.User` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
479 480
    access = serializers.BooleanField(source='has_access')
    uid = serializers.IntegerField(source='uid_number')
Maël Kervella's avatar
Maël Kervella committed
481 482

    class Meta:
Maël Kervella's avatar
Maël Kervella committed
483
        model = users.User
Maël Kervella's avatar
Maël Kervella committed
484
        fields = ('name', 'pseudo', 'email', 'school', 'shell', 'comment',
Maël Kervella's avatar
Maël Kervella committed
485 486 487 488 489
                  'state', 'registered', 'telephone', 'solde', 'access',
                  'end_access', 'uid', 'class_name', 'api_url')
        extra_kwargs = {
            'shell': {'view_name': 'shell-detail'}
        }
Maël Kervella's avatar
Maël Kervella committed
490

Maël Kervella's avatar
Maël Kervella committed
491

Maël Kervella's avatar
Maël Kervella committed
492
class ClubSerializer(NamespacedHMSerializer):
493 494
    """Serialize `users.models.Club` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
495 496 497
    name = serializers.CharField(source='surname')
    access = serializers.BooleanField(source='has_access')
    uid = serializers.IntegerField(source='uid_number')
Maël Kervella's avatar
Maël Kervella committed
498 499

    class Meta:
Maël Kervella's avatar
Maël Kervella committed
500
        model = users.Club
Maël Kervella's avatar
Maël Kervella committed
501
        fields = ('name', 'pseudo', 'email', 'school', 'shell', 'comment',
Maël Kervella's avatar
Maël Kervella committed
502
                  'state', 'registered', 'telephone', 'solde', 'room',
Maël Kervella's avatar
Maël Kervella committed
503 504
                  'access', 'end_access', 'administrators', 'members',
                  'mailing', 'uid', 'api_url')
Maël Kervella's avatar
Maël Kervella committed
505 506 507
        extra_kwargs = {
            'shell': {'view_name': 'shell-detail'}
        }
Maël Kervella's avatar
Maël Kervella committed
508 509


Maël Kervella's avatar
Maël Kervella committed
510
class AdherentSerializer(NamespacedHMSerializer):
511 512
    """Serialize `users.models.Adherent` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
513 514
    access = serializers.BooleanField(source='has_access')
    uid = serializers.IntegerField(source='uid_number')
Maël Kervella's avatar
Maël Kervella committed
515

Maël Kervella's avatar
Maël Kervella committed
516
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
517
        model = users.Adherent
Maël Kervella's avatar
Maël Kervella committed
518
        fields = ('name', 'surname', 'pseudo', 'email', 'school', 'shell',
Maël Kervella's avatar
Maël Kervella committed
519
                  'comment', 'state', 'registered', 'telephone', 'room',
Maël Kervella's avatar
Maël Kervella committed
520
                  'solde', 'access', 'end_access', 'uid', 'api_url')
Maël Kervella's avatar
Maël Kervella committed
521 522 523
        extra_kwargs = {
            'shell': {'view_name': 'shell-detail'}
        }
Maël Kervella's avatar
Maël Kervella committed
524 525


Maël Kervella's avatar
Maël Kervella committed
526
class ServiceUserSerializer(NamespacedHMSerializer):
527 528
    """Serialize `users.models.ServiceUser` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
529
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
530
        model = users.ServiceUser
Maël Kervella's avatar
Maël Kervella committed
531
        fields = ('pseudo', 'access_group', 'comment', 'api_url')
Maël Kervella's avatar
Maël Kervella committed
532 533


Maël Kervella's avatar
Maël Kervella committed
534
class SchoolSerializer(NamespacedHMSerializer):
535 536
    """Serialize `users.models.School` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
537
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
538
        model = users.School
Maël Kervella's avatar
Maël Kervella committed
539
        fields = ('name', 'api_url')
Maël Kervella's avatar
Maël Kervella committed
540 541


Maël Kervella's avatar
Maël Kervella committed
542
class ListRightSerializer(NamespacedHMSerializer):
543 544
    """Serialize `users.models.ListRight` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
545
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
546
        model = users.ListRight
Maël Kervella's avatar
Maël Kervella committed
547
        fields = ('unix_name', 'gid', 'critical', 'details', 'api_url')
Maël Kervella's avatar
Maël Kervella committed
548 549


Maël Kervella's avatar
Maël Kervella committed
550
class ShellSerializer(NamespacedHMSerializer):
551 552
    """Serialize `users.models.ListShell` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
553
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
554
        model = users.ListShell
Maël Kervella's avatar
Maël Kervella committed
555
        fields = ('shell', 'api_url')
Maël Kervella's avatar
Maël Kervella committed
556 557 558
        extra_kwargs = {
            'api_url': {'view_name': 'shell-detail'}
        }
Maël Kervella's avatar
Maël Kervella committed
559 560


Maël Kervella's avatar
Maël Kervella committed
561
class BanSerializer(NamespacedHMSerializer):
562 563
    """Serialize `users.models.Ban` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
564
    active = serializers.BooleanField(source='is_active')
Maël Kervella's avatar
Maël Kervella committed
565 566

    class Meta:
Maël Kervella's avatar
Maël Kervella committed
567
        model = users.Ban
Maël Kervella's avatar
Maël Kervella committed
568 569
        fields = ('user', 'raison', 'date_start', 'date_end', 'state',
                  'active', 'api_url')
Maël Kervella's avatar
Maël Kervella committed
570 571


Maël Kervella's avatar
Maël Kervella committed
572
class WhitelistSerializer(NamespacedHMSerializer):
573 574
    """Serialize `users.models.Whitelist` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
575
    active = serializers.BooleanField(source='is_active')
Maël Kervella's avatar
Maël Kervella committed
576 577

    class Meta:
Maël Kervella's avatar
Maël Kervella committed
578
        model = users.Whitelist
Maël Kervella's avatar
Maël Kervella committed
579
        fields = ('user', 'raison', 'date_start', 'date_end', 'active', 'api_url')
Maël Kervella's avatar
Maël Kervella committed
580 581


582
# SERVICE REGEN
583 584 585


class ServiceRegenSerializer(NamespacedHMSerializer):
586 587
    """Serialize the data about the services to regen.
    """
588 589
    hostname = serializers.CharField(source='server.domain.name', read_only=True)
    service_name = serializers.CharField(source='service.service_type', read_only=True)
590
    need_regen = serializers.BooleanField()
591 592 593 594 595

    class Meta:
        model = machines.Service_link
        fields = ('hostname', 'service_name', 'need_regen', 'api_url')
        extra_kwargs = {
596
            'api_url': {'view_name': 'serviceregen-detail'}
597 598 599
        }


Maël Kervella's avatar
Maël Kervella committed
600 601 602 603
# DHCP


class HostMacIpSerializer(serializers.ModelSerializer):
604 605 606
    """Serialize the data about the hostname-ipv4-mac address association
    to build the DHCP lease files.
    """
Maël Kervella's avatar
Maël Kervella committed
607 608
    hostname = serializers.CharField(source='domain.name', read_only=True)
    extension = serializers.CharField(source='domain.extension.name', read_only=True)
Maël Kervella's avatar
Maël Kervella committed
609
    mac_address = serializers.CharField(read_only=True)
Maël Kervella's avatar
Maël Kervella committed
610 611 612 613 614
    ipv4 = serializers.CharField(source='ipv4.ipv4', read_only=True)

    class Meta:
        model = machines.Interface
        fields = ('hostname', 'extension', 'mac_address', 'ipv4')
615 616 617 618 619 620


# DNS


class SOARecordSerializer(SOASerializer):
621 622 623
    """Serialize `machines.models.SOA` objects with the data needed to
    generate a SOA DNS record.
    """
624 625 626 627 628 629
    class Meta:
        model = machines.SOA
        fields = ('name', 'mail', 'refresh', 'retry', 'expire', 'ttl')


class OriginV4RecordSerializer(IpListSerializer):
630 631 632
    """Serialize `machines.models.IpList` objects with the data needed to
    generate an IPv4 Origin DNS record.
    """
633 634 635 636 637
    class Meta(IpListSerializer.Meta):
        fields = ('ipv4',)


class NSRecordSerializer(NsSerializer):
638 639 640
    """Serialize `machines.models.Ns` objects with the data needed to
    generate a NS DNS record.
    """
641 642 643 644 645 646 647
    target = serializers.CharField(source='ns.name', read_only=True)

    class Meta(NsSerializer.Meta):
        fields = ('target',)


class MXRecordSerializer(MxSerializer):
648 649 650
    """Serialize `machines.models.Mx` objects with the data needed to
    generate a MX DNS record.
    """
651 652 653 654 655 656 657
    target = serializers.CharField(source='name.name', read_only=True)

    class Meta(MxSerializer.Meta):
        fields = ('target', 'priority')


class TXTRecordSerializer(TxtSerializer):
658 659 660
    """Serialize `machines.models.Txt` objects with the data needed to
    generate a TXT DNS record.
    """
661 662 663 664 665
    class Meta(TxtSerializer.Meta):
        fields = ('field1', 'field2')


class SRVRecordSerializer(SrvSerializer):
666 667 668
    """Serialize `machines.models.Srv` objects with the data needed to
    generate a SRV DNS record.
    """
669 670 671 672 673 674 675
    target = serializers.CharField(source='target.name', read_only=True)

    class Meta(SrvSerializer.Meta):
        fields = ('service', 'protocole', 'ttl', 'priority', 'weight', 'port', 'target')


class ARecordSerializer(serializers.ModelSerializer):
676 677 678
    """Serialize `machines.models.Interface` objects with the data needed to
    generate a A DNS record.
    """
679 680 681 682 683 684 685 686 687
    hostname = serializers.CharField(source='domain.name', read_only=True)
    ipv4 = serializers.CharField(source='ipv4.ipv4', read_only=True)

    class Meta:
        model = machines.Interface
        fields = ('hostname', 'ipv4')


class AAAARecordSerializer(serializers.ModelSerializer):
688 689 690
    """Serialize `machines.models.Interface` objects with the data needed to
    generate a AAAA DNS record.
    """
691
    hostname = serializers.CharField(source='domain.name', read_only=True)
692
    ipv6 = Ipv6ListSerializer(many=True, read_only=True)
693 694 695 696 697 698 699

    class Meta:
        model = machines.Interface
        fields = ('hostname', 'ipv6')


class CNAMERecordSerializer(serializers.ModelSerializer):
700 701 702
    """Serialize `machines.models.Domain` objects with the data needed to
    generate a CNAME DNS record.
    """
703 704
    alias = serializers.CharField(source='cname.name', read_only=True)
    hostname = serializers.CharField(source='name', read_only=True)
Maël Kervella's avatar
Maël Kervella committed
705
    extension = serializers.CharField(source='extension.name', read_only=True)
706 707 708

    class Meta:
        model = machines.Domain
Maël Kervella's avatar
Maël Kervella committed
709
        fields = ('alias', 'hostname', 'extension')
710 711


712
class DNSZonesSerializer(serializers.ModelSerializer):
713 714
    """Serialize the data about DNS Zones.
    """
715 716 717
    soa = SOARecordSerializer()
    ns_records = NSRecordSerializer(many=True, source='ns_set')
    originv4 = OriginV4RecordSerializer(source='origin')
718
    originv6 = serializers.CharField(source='origin_v6')
719 720 721 722 723 724 725 726 727 728 729
    mx_records = MXRecordSerializer(many=True, source='mx_set')
    txt_records = TXTRecordSerializer(many=True, source='txt_set')
    srv_records = SRVRecordSerializer(many=True, source='srv_set')
    a_records = ARecordSerializer(many=True, source='get_associated_a_records')
    aaaa_records = AAAARecordSerializer(many=True, source='get_associated_aaaa_records')
    cname_records = CNAMERecordSerializer(many=True, source='get_associated_cname_records')

    class Meta:
        model = machines.Extension
        fields = ('name', 'soa', 'ns_records', 'originv4', 'originv6',
                  'mx_records', 'txt_records', 'srv_records', 'a_records',
730
                  'aaaa_records', 'cname_records')
Maël Kervella's avatar
Maël Kervella committed
731 732


733
# MAILING
Maël Kervella's avatar
Maël Kervella committed
734 735 736


class MailingMemberSerializer(UserSerializer):
737 738
    """Serialize the data about a mailing member.
    """
Maël Kervella's avatar
Maël Kervella committed
739 740 741 742
    class Meta(UserSerializer.Meta):
        fields = ('name', 'pseudo', 'email')

class MailingSerializer(ClubSerializer):
743 744
    """Serialize the data about a mailing.
    """
Maël Kervella's avatar
Maël Kervella committed
745 746 747 748 749
    members = MailingMemberSerializer(many=True)
    admins = MailingMemberSerializer(source='administrators', many=True)

    class Meta(ClubSerializer.Meta):
        fields = ('name', 'members', 'admins')