Ce serveur Gitlab sera éteint le 30 juin 2020, pensez à migrer vos projets vers les serveurs gitlab-research.centralesupelec.fr et gitlab-student.centralesupelec.fr !

serializers.py 28.3 KB
Newer Older
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
"""
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
65

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
123 124


Maël Kervella's avatar
Maël Kervella committed
125
class MachineSerializer(NamespacedHMSerializer):
126 127
    """Serialize `machines.models.Machine` objects.
    """
128
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
129
        model = machines.Machine
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.
    """
136
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
137
        model = machines.MachineType
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.
    """
144
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
145
        model = machines.IpType
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.
    """
154
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
155
        model = machines.Vlan
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.
    """
162
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
163
        model = machines.Nas
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.
    """
171
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
172
        model = machines.SOA
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.
    """
180
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
181
        model = machines.Extension
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.
    """
189
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
190
        model = machines.Mx
191 192 193
        fields = ('zone', 'priority', 'name', 'api_url')


194 195 196 197 198 199 200
class DNameSerializer(NamespacedHMSerializer):
    """Serialize `machines.models.DName` objects.
    """
    class Meta:
        model = machines.DName
        fields = ('zone', 'alias', 'api_url')

Maël Kervella's avatar
Maël Kervella committed
201
class NsSerializer(NamespacedHMSerializer):
202 203
    """Serialize `machines.models.Ns` objects.
    """
204
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
205
        model = machines.Ns
206 207 208
        fields = ('zone', 'ns', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
209
class TxtSerializer(NamespacedHMSerializer):
210 211
    """Serialize `machines.models.Txt` objects.
    """
212
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
213
        model = machines.Txt
214 215 216
        fields = ('zone', 'field1', 'field2', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
217
class SrvSerializer(NamespacedHMSerializer):
218 219
    """Serialize `machines.models.Srv` objects.
    """
220
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
221
        model = machines.Srv
222 223 224
        fields = ('service', 'protocole', 'extension', 'ttl', 'priority',
                  'weight', 'port', 'target', 'api_url')

225 226 227 228 229 230 231
class SshFpSerializer(NamespacedHMSerializer):
    """Serialize `machines.models.SSHFP` objects.
    """
    class Meta:
        model = machines.SshFp
        field = ('machine', 'pub_key_entry', 'algo', 'comment', 'api_url')

232

Maël Kervella's avatar
Maël Kervella committed
233
class InterfaceSerializer(NamespacedHMSerializer):
234 235
    """Serialize `machines.models.Interface` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
236
    mac_address = serializers.CharField()
237 238 239
    active = serializers.BooleanField(source='is_active')

    class Meta:
Maël Kervella's avatar
Maël Kervella committed
240
        model = machines.Interface
241 242 243 244
        fields = ('ipv4', 'mac_address', 'machine', 'type', 'details',
                  'port_lists', 'active', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
245
class Ipv6ListSerializer(NamespacedHMSerializer):
246 247
    """Serialize `machines.models.Ipv6List` objects.
    """
248
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
249
        model = machines.Ipv6List
250
        fields = ('ipv6', 'interface', 'slaac_ip', 'api_url')
251 252


Maël Kervella's avatar
Maël Kervella committed
253
class DomainSerializer(NamespacedHMSerializer):
254 255
    """Serialize `machines.models.Domain` objects.
    """
256
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
257
        model = machines.Domain
258 259 260 261
        fields = ('interface_parent', 'name', 'extension', 'cname',
                  'api_url')


Maël Kervella's avatar
Maël Kervella committed
262
class IpListSerializer(NamespacedHMSerializer):
263 264
    """Serialize `machines.models.IpList` objects.
    """
265
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
266
        model = machines.IpList
267 268 269
        fields = ('ipv4', 'ip_type', 'need_infra', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
270
class ServiceSerializer(NamespacedHMSerializer):
271 272
    """Serialize `machines.models.Service` objects.
    """
273
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
274
        model = machines.Service
275 276 277 278
        fields = ('service_type', 'min_time_regen', 'regular_time_regen',
                  'servers', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
279
class ServiceLinkSerializer(NamespacedHMSerializer):
280 281
    """Serialize `machines.models.Service_link` objects.
    """
282
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
283
        model = machines.Service_link
284 285
        fields = ('service', 'server', 'last_regen', 'asked_regen',
                  'need_regen', 'api_url')
Maël Kervella's avatar
Maël Kervella committed
286 287 288
        extra_kwargs = {
            'api_url': {'view_name': 'servicelink-detail'}
        }
289 290


Maël Kervella's avatar
Maël Kervella committed
291
class OuverturePortListSerializer(NamespacedHMSerializer):
292 293
    """Serialize `machines.models.OuverturePortList` objects.
    """
294 295 296 297 298
    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)

299
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
300
        model = machines.OuverturePortList
301 302 303 304
        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
305
class OuverturePortSerializer(NamespacedHMSerializer):
306 307
    """Serialize `machines.models.OuverturePort` objects.
    """
308
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
309
        model = machines.OuverturePort
310 311 312
        fields = ('begin', 'end', 'port_list', 'protocole', 'io', 'api_url')


313
# PREFERENCES
314 315


316
class OptionalUserSerializer(NamespacedHMSerializer):
317 318
    """Serialize `preferences.models.OptionalUser` objects.
    """
319 320 321 322 323 324 325 326 327 328
    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):
329 330
    """Serialize `preferences.models.OptionalMachine` objects.
    """
331 332 333 334 335 336 337 338
    class Meta:
        model = preferences.OptionalMachine
        fields = ('password_machine', 'max_lambdauser_interfaces',
                  'max_lambdauser_aliases', 'ipv6_mode', 'create_machine',
                  'ipv6')


class OptionalTopologieSerializer(NamespacedHMSerializer):
339 340
    """Serialize `preferences.models.OptionalTopologie` objects.
    """
341

342 343 344
    class Meta:
        model = preferences.OptionalTopologie
        fields = ('radius_general_policy', 'vlan_decision_ok',
345
                  'vlan_decision_nok')
346 347 348


class GeneralOptionSerializer(NamespacedHMSerializer):
349 350
    """Serialize `preferences.models.GeneralOption` objects.
    """
351 352 353 354 355 356 357 358
    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')


359
class HomeServiceSerializer(NamespacedHMSerializer):
360 361
    """Serialize `preferences.models.Service` objects.
    """
362 363 364
    class Meta:
        model = preferences.Service
        fields = ('name', 'url', 'description', 'image', 'api_url')
365 366 367
        extra_kwargs = {
            'api_url': {'view_name': 'homeservice-detail'}
        }
368 369 370


class AssoOptionSerializer(NamespacedHMSerializer):
371 372
    """Serialize `preferences.models.AssoOption` objects.
    """
373 374 375 376 377 378 379 380
    class Meta:
        model = preferences.AssoOption
        fields = ('name', 'siret', 'adresse1', 'adresse2', 'contact',
                  'telephone', 'pseudo', 'utilisateur_asso', 'payment',
                  'payment_id', 'payment_pass', 'description')


class HomeOptionSerializer(NamespacedHMSerializer):
381 382
    """Serialize `preferences.models.HomeOption` objects.
    """
383 384 385 386 387 388
    class Meta:
        model = preferences.HomeOption
        fields = ('facebook_url', 'twitter_url', 'twitter_account_name')


class MailMessageOptionSerializer(NamespacedHMSerializer):
389 390
    """Serialize `preferences.models.MailMessageOption` objects.
    """
391 392 393
    class Meta:
        model = preferences.MailMessageOption
        fields = ('welcome_mail_fr', 'welcome_mail_en')
394 395


396

397
# TOPOLOGIE
398 399


Maël Kervella's avatar
Maël Kervella committed
400
class StackSerializer(NamespacedHMSerializer):
401 402
    """Serialize `topologie.models.Stack` objects
    """
403
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
404
        model = topologie.Stack
405 406 407 408
        fields = ('name', 'stack_id', 'details', 'member_id_min',
                  'member_id_max', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
409
class AccessPointSerializer(NamespacedHMSerializer):
410 411
    """Serialize `topologie.models.AccessPoint` objects
    """
412
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
413
        model = topologie.AccessPoint
414 415 416
        fields = ('user', 'name', 'active', 'location', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
417
class SwitchSerializer(NamespacedHMSerializer):
418 419
    """Serialize `topologie.models.Switch` objects
    """
420 421
    port_amount = serializers.IntegerField(source='number')
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
422
        model = topologie.Switch
Maël Kervella's avatar
Maël Kervella committed
423 424
        fields = ('user', 'name', 'active', 'port_amount', 'stack',
                  'stack_member_id', 'model', 'switchbay', 'api_url')
425 426


427 428 429 430 431 432 433 434
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
435
class ModelSwitchSerializer(NamespacedHMSerializer):
436 437
    """Serialize `topologie.models.ModelSwitch` objects
    """
438
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
439
        model = topologie.ModelSwitch
440 441 442
        fields = ('reference', 'constructor', 'api_url')


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


Maël Kervella's avatar
Maël Kervella committed
451
class SwitchBaySerializer(NamespacedHMSerializer):
452 453
    """Serialize `topologie.models.SwitchBay` objects
    """
454
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
455
        model = topologie.SwitchBay
456 457 458
        fields = ('name', 'building', 'info', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
459
class BuildingSerializer(NamespacedHMSerializer):
460 461
    """Serialize `topologie.models.Building` objects
    """
462
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
463
        model = topologie.Building
464 465 466
        fields = ('name', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
467
class SwitchPortSerializer(NamespacedHMSerializer):
468 469
    """Serialize `topologie.models.Port` objects
    """
470
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
471
        model = topologie.Port
472
        fields = ('switch', 'port', 'room', 'machine_interface', 'related',
473
                  'custom_profile', 'state', 'details', 'api_url')
Maël Kervella's avatar
Maël Kervella committed
474 475
        extra_kwargs = {
            'related': {'view_name': 'switchport-detail'},
476
            'api_url': {'view_name': 'switchport-detail'},
Maël Kervella's avatar
Maël Kervella committed
477
        }
478 479


Maël Kervella's avatar
Maël Kervella committed
480
class RoomSerializer(NamespacedHMSerializer):
481 482
    """Serialize `topologie.models.Room` objects
    """
483
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
484
        model = topologie.Room
485 486 487
        fields = ('name', 'details', 'api_url')


488 489 490 491 492 493 494 495 496 497 498 499
class PortProfileSerializer(NamespacedHMSerializer):
    vlan_untagged = VlanSerializer(read_only=True)

    class Meta:
        model = topologie.PortProfile
        fields = ('name', 'profil_default', 'vlan_untagged', 'vlan_tagged',
                  'radius_type', 'radius_mode', 'speed', 'mac_limit',
                  'flow_control', 'dhcp_snooping', 'dhcpv6_snooping',
                  'arp_protect', 'ra_guard', 'loop_protect', 'vlan_untagged',
                  'vlan_tagged')


500
# USERS
501

502

Maël Kervella's avatar
Maël Kervella committed
503
class UserSerializer(NamespacedHMSerializer):
504 505
    """Serialize `users.models.User` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
506 507
    access = serializers.BooleanField(source='has_access')
    uid = serializers.IntegerField(source='uid_number')
508 509

    class Meta:
Maël Kervella's avatar
Maël Kervella committed
510
        model = users.User
511 512
        fields = ('surname', 'pseudo', 'email', 'local_email_redirect',
                  'local_email_enabled', 'school', 'shell', 'comment',
Maël Kervella's avatar
Maël Kervella committed
513 514 515 516 517
                  'state', 'registered', 'telephone', 'solde', 'access',
                  'end_access', 'uid', 'class_name', 'api_url')
        extra_kwargs = {
            'shell': {'view_name': 'shell-detail'}
        }
518

519

Maël Kervella's avatar
Maël Kervella committed
520
class ClubSerializer(NamespacedHMSerializer):
521 522
    """Serialize `users.models.Club` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
523 524 525
    name = serializers.CharField(source='surname')
    access = serializers.BooleanField(source='has_access')
    uid = serializers.IntegerField(source='uid_number')
526 527

    class Meta:
Maël Kervella's avatar
Maël Kervella committed
528
        model = users.Club
529 530
        fields = ('name', 'pseudo', 'email', 'local_email_redirect',
                  'local_email_enabled', 'school', 'shell', 'comment',
Maël Kervella's avatar
Maël Kervella committed
531
                  'state', 'registered', 'telephone', 'solde', 'room',
Maël Kervella's avatar
Maël Kervella committed
532 533
                  'access', 'end_access', 'administrators', 'members',
                  'mailing', 'uid', 'api_url')
Maël Kervella's avatar
Maël Kervella committed
534 535 536
        extra_kwargs = {
            'shell': {'view_name': 'shell-detail'}
        }
537 538


Maël Kervella's avatar
Maël Kervella committed
539
class AdherentSerializer(NamespacedHMSerializer):
540 541
    """Serialize `users.models.Adherent` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
542 543
    access = serializers.BooleanField(source='has_access')
    uid = serializers.IntegerField(source='uid_number')
544

545
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
546
        model = users.Adherent
547 548 549
        fields = ('name', 'surname', 'pseudo', 'email', 'local_email_redirect',
                  'local_email_enabled', 'school', 'shell', 'comment',
                  'state', 'registered', 'telephone', 'room', 'solde',
550
                  'access', 'end_access', 'uid', 'api_url','gid')
Maël Kervella's avatar
Maël Kervella committed
551 552 553
        extra_kwargs = {
            'shell': {'view_name': 'shell-detail'}
        }
554

555 556 557 558 559 560 561 562 563
class HomeCreationSerializer(NamespacedHMSerializer):
    """Serialize 'users.models.User' minimal infos to create home
    """
    uid = serializers.IntegerField(source='uid_number')
    gid = serializers.IntegerField(source='gid_number')

    class Meta:
        model = users.User
        fields = ('pseudo', 'uid', 'gid')
564

Maël Kervella's avatar
Maël Kervella committed
565
class ServiceUserSerializer(NamespacedHMSerializer):
566 567
    """Serialize `users.models.ServiceUser` objects.
    """
568
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
569
        model = users.ServiceUser
Maël Kervella's avatar
Maël Kervella committed
570
        fields = ('pseudo', 'access_group', 'comment', 'api_url')
571 572


Maël Kervella's avatar
Maël Kervella committed
573
class SchoolSerializer(NamespacedHMSerializer):
574 575
    """Serialize `users.models.School` objects.
    """
576
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
577
        model = users.School
Maël Kervella's avatar
Maël Kervella committed
578
        fields = ('name', 'api_url')
579 580


Maël Kervella's avatar
Maël Kervella committed
581
class ListRightSerializer(NamespacedHMSerializer):
582 583
    """Serialize `users.models.ListRight` objects.
    """
584
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
585
        model = users.ListRight
Maël Kervella's avatar
Maël Kervella committed
586
        fields = ('unix_name', 'gid', 'critical', 'details', 'api_url')
587 588


Maël Kervella's avatar
Maël Kervella committed
589
class ShellSerializer(NamespacedHMSerializer):
590 591
    """Serialize `users.models.ListShell` objects.
    """
592
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
593
        model = users.ListShell
Maël Kervella's avatar
Maël Kervella committed
594
        fields = ('shell', 'api_url')
Maël Kervella's avatar
Maël Kervella committed
595 596 597
        extra_kwargs = {
            'api_url': {'view_name': 'shell-detail'}
        }
598 599


Maël Kervella's avatar
Maël Kervella committed
600
class BanSerializer(NamespacedHMSerializer):
601 602
    """Serialize `users.models.Ban` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
603
    active = serializers.BooleanField(source='is_active')
604 605

    class Meta:
Maël Kervella's avatar
Maël Kervella committed
606
        model = users.Ban
Maël Kervella's avatar
Maël Kervella committed
607 608
        fields = ('user', 'raison', 'date_start', 'date_end', 'state',
                  'active', 'api_url')
609 610


Maël Kervella's avatar
Maël Kervella committed
611
class WhitelistSerializer(NamespacedHMSerializer):
612 613
    """Serialize `users.models.Whitelist` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
614
    active = serializers.BooleanField(source='is_active')
615 616

    class Meta:
Maël Kervella's avatar
Maël Kervella committed
617
        model = users.Whitelist
Maël Kervella's avatar
Maël Kervella committed
618
        fields = ('user', 'raison', 'date_start', 'date_end', 'active', 'api_url')
619 620


621 622
class EMailAddressSerializer(NamespacedHMSerializer):
    """Serialize `users.models.EMailAddress` objects.
623
    """
624
    user = serializers.CharField(source='user.pseudo', read_only=True)
625
    class Meta:
626
        model = users.EMailAddress
627
        fields = ('user', 'local_part', 'complete_email_address', 'api_url')
628 629


630
# SERVICE REGEN
631 632 633


class ServiceRegenSerializer(NamespacedHMSerializer):
634 635
    """Serialize the data about the services to regen.
    """
636 637
    hostname = serializers.CharField(source='server.domain.name', read_only=True)
    service_name = serializers.CharField(source='service.service_type', read_only=True)
638
    need_regen = serializers.BooleanField()
639 640 641 642 643

    class Meta:
        model = machines.Service_link
        fields = ('hostname', 'service_name', 'need_regen', 'api_url')
        extra_kwargs = {
644
            'api_url': {'view_name': 'serviceregen-detail'}
645
        }
646

647

648 649 650 651
# LOCAL EMAILS


class LocalEmailUsersSerializer(NamespacedHMSerializer):
652
    email_address = EMailAddressSerializer(
653 654 655
        read_only=True,
        many=True
    )
656 657 658

    class Meta:
        model = users.User
659
        fields = ('local_email_enabled', 'local_email_redirect',
660
                  'email_address', 'email')
661 662


663 664 665 666 667
#Firewall

class FirewallPortListSerializer(serializers.ModelSerializer):
    class Meta:
        model = machines.OuverturePort
668
        fields = ('begin', 'end', 'protocole', 'io', 'show_port')
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684

class FirewallOuverturePortListSerializer(serializers.ModelSerializer):
    tcp_ports_in = FirewallPortListSerializer(many=True, read_only=True)
    udp_ports_in = FirewallPortListSerializer(many=True, read_only=True)
    tcp_ports_out = FirewallPortListSerializer(many=True, read_only=True)
    udp_ports_out = FirewallPortListSerializer(many=True, read_only=True)

    class Meta:
        model = machines.OuverturePortList
        fields = ('tcp_ports_in', 'udp_ports_in', 'tcp_ports_out', 'udp_ports_out')

class SubnetPortsOpenSerializer(serializers.ModelSerializer):
    ouverture_ports = FirewallOuverturePortListSerializer(read_only=True)

    class Meta:
        model = machines.IpType
685
        fields = ('type', 'domaine_ip_start', 'domaine_ip_stop', 'complete_prefixv6', 'ouverture_ports')
686

687 688 689 690 691 692 693 694 695
class InterfacePortsOpenSerializer(serializers.ModelSerializer):
    port_lists = FirewallOuverturePortListSerializer(read_only=True, many=True)
    ipv4 = serializers.CharField(source='ipv4.ipv4', read_only=True)
    ipv6 = Ipv6ListSerializer(many=True, read_only=True)

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

696 697 698 699
# DHCP


class HostMacIpSerializer(serializers.ModelSerializer):
700 701 702
    """Serialize the data about the hostname-ipv4-mac address association
    to build the DHCP lease files.
    """
703 704
    hostname = serializers.CharField(source='domain.name', read_only=True)
    extension = serializers.CharField(source='domain.extension.name', read_only=True)
705
    mac_address = serializers.CharField(read_only=True)
706 707 708 709 710
    ipv4 = serializers.CharField(source='ipv4.ipv4', read_only=True)

    class Meta:
        model = machines.Interface
        fields = ('hostname', 'extension', 'mac_address', 'ipv4')
711 712 713 714 715 716


# DNS


class SOARecordSerializer(SOASerializer):
717 718 719
    """Serialize `machines.models.SOA` objects with the data needed to
    generate a SOA DNS record.
    """
720 721 722 723 724 725
    class Meta:
        model = machines.SOA
        fields = ('name', 'mail', 'refresh', 'retry', 'expire', 'ttl')


class OriginV4RecordSerializer(IpListSerializer):
726 727 728
    """Serialize `machines.models.IpList` objects with the data needed to
    generate an IPv4 Origin DNS record.
    """
729 730 731 732 733
    class Meta(IpListSerializer.Meta):
        fields = ('ipv4',)


class NSRecordSerializer(NsSerializer):
734 735 736
    """Serialize `machines.models.Ns` objects with the data needed to
    generate a NS DNS record.
    """
737
    target = serializers.CharField(source='ns', read_only=True)
738 739 740 741 742 743

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


class MXRecordSerializer(MxSerializer):
744 745 746
    """Serialize `machines.models.Mx` objects with the data needed to
    generate a MX DNS record.
    """
747
    target = serializers.CharField(source='name', read_only=True)
748 749 750 751 752 753

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


class TXTRecordSerializer(TxtSerializer):
754 755 756
    """Serialize `machines.models.Txt` objects with the data needed to
    generate a TXT DNS record.
    """
757 758 759 760 761
    class Meta(TxtSerializer.Meta):
        fields = ('field1', 'field2')


class SRVRecordSerializer(SrvSerializer):
762 763 764
    """Serialize `machines.models.Srv` objects with the data needed to
    generate a SRV DNS record.
    """
765 766 767 768 769 770
    target = serializers.CharField(source='target.name', read_only=True)

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


771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
class SSHFPRecordSerializer(SshFpSerializer):
    """Serialize `machines.models.SshFp` objects with the data needed to
    generate a SSHFP DNS record.
    """
    class Meta(SshFpSerializer.Meta):
        fields = ('algo_id', 'hash')


class SSHFPInterfaceSerializer(serializers.ModelSerializer):
    """Serialize `machines.models.Domain` objects with the data needed to
    generate a CNAME DNS record.
    """
    hostname = serializers.CharField(source='domain.name', read_only=True)
    sshfp = SSHFPRecordSerializer(source='machine.sshfp_set', many=True, read_only=True)

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


791
class ARecordSerializer(serializers.ModelSerializer):
792 793 794
    """Serialize `machines.models.Interface` objects with the data needed to
    generate a A DNS record.
    """
795 796 797 798 799 800 801 802 803
    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):
804 805 806
    """Serialize `machines.models.Interface` objects with the data needed to
    generate a AAAA DNS record.
    """
807
    hostname = serializers.CharField(source='domain.name', read_only=True)
808
    ipv6 = Ipv6ListSerializer(many=True, read_only=True)
809 810 811 812 813 814 815

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


class CNAMERecordSerializer(serializers.ModelSerializer):
816 817 818
    """Serialize `machines.models.Domain` objects with the data needed to
    generate a CNAME DNS record.
    """
Charlie Jacomme's avatar
Charlie Jacomme committed
819
    alias = serializers.CharField(source='cname', read_only=True)
820 821 822 823
    hostname = serializers.CharField(source='name', read_only=True)

    class Meta:
        model = machines.Domain
Charlie Jacomme's avatar
Charlie Jacomme committed
824
        fields = ('alias', 'hostname')
825 826


827
class DNSZonesSerializer(serializers.ModelSerializer):
828 829
    """Serialize the data about DNS Zones.
    """
830 831 832
    soa = SOARecordSerializer()
    ns_records = NSRecordSerializer(many=True, source='ns_set')
    originv4 = OriginV4RecordSerializer(source='origin')
833
    originv6 = serializers.CharField(source='origin_v6')
834 835 836 837 838 839
    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')
840
    sshfp_records = SSHFPInterfaceSerializer(many=True, source='get_associated_sshfp_records')
841 842 843 844 845

    class Meta:
        model = machines.Extension
        fields = ('name', 'soa', 'ns_records', 'originv4', 'originv6',
                  'mx_records', 'txt_records', 'srv_records', 'a_records',
846
                  'aaaa_records', 'cname_records', 'sshfp_records')
847 848


849 850 851 852 853
class DNSReverseZonesSerializer(serializers.ModelSerializer):
    """Serialize the data about DNS Zones.
    """
    soa = SOARecordSerializer(source='extension.soa')
    extension = serializers.CharField(source='extension.name', read_only=True)
854
    cidrs = serializers.ListField(child=serializers.CharField(), source='ip_set_cidrs_as_str', read_only=True)
855 856 857 858 859 860 861 862 863 864
    ns_records = NSRecordSerializer(many=True, source='extension.ns_set')
    mx_records = MXRecordSerializer(many=True, source='extension.mx_set')
    txt_records = TXTRecordSerializer(many=True, source='extension.txt_set')
    ptr_records = ARecordSerializer(many=True, source='get_associated_ptr_records')
    ptr_v6_records = AAAARecordSerializer(many=True, source='get_associated_ptr_v6_records')


    class Meta:
        model = machines.IpType
        fields = ('type', 'extension', 'soa', 'ns_records', 'mx_records',
865
                  'txt_records', 'ptr_records', 'ptr_v6_records', 'cidrs',
866
                  'prefix_v6', 'prefix_v6_length')
867

868
# MAILING
869 870 871


class MailingMemberSerializer(UserSerializer):
872 873
    """Serialize the data about a mailing member.
    """
874
    class Meta(UserSerializer.Meta):
875
        fields = ('name', 'pseudo', 'get_mail')
876 877

class MailingSerializer(ClubSerializer):
878 879
    """Serialize the data about a mailing.
    """
880 881 882 883 884
    members = MailingMemberSerializer(many=True)
    admins = MailingMemberSerializer(source='administrators', many=True)

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