serializers.py 34 KB
Newer Older
1
# -*- mode: python; coding: utf-8 -*-
moamoak's avatar
moamoak 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.
#
moamoak's avatar
moamoak committed
6
# Copyright © 2018 Maël Kervella
moamoak's avatar
moamoak 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.

moamoak's avatar
moamoak committed
22
"""Defines the serializers of the API
moamoak's avatar
moamoak committed
23
"""
moamoak's avatar
moamoak committed
24 25

from rest_framework import serializers
moamoak's avatar
moamoak committed
26

moamoak's avatar
moamoak committed
27 28 29 30 31 32
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

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


class NamespacedHRField(serializers.HyperlinkedRelatedField):
moamoak's avatar
moamoak committed
39 40 41
    """A `rest_framework.serializers.HyperlinkedRelatedField` subclass to
    automatically prefix view names with the API namespace.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
42

moamoak's avatar
moamoak 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):
moamoak's avatar
moamoak committed
50 51 52
    """A `rest_framework.serializers.HyperlinkedIdentityField` subclass to
    automatically prefix view names with teh API namespace.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
53

moamoak's avatar
moamoak committed
54 55 56 57 58 59 60
    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):
moamoak's avatar
moamoak committed
61 62 63
    """A `rest_framework.serializers.HyperlinkedModelSerializer` subclass to
    automatically prefix view names with the API namespace.
    """
moamoak's avatar
moamoak committed
64 65
    serializer_related_field = NamespacedHRField
    serializer_url_field = NamespacedHIField
moamoak's avatar
moamoak committed
66

moamoak's avatar
moamoak committed
67

moamoak's avatar
moamoak committed
68
# COTISATIONS
moamoak's avatar
moamoak committed
69

moamoak's avatar
moamoak committed
70 71

class FactureSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
72 73
    """Serialize `cotisations.models.Facture` objects.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
74

moamoak's avatar
moamoak committed
75
    class Meta:
moamoak's avatar
moamoak committed
76
        model = cotisations.Facture
moamoak's avatar
moamoak committed
77 78 79 80
        fields = ('user', 'paiement', 'banque', 'cheque', 'date', 'valid',
                  'control', 'prix_total', 'name', 'api_url')


81 82 83 84 85
class BaseInvoiceSerializer(NamespacedHMSerializer):
    class Meta:
        model = cotisations.BaseInvoice
        fields = ('__all__')

moamoak's avatar
moamoak committed
86
class VenteSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
87 88
    """Serialize `cotisations.models.Vente` objects.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
89

moamoak's avatar
moamoak committed
90
    class Meta:
moamoak's avatar
moamoak committed
91
        model = cotisations.Vente
92 93
        fields = ('facture',
                  'number', 'name', 'prix', 'duration',
moamoak's avatar
moamoak committed
94 95 96
                  'type_cotisation', 'prix_total', 'api_url')


moamoak's avatar
moamoak committed
97
class ArticleSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
98 99
    """Serialize `cotisations.models.Article` objects.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
100

moamoak's avatar
moamoak committed
101
    class Meta:
moamoak's avatar
moamoak committed
102
        model = cotisations.Article
moamoak's avatar
moamoak committed
103 104 105 106
        fields = ('name', 'prix', 'duration', 'type_user',
                  'type_cotisation', 'api_url')


moamoak's avatar
moamoak committed
107
class BanqueSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
108 109
    """Serialize `cotisations.models.Banque` objects.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
110

moamoak's avatar
moamoak committed
111
    class Meta:
moamoak's avatar
moamoak committed
112
        model = cotisations.Banque
moamoak's avatar
moamoak committed
113 114 115
        fields = ('name', 'api_url')


moamoak's avatar
moamoak committed
116
class PaiementSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
117 118
    """Serialize `cotisations.models.Paiement` objects.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
119

moamoak's avatar
moamoak committed
120
    class Meta:
moamoak's avatar
moamoak committed
121
        model = cotisations.Paiement
122
        fields = ('moyen', 'api_url')
moamoak's avatar
moamoak committed
123 124


moamoak's avatar
moamoak committed
125
class CotisationSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
126 127
    """Serialize `cotisations.models.Cotisation` objects.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
128

moamoak's avatar
moamoak committed
129
    class Meta:
moamoak's avatar
moamoak committed
130
        model = cotisations.Cotisation
moamoak's avatar
moamoak committed
131 132 133 134
        fields = ('vente', 'type_cotisation', 'date_start', 'date_end',
                  'api_url')


moamoak's avatar
moamoak committed
135
# MACHINES
moamoak's avatar
moamoak committed
136 137


moamoak's avatar
moamoak committed
138
class MachineSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
139 140
    """Serialize `machines.models.Machine` objects.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
141

moamoak's avatar
moamoak committed
142
    class Meta:
moamoak's avatar
moamoak committed
143
        model = machines.Machine
moamoak's avatar
moamoak committed
144 145 146
        fields = ('user', 'name', 'active', 'api_url')


moamoak's avatar
moamoak committed
147
class MachineTypeSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
148 149
    """Serialize `machines.models.MachineType` objects.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
150

moamoak's avatar
moamoak committed
151
    class Meta:
moamoak's avatar
moamoak committed
152
        model = machines.MachineType
moamoak's avatar
moamoak committed
153 154 155
        fields = ('type', 'ip_type', 'api_url')


moamoak's avatar
moamoak committed
156
class IpTypeSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
157 158
    """Serialize `machines.models.IpType` objects.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
159

moamoak's avatar
moamoak committed
160
    class Meta:
moamoak's avatar
moamoak committed
161
        model = machines.IpType
moamoak's avatar
moamoak committed
162 163 164 165 166
        fields = ('type', 'extension', 'need_infra', 'domaine_ip_start',
                  'domaine_ip_stop', 'prefix_v6', 'vlan', 'ouverture_ports',
                  'api_url')


moamoak's avatar
moamoak committed
167
class VlanSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
168 169
    """Serialize `machines.models.Vlan` objects.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
170

moamoak's avatar
moamoak committed
171
    class Meta:
moamoak's avatar
moamoak committed
172
        model = machines.Vlan
173 174
        fields = ('vlan_id', 'name', 'comment', 'arp_protect', 'dhcp_snooping',
                  'dhcpv6_snooping', 'igmp', 'mld', 'api_url')
moamoak's avatar
moamoak committed
175 176


moamoak's avatar
moamoak committed
177
class NasSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
178 179
    """Serialize `machines.models.Nas` objects.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
180

moamoak's avatar
moamoak committed
181
    class Meta:
moamoak's avatar
moamoak committed
182
        model = machines.Nas
moamoak's avatar
moamoak committed
183 184 185 186
        fields = ('name', 'nas_type', 'machine_type', 'port_access_mode',
                  'autocapture_mac', 'api_url')


moamoak's avatar
moamoak committed
187
class SOASerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
188 189
    """Serialize `machines.models.SOA` objects.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
190

moamoak's avatar
moamoak committed
191
    class Meta:
moamoak's avatar
moamoak committed
192
        model = machines.SOA
moamoak's avatar
moamoak committed
193 194 195 196
        fields = ('name', 'mail', 'refresh', 'retry', 'expire', 'ttl',
                  'api_url')


moamoak's avatar
moamoak committed
197
class ExtensionSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
198 199
    """Serialize `machines.models.Extension` objects.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
200

moamoak's avatar
moamoak committed
201
    class Meta:
moamoak's avatar
moamoak committed
202
        model = machines.Extension
moamoak's avatar
moamoak committed
203 204 205 206
        fields = ('name', 'need_infra', 'origin', 'origin_v6', 'soa',
                  'api_url')


moamoak's avatar
moamoak committed
207
class MxSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
208 209
    """Serialize `machines.models.Mx` objects.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
210

moamoak's avatar
moamoak committed
211
    class Meta:
moamoak's avatar
moamoak committed
212
        model = machines.Mx
moamoak's avatar
moamoak committed
213 214 215
        fields = ('zone', 'priority', 'name', 'api_url')


jacomme's avatar
jacomme committed
216 217 218
class DNameSerializer(NamespacedHMSerializer):
    """Serialize `machines.models.DName` objects.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
219

jacomme's avatar
jacomme committed
220 221 222 223
    class Meta:
        model = machines.DName
        fields = ('zone', 'alias', 'api_url')

Alexandre IOOSS's avatar
Alexandre IOOSS committed
224

moamoak's avatar
moamoak committed
225
class NsSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
226 227
    """Serialize `machines.models.Ns` objects.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
228

moamoak's avatar
moamoak committed
229
    class Meta:
moamoak's avatar
moamoak committed
230
        model = machines.Ns
moamoak's avatar
moamoak committed
231 232 233
        fields = ('zone', 'ns', 'api_url')


moamoak's avatar
moamoak committed
234
class TxtSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
235 236
    """Serialize `machines.models.Txt` objects.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
237

moamoak's avatar
moamoak committed
238
    class Meta:
moamoak's avatar
moamoak committed
239
        model = machines.Txt
moamoak's avatar
moamoak committed
240 241 242
        fields = ('zone', 'field1', 'field2', 'api_url')


moamoak's avatar
moamoak committed
243
class SrvSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
244 245
    """Serialize `machines.models.Srv` objects.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
246

moamoak's avatar
moamoak committed
247
    class Meta:
moamoak's avatar
moamoak committed
248
        model = machines.Srv
moamoak's avatar
moamoak committed
249 250 251
        fields = ('service', 'protocole', 'extension', 'ttl', 'priority',
                  'weight', 'port', 'target', 'api_url')

Alexandre IOOSS's avatar
Alexandre IOOSS committed
252

253 254 255
class SshFpSerializer(NamespacedHMSerializer):
    """Serialize `machines.models.SSHFP` objects.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
256

257 258 259 260
    class Meta:
        model = machines.SshFp
        field = ('machine', 'pub_key_entry', 'algo', 'comment', 'api_url')

moamoak's avatar
moamoak committed
261

moamoak's avatar
moamoak committed
262
class InterfaceSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
263 264
    """Serialize `machines.models.Interface` objects.
    """
moamoak's avatar
moamoak committed
265
    mac_address = serializers.CharField()
moamoak's avatar
moamoak committed
266 267 268
    active = serializers.BooleanField(source='is_active')

    class Meta:
moamoak's avatar
moamoak committed
269
        model = machines.Interface
moamoak's avatar
moamoak committed
270 271 272 273
        fields = ('ipv4', 'mac_address', 'machine', 'type', 'details',
                  'port_lists', 'active', 'api_url')


moamoak's avatar
moamoak committed
274
class Ipv6ListSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
275 276
    """Serialize `machines.models.Ipv6List` objects.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
277

moamoak's avatar
moamoak committed
278
    class Meta:
moamoak's avatar
moamoak committed
279
        model = machines.Ipv6List
moamoak's avatar
moamoak committed
280
        fields = ('ipv6', 'interface', 'slaac_ip', 'api_url')
moamoak's avatar
moamoak committed
281 282


moamoak's avatar
moamoak committed
283
class DomainSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
284 285
    """Serialize `machines.models.Domain` objects.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
286

moamoak's avatar
moamoak committed
287
    class Meta:
moamoak's avatar
moamoak committed
288
        model = machines.Domain
moamoak's avatar
moamoak committed
289 290 291 292
        fields = ('interface_parent', 'name', 'extension', 'cname',
                  'api_url')


moamoak's avatar
moamoak committed
293
class IpListSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
294 295
    """Serialize `machines.models.IpList` objects.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
296

moamoak's avatar
moamoak committed
297
    class Meta:
moamoak's avatar
moamoak committed
298
        model = machines.IpList
moamoak's avatar
moamoak committed
299 300 301
        fields = ('ipv4', 'ip_type', 'need_infra', 'api_url')


moamoak's avatar
moamoak committed
302
class ServiceSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
303 304
    """Serialize `machines.models.Service` objects.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
305

moamoak's avatar
moamoak committed
306
    class Meta:
moamoak's avatar
moamoak committed
307
        model = machines.Service
moamoak's avatar
moamoak committed
308 309 310 311
        fields = ('service_type', 'min_time_regen', 'regular_time_regen',
                  'servers', 'api_url')


moamoak's avatar
moamoak committed
312
class ServiceLinkSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
313 314
    """Serialize `machines.models.Service_link` objects.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
315

moamoak's avatar
moamoak committed
316
    class Meta:
moamoak's avatar
moamoak committed
317
        model = machines.Service_link
moamoak's avatar
moamoak committed
318 319
        fields = ('service', 'server', 'last_regen', 'asked_regen',
                  'need_regen', 'api_url')
moamoak's avatar
moamoak committed
320 321 322
        extra_kwargs = {
            'api_url': {'view_name': 'servicelink-detail'}
        }
moamoak's avatar
moamoak committed
323 324


moamoak's avatar
moamoak committed
325
class OuverturePortListSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
326 327
    """Serialize `machines.models.OuverturePortList` objects.
    """
328 329 330 331 332
    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)

moamoak's avatar
moamoak committed
333
    class Meta:
moamoak's avatar
moamoak committed
334
        model = machines.OuverturePortList
moamoak's avatar
moamoak committed
335 336 337 338
        fields = ('name', 'tcp_ports_in', 'udp_ports_in', 'tcp_ports_out',
                  'udp_ports_out', 'api_url')


moamoak's avatar
moamoak committed
339
class OuverturePortSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
340 341
    """Serialize `machines.models.OuverturePort` objects.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
342

moamoak's avatar
moamoak committed
343
    class Meta:
moamoak's avatar
moamoak committed
344
        model = machines.OuverturePort
moamoak's avatar
moamoak committed
345 346 347
        fields = ('begin', 'end', 'port_list', 'protocole', 'io', 'api_url')


348 349 350 351 352 353 354 355 356 357
class RoleSerializer(NamespacedHMSerializer):
    """Serialize `machines.models.OuverturePort` objects.
    """
    servers = InterfaceSerializer(read_only=True, many=True)

    class Meta:
        model = machines.Role
        fields = ('role_type', 'servers', 'api_url')


moamoak's avatar
moamoak committed
358
# PREFERENCES
moamoak's avatar
moamoak committed
359 360


moamoak's avatar
moamoak committed
361
class OptionalUserSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
362 363
    """Serialize `preferences.models.OptionalUser` objects.
    """
moamoak's avatar
moamoak committed
364
    tel_mandatory = serializers.BooleanField(source='is_tel_mandatory')
365
    shell_default = serializers.StringRelatedField()
moamoak's avatar
moamoak committed
366 367 368

    class Meta:
        model = preferences.OptionalUser
369
        fields = ('tel_mandatory', 'gpg_fingerprint',
370 371 372 373
                  'all_can_create_club', 'self_adhesion', 'shell_default',
                  'self_change_shell', 'local_email_accounts_enabled', 'local_email_domain',
                  'max_email_address',
        )
moamoak's avatar
moamoak committed
374 375 376


class OptionalMachineSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
377 378
    """Serialize `preferences.models.OptionalMachine` objects.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
379

moamoak's avatar
moamoak committed
380 381 382 383 384 385 386 387
    class Meta:
        model = preferences.OptionalMachine
        fields = ('password_machine', 'max_lambdauser_interfaces',
                  'max_lambdauser_aliases', 'ipv6_mode', 'create_machine',
                  'ipv6')


class OptionalTopologieSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
388 389
    """Serialize `preferences.models.OptionalTopologie` objects.
    """
detraz's avatar
detraz committed
390
    switchs_management_interface_ip = serializers.CharField()
391

moamoak's avatar
moamoak committed
392 393
    class Meta:
        model = preferences.OptionalTopologie
394
        fields = ('switchs_ip_type', 'switchs_web_management',
Gabriel Detraz's avatar
Gabriel Detraz committed
395 396 397
                  'switchs_web_management_ssl', 'switchs_rest_management',
                  'switchs_management_utils', 'switchs_management_interface_ip',
                  'provision_switchs_enabled', 'switchs_provision', 'switchs_management_sftp_creds')
moamoak's avatar
moamoak committed
398 399


400 401 402 403 404 405 406 407 408 409 410 411 412
class RadiusOptionSerializer(NamespacedHMSerializer):
    """Serialize `preferences.models.RadiusOption` objects
    """

    class Meta:
        model = preferences.RadiusOption
        fields = ('radius_general_policy', 'unknown_machine',
                'unknown_machine_vlan', 'unknown_port',
                'unknown_port_vlan', 'unknown_room', 'unknown_room_vlan',
                'non_member', 'non_member_vlan', 'banned', 'banned_vlan',
                'vlan_decision_ok')


moamoak's avatar
moamoak committed
413
class GeneralOptionSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
414 415
    """Serialize `preferences.models.GeneralOption` objects.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
416

moamoak's avatar
moamoak committed
417 418
    class Meta:
        model = preferences.GeneralOption
419 420 421
        fields = ('general_message_fr', 'general_message_en',
                  'search_display_page', 'pagination_number',
                  'pagination_large_number', 'req_expire_hrs',
422 423
                  'site_name', 'main_site_url', 'email_from',
                  'GTU_sum_up', 'GTU')
moamoak's avatar
moamoak committed
424

425
class HomeServiceSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
426 427
    """Serialize `preferences.models.Service` objects.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
428

moamoak's avatar
moamoak committed
429 430 431
    class Meta:
        model = preferences.Service
        fields = ('name', 'url', 'description', 'image', 'api_url')
432 433 434
        extra_kwargs = {
            'api_url': {'view_name': 'homeservice-detail'}
        }
moamoak's avatar
moamoak committed
435 436 437


class AssoOptionSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
438 439
    """Serialize `preferences.models.AssoOption` objects.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
440

moamoak's avatar
moamoak committed
441 442 443
    class Meta:
        model = preferences.AssoOption
        fields = ('name', 'siret', 'adresse1', 'adresse2', 'contact',
444
                  'telephone', 'pseudo', 'utilisateur_asso', 'description')
moamoak's avatar
moamoak committed
445 446 447


class HomeOptionSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
448 449
    """Serialize `preferences.models.HomeOption` objects.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
450

moamoak's avatar
moamoak committed
451 452 453 454 455 456
    class Meta:
        model = preferences.HomeOption
        fields = ('facebook_url', 'twitter_url', 'twitter_account_name')


class MailMessageOptionSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
457 458
    """Serialize `preferences.models.MailMessageOption` objects.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
459

moamoak's avatar
moamoak committed
460 461 462
    class Meta:
        model = preferences.MailMessageOption
        fields = ('welcome_mail_fr', 'welcome_mail_en')
moamoak's avatar
moamoak committed
463 464


moamoak's avatar
moamoak committed
465
# TOPOLOGIE
moamoak's avatar
moamoak committed
466 467


moamoak's avatar
moamoak committed
468
class StackSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
469 470
    """Serialize `topologie.models.Stack` objects
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
471

moamoak's avatar
moamoak committed
472
    class Meta:
moamoak's avatar
moamoak committed
473
        model = topologie.Stack
moamoak's avatar
moamoak committed
474 475 476 477
        fields = ('name', 'stack_id', 'details', 'member_id_min',
                  'member_id_max', 'api_url')


moamoak's avatar
moamoak committed
478
class AccessPointSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
479 480
    """Serialize `topologie.models.AccessPoint` objects
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
481

moamoak's avatar
moamoak committed
482
    class Meta:
moamoak's avatar
moamoak committed
483
        model = topologie.AccessPoint
moamoak's avatar
moamoak committed
484 485 486
        fields = ('user', 'name', 'active', 'location', 'api_url')


moamoak's avatar
moamoak committed
487
class SwitchSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
488 489
    """Serialize `topologie.models.Switch` objects
    """
moamoak's avatar
moamoak committed
490
    port_amount = serializers.IntegerField(source='number')
Alexandre IOOSS's avatar
Alexandre IOOSS committed
491

moamoak's avatar
moamoak committed
492
    class Meta:
moamoak's avatar
moamoak committed
493
        model = topologie.Switch
moamoak's avatar
moamoak committed
494 495
        fields = ('user', 'name', 'active', 'port_amount', 'stack',
                  'stack_member_id', 'model', 'switchbay', 'api_url')
moamoak's avatar
moamoak committed
496 497


498 499 500
class ServerSerializer(NamespacedHMSerializer):
    """Serialize `topologie.models.Server` objects
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
501

502 503 504 505 506
    class Meta:
        model = topologie.Server
        fields = ('user', 'name', 'active', 'api_url')


moamoak's avatar
moamoak committed
507
class ModelSwitchSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
508 509
    """Serialize `topologie.models.ModelSwitch` objects
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
510

moamoak's avatar
moamoak committed
511
    class Meta:
moamoak's avatar
moamoak committed
512
        model = topologie.ModelSwitch
moamoak's avatar
moamoak committed
513 514 515
        fields = ('reference', 'constructor', 'api_url')


moamoak's avatar
moamoak committed
516
class ConstructorSwitchSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
517 518
    """Serialize `topologie.models.ConstructorSwitch` objects
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
519

moamoak's avatar
moamoak committed
520
    class Meta:
moamoak's avatar
moamoak committed
521
        model = topologie.ConstructorSwitch
moamoak's avatar
moamoak committed
522 523 524
        fields = ('name', 'api_url')


moamoak's avatar
moamoak committed
525
class SwitchBaySerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
526 527
    """Serialize `topologie.models.SwitchBay` objects
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
528

moamoak's avatar
moamoak committed
529
    class Meta:
moamoak's avatar
moamoak committed
530
        model = topologie.SwitchBay
moamoak's avatar
moamoak committed
531 532 533
        fields = ('name', 'building', 'info', 'api_url')


moamoak's avatar
moamoak committed
534
class BuildingSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
535 536
    """Serialize `topologie.models.Building` objects
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
537

moamoak's avatar
moamoak committed
538
    class Meta:
moamoak's avatar
moamoak committed
539
        model = topologie.Building
moamoak's avatar
moamoak committed
540 541 542
        fields = ('name', 'api_url')


moamoak's avatar
moamoak committed
543
class SwitchPortSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
544 545
    """Serialize `topologie.models.Port` objects
    """
546

547
    get_port_profile = NamespacedHIField(view_name='portprofile-detail', read_only=True)
548

moamoak's avatar
moamoak committed
549
    class Meta:
moamoak's avatar
moamoak committed
550
        model = topologie.Port
moamoak's avatar
moamoak committed
551
        fields = ('switch', 'port', 'room', 'machine_interface', 'related',
552
                  'custom_profile', 'state', 'get_port_profile', 'details', 'api_url')
moamoak's avatar
moamoak committed
553 554
        extra_kwargs = {
            'related': {'view_name': 'switchport-detail'},
555
            'api_url': {'view_name': 'switchport-detail'},
moamoak's avatar
moamoak committed
556
        }
moamoak's avatar
moamoak committed
557 558


559 560 561 562 563
class PortProfileSerializer(NamespacedHMSerializer):
    """Serialize `topologie.models.Room` objects
    """
    class Meta:
        model = topologie.PortProfile
Alexandre IOOSS's avatar
Alexandre IOOSS committed
564
        fields = ('name', 'profil_default', 'vlan_untagged', 'vlan_tagged',
565 566 567 568 569
                  'radius_type', 'radius_mode', 'speed', 'mac_limit', 'flow_control',
                  'dhcp_snooping', 'dhcpv6_snooping', 'dhcpv6_snooping', 'arp_protect',
                  'ra_guard', 'loop_protect', 'api_url')


moamoak's avatar
moamoak committed
570
class RoomSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
571 572
    """Serialize `topologie.models.Room` objects
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
573

moamoak's avatar
moamoak committed
574
    class Meta:
moamoak's avatar
moamoak committed
575
        model = topologie.Room
moamoak's avatar
moamoak committed
576 577 578
        fields = ('name', 'details', 'api_url')


579 580 581 582 583 584 585 586 587 588 589 590
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')


moamoak's avatar
moamoak committed
591
# USERS
moamoak's avatar
moamoak committed
592

moamoak's avatar
moamoak committed
593

moamoak's avatar
moamoak committed
594
class UserSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
595 596
    """Serialize `users.models.User` objects.
    """
moamoak's avatar
moamoak committed
597 598
    access = serializers.BooleanField(source='has_access')
    uid = serializers.IntegerField(source='uid_number')
moamoak's avatar
moamoak committed
599 600

    class Meta:
moamoak's avatar
moamoak committed
601
        model = users.User
moamoak's avatar
moamoak committed
602 603
        fields = ('surname', 'pseudo', 'email', 'local_email_redirect',
                  'local_email_enabled', 'school', 'shell', 'comment',
moamoak's avatar
moamoak committed
604 605 606 607 608
                  'state', 'registered', 'telephone', 'solde', 'access',
                  'end_access', 'uid', 'class_name', 'api_url')
        extra_kwargs = {
            'shell': {'view_name': 'shell-detail'}
        }
moamoak's avatar
moamoak committed
609

moamoak's avatar
moamoak committed
610

moamoak's avatar
moamoak committed
611
class ClubSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
612 613
    """Serialize `users.models.Club` objects.
    """
moamoak's avatar
moamoak committed
614 615 616
    name = serializers.CharField(source='surname')
    access = serializers.BooleanField(source='has_access')
    uid = serializers.IntegerField(source='uid_number')
moamoak's avatar
moamoak committed
617 618

    class Meta:
moamoak's avatar
moamoak committed
619
        model = users.Club
moamoak's avatar
moamoak committed
620 621
        fields = ('name', 'pseudo', 'email', 'local_email_redirect',
                  'local_email_enabled', 'school', 'shell', 'comment',
moamoak's avatar
moamoak committed
622
                  'state', 'registered', 'telephone', 'solde', 'room',
moamoak's avatar
moamoak committed
623 624
                  'access', 'end_access', 'administrators', 'members',
                  'mailing', 'uid', 'api_url')
moamoak's avatar
moamoak committed
625 626 627
        extra_kwargs = {
            'shell': {'view_name': 'shell-detail'}
        }
moamoak's avatar
moamoak committed
628 629


moamoak's avatar
moamoak committed
630
class AdherentSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
631 632
    """Serialize `users.models.Adherent` objects.
    """
moamoak's avatar
moamoak committed
633 634
    access = serializers.BooleanField(source='has_access')
    uid = serializers.IntegerField(source='uid_number')
moamoak's avatar
moamoak committed
635

moamoak's avatar
moamoak committed
636
    class Meta:
moamoak's avatar
moamoak committed
637
        model = users.Adherent
moamoak's avatar
moamoak committed
638 639 640
        fields = ('name', 'surname', 'pseudo', 'email', 'local_email_redirect',
                  'local_email_enabled', 'school', 'shell', 'comment',
                  'state', 'registered', 'telephone', 'room', 'solde',
Alexandre IOOSS's avatar
Alexandre IOOSS committed
641
                  'access', 'end_access', 'uid', 'api_url', 'gid')
moamoak's avatar
moamoak committed
642 643 644
        extra_kwargs = {
            'shell': {'view_name': 'shell-detail'}
        }
moamoak's avatar
moamoak committed
645

Alexandre IOOSS's avatar
Alexandre IOOSS committed
646

detraz's avatar
detraz committed
647 648
class BasicUserSerializer(NamespacedHMSerializer):
    """Serialize 'users.models.User' minimal infos"""
649 650 651 652 653 654
    uid = serializers.IntegerField(source='uid_number')
    gid = serializers.IntegerField(source='gid_number')

    class Meta:
        model = users.User
        fields = ('pseudo', 'uid', 'gid')
moamoak's avatar
moamoak committed
655

Alexandre IOOSS's avatar
Alexandre IOOSS committed
656

moamoak's avatar
moamoak committed
657
class ServiceUserSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
658 659
    """Serialize `users.models.ServiceUser` objects.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
660

moamoak's avatar
moamoak committed
661
    class Meta:
moamoak's avatar
moamoak committed
662
        model = users.ServiceUser
moamoak's avatar
moamoak committed
663
        fields = ('pseudo', 'access_group', 'comment', 'api_url')
moamoak's avatar
moamoak committed
664 665


moamoak's avatar
moamoak committed
666
class SchoolSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
667 668
    """Serialize `users.models.School` objects.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
669

moamoak's avatar
moamoak committed
670
    class Meta:
moamoak's avatar
moamoak committed
671
        model = users.School
moamoak's avatar
moamoak committed
672
        fields = ('name', 'api_url')
moamoak's avatar
moamoak committed
673 674


moamoak's avatar
moamoak committed
675
class ListRightSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
676 677
    """Serialize `users.models.ListRight` objects.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
678

moamoak's avatar
moamoak committed
679
    class Meta:
moamoak's avatar
moamoak committed
680
        model = users.ListRight
moamoak's avatar
moamoak committed
681
        fields = ('unix_name', 'gid', 'critical', 'details', 'api_url')
moamoak's avatar
moamoak committed
682 683


moamoak's avatar
moamoak committed
684
class ShellSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
685 686
    """Serialize `users.models.ListShell` objects.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
687

moamoak's avatar
moamoak committed
688
    class Meta:
moamoak's avatar
moamoak committed
689
        model = users.ListShell
moamoak's avatar
moamoak committed
690
        fields = ('shell', 'api_url')
moamoak's avatar
moamoak committed
691 692 693
        extra_kwargs = {
            'api_url': {'view_name': 'shell-detail'}
        }
moamoak's avatar
moamoak committed
694 695


moamoak's avatar
moamoak committed
696
class BanSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
697 698
    """Serialize `users.models.Ban` objects.
    """
moamoak's avatar
moamoak committed
699
    active = serializers.BooleanField(source='is_active')
moamoak's avatar
moamoak committed
700 701

    class Meta:
moamoak's avatar
moamoak committed
702
        model = users.Ban
moamoak's avatar
moamoak committed
703 704
        fields = ('user', 'raison', 'date_start', 'date_end', 'state',
                  'active', 'api_url')
moamoak's avatar
moamoak committed
705 706


moamoak's avatar
moamoak committed
707
class WhitelistSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
708 709
    """Serialize `users.models.Whitelist` objects.
    """
moamoak's avatar
moamoak committed
710
    active = serializers.BooleanField(source='is_active')
moamoak's avatar
moamoak committed
711 712

    class Meta:
moamoak's avatar
moamoak committed
713
        model = users.Whitelist
moamoak's avatar
moamoak committed
714
        fields = ('user', 'raison', 'date_start', 'date_end', 'active', 'api_url')
moamoak's avatar
moamoak committed
715 716


717 718
class EMailAddressSerializer(NamespacedHMSerializer):
    """Serialize `users.models.EMailAddress` objects.
719
    """
jacomme's avatar
jacomme committed
720
    user = serializers.CharField(source='user.pseudo', read_only=True)
Alexandre IOOSS's avatar
Alexandre IOOSS committed
721

722
    class Meta:
723
        model = users.EMailAddress
moamoak's avatar
moamoak committed
724
        fields = ('user', 'local_part', 'complete_email_address', 'api_url')
725 726


moamoak's avatar
moamoak committed
727
# SERVICE REGEN
728 729 730


class ServiceRegenSerializer(NamespacedHMSerializer):
moamoak's avatar
moamoak committed
731 732
    """Serialize the data about the services to regen.
    """
733 734
    hostname = serializers.CharField(source='server.domain.name', read_only=True)
    service_name = serializers.CharField(source='service.service_type', read_only=True)
735
    need_regen = serializers.BooleanField()
736 737 738 739 740

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

744 745
# Switches et ports

746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
class InterfaceVlanSerializer(NamespacedHMSerializer):
    domain = serializers.CharField(read_only=True)
    ipv4 = serializers.CharField(read_only=True)
    ipv6 = Ipv6ListSerializer(read_only=True, many=True)
    vlan_id = serializers.IntegerField(source='type.ip_type.vlan.vlan_id', read_only=True)

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

class InterfaceRoleSerializer(NamespacedHMSerializer):
    interface = InterfaceVlanSerializer(source='machine.interface_set', read_only=True, many=True)

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


class RoleSerializer(NamespacedHMSerializer):
    """Serialize `machines.models.OuverturePort` objects.
    """
    servers = InterfaceRoleSerializer(read_only=True, many=True)

    class Meta:
        model = machines.Role
771
        fields = ('role_type', 'servers', 'specific_role')
772 773


774 775 776
class VlanPortSerializer(NamespacedHMSerializer):
    class Meta:
        model = machines.Vlan
Alexandre IOOSS's avatar
Alexandre IOOSS committed
777
        fields = ('vlan_id', 'name')
778 779


780
class ProfilSerializer(NamespacedHMSerializer):
781 782 783
    vlan_untagged = VlanSerializer(read_only=True)
    vlan_tagged = VlanPortSerializer(read_only=True, many=True)

784 785
    class Meta:
        model = topologie.PortProfile
786
        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')
787 788 789


class ModelSwitchSerializer(NamespacedHMSerializer):
790 791
    constructor = serializers.CharField(read_only=True)

792 793
    class Meta:
        model = topologie.ModelSwitch
794
        fields = ('reference', 'firmware', 'constructor')
795 796 797 798 799 800 801 802 803 804 805


class SwitchBaySerializer(NamespacedHMSerializer):
    class Meta:
        model = topologie.SwitchBay
        fields = ('name',)


class PortsSerializer(NamespacedHMSerializer):
    """Serialize `machines.models.Ipv6List` objects.
    """
806
    get_port_profile = ProfilSerializer(read_only=True)
807

808

809 810
    class Meta:
        model = topologie.Port
811
        fields = ('state', 'port', 'pretty_name', 'get_port_profile')
812 813


814

815 816 817 818 819 820
class SwitchPortSerializer(serializers.ModelSerializer):
    """Serialize the data about the switches"""
    ports = PortsSerializer(many=True, read_only=True)
    model = ModelSwitchSerializer(read_only=True)
    switchbay = SwitchBaySerializer(read_only=True)

821

822 823
    class Meta:
        model = topologie.Switch
824
        fields = ('short_name', 'model', 'switchbay', 'ports', 'ipv4', 'ipv6',
825
                  'interfaces_subnet', 'interfaces6_subnet', 'automatic_provision', 'rest_enabled',
826 827
                  'web_management_enabled', 'get_radius_key_value', 'get_management_cred_value',
                  'list_modules')
828

moamoak's avatar
moamoak committed
829 830 831 832
# LOCAL EMAILS


class LocalEmailUsersSerializer(NamespacedHMSerializer):
833
    email_address = EMailAddressSerializer(
moamoak's avatar
moamoak committed
834 835 836
        read_only=True,
        many=True
    )
837 838 839

    class Meta:
        model = users.User
moamoak's avatar
moamoak committed
840
        fields = ('local_email_enabled', 'local_email_redirect',
jacomme's avatar
jacomme committed
841
                  'email_address', 'email')
842 843


Alexandre IOOSS's avatar
Alexandre IOOSS committed
844
# Firewall
845 846 847 848

class FirewallPortListSerializer(serializers.ModelSerializer):
    class Meta:
        model = machines.OuverturePort
849
        fields = ('begin', 'end', 'protocole', 'io', 'show_port')
850

Alexandre IOOSS's avatar
Alexandre IOOSS committed
851

852 853 854 855 856 857 858 859 860 861
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')

Alexandre IOOSS's avatar
Alexandre IOOSS committed
862

863 864 865 866 867
class SubnetPortsOpenSerializer(serializers.ModelSerializer):
    ouverture_ports = FirewallOuverturePortListSerializer(read_only=True)

    class Meta:
        model = machines.IpType
Gabriel Detraz's avatar
Gabriel Detraz committed
868
        fields = ('type', 'domaine_ip_start', 'domaine_ip_stop', 'complete_prefixv6', 'ouverture_ports')
869

Alexandre IOOSS's avatar
Alexandre IOOSS committed
870

871 872 873 874 875 876 877 878 879
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')

Alexandre IOOSS's avatar
Alexandre IOOSS committed
880

moamoak's avatar
moamoak committed
881 882 883 884
# DHCP


class HostMacIpSerializer(serializers.ModelSerializer):
moamoak's avatar
moamoak committed
885 886 887
    """Serialize the data about the hostname-ipv4-mac address association
    to build the DHCP lease files.
    """
moamoak's avatar
moamoak committed
888 889
    hostname = serializers.CharField(source='domain.name', read_only=True)
    extension = serializers.CharField(source='domain.extension.name', read_only=True)
moamoak's avatar
moamoak committed
890
    mac_address = serializers.CharField(read_only=True)
moamoak's avatar
moamoak committed
891 892 893 894 895
    ipv4 = serializers.CharField(source='ipv4.ipv4', read_only=True)

    class Meta:
        model = machines.Interface
        fields = ('hostname', 'extension', 'mac_address', 'ipv4')
moamoak's avatar
moamoak committed
896 897 898 899 900 901


# DNS


class SOARecordSerializer(SOASerializer):
moamoak's avatar
moamoak committed
902 903 904
    """Serialize `machines.models.SOA` objects with the data needed to
    generate a SOA DNS record.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
905

moamoak's avatar
moamoak committed
906 907 908 909 910 911
    class Meta:
        model = machines.SOA
        fields = ('name', 'mail', 'refresh', 'retry', 'expire', 'ttl')


class OriginV4RecordSerializer(IpListSerializer):
moamoak's avatar
moamoak committed
912 913 914
    """Serialize `machines.models.IpList` objects with the data needed to
    generate an IPv4 Origin DNS record.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
915

moamoak's avatar
moamoak committed
916 917 918 919 920
    class Meta(IpListSerializer.Meta):
        fields = ('ipv4',)


class NSRecordSerializer(NsSerializer):
moamoak's avatar
moamoak committed
921 922 923
    """Serialize `machines.models.Ns` objects with the data needed to
    generate a NS DNS record.
    """
jacomme's avatar
jacomme committed
924
    target = serializers.CharField(source='ns', read_only=True)
moamoak's avatar
moamoak committed
925 926 927 928 929 930

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


class MXRecordSerializer(MxSerializer):
moamoak's avatar
moamoak committed
931 932 933
    """Serialize `machines.models.Mx` objects with the data needed to
    generate a MX DNS record.
    """
jacomme's avatar
jacomme committed
934
    target = serializers.CharField(source='name', read_only=True)
moamoak's avatar
moamoak committed
935 936 937 938 939 940

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


class TXTRecordSerializer(TxtSerializer):
moamoak's avatar
moamoak committed
941 942 943
    """Serialize `machines.models.Txt` objects with the data needed to
    generate a TXT DNS record.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
944

moamoak's avatar
moamoak committed
945 946 947 948 949
    class Meta(TxtSerializer.Meta):
        fields = ('field1', 'field2')


class SRVRecordSerializer(SrvSerializer):
moamoak's avatar
moamoak committed
950 951 952
    """Serialize `machines.models.Srv` objects with the data needed to
    generate a SRV DNS record.
    """
moamoak's avatar
moamoak committed
953 954 955 956 957 958
    target = serializers.CharField(source='target.name', read_only=True)

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


959 960 961 962
class SSHFPRecordSerializer(SshFpSerializer):
    """Serialize `machines.models.SshFp` objects with the data needed to
    generate a SSHFP DNS record.
    """
Alexandre IOOSS's avatar
Alexandre IOOSS committed
963

964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
    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')


moamoak's avatar
moamoak committed
980
class ARecordSerializer(serializers.ModelSerializer):
moamoak's avatar
moamoak committed
981 982 983
    """Serialize `machines.models.Interface` objects with the data needed to
    generate a A DNS record.
    """
moamoak's avatar
moamoak committed
984 985 986 987 988 989 990 991 992
    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):
moamoak's avatar
moamoak committed
993 994 995
    """Serialize `machines.models.Interface` objects with the data needed to
    generate a AAAA DNS record.
    """
moamoak's avatar
moamoak committed
996
    hostname = serializers.CharField(source='domain.name', read_only=True)
997
    ipv6 = Ipv6ListSerializer(many=True, read_only=True)
moamoak's avatar
moamoak committed
998 999 1000 1001 1002 1003 1004

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


class CNAMERecordSerializer(serializers.ModelSerializer):
moamoak's avatar
moamoak committed
1005 1006 1007
    """Serialize `machines.models.Domain` objects with the data needed to
    generate a CNAME DNS record.
    """
jacomme's avatar
jacomme committed
1008
    alias = serializers.CharField(source='cname', read_only=True)
moamoak's avatar
moamoak committed
1009 1010 1011 1012
    hostname = serializers.CharField(source='name', read_only=True)

    class Meta:
        model = machines.Domain
jacomme's avatar
jacomme committed
1013
        fields = ('alias', 'hostname')
moamoak's avatar
moamoak committed
1014

Gabriel Detraz's avatar
Gabriel Detraz committed
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
class DNAMERecordSerializer(serializers.ModelSerializer):
    """Serialize `machines.models.Domain` objects with the data needed to
    generate a DNAME DNS record.
    """
    alias = serializers.CharField(read_only=True)
    zone = serializers.CharField(read_only=True)

    class Meta:
        model = machines.DName
        fields = ('alias', 'zone')

moamoak's avatar
moamoak committed
1026

1027
class DNSZonesSerializer(serializers.ModelSerializer):
moamoak's avatar
moamoak committed
1028 1029
    """Serialize the data about DNS Zones.
    """
moamoak's avatar
moamoak committed
1030 1031 1032
    soa = SOARecordSerializer()
    ns_records = NSRecordSerializer(many=True, source='ns_set')
    originv4 = OriginV4RecordSerializer(source='origin')
1033
    originv6 = serializers.CharField(source='origin_v6')
moamoak's avatar
moamoak committed
1034 1035 1036 1037 1038 1039
    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'