serializers.py 22.7 KB
Newer Older
Maël Kervella's avatar
Maël Kervella committed
1 2 3 4
# 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.
#
5
# Copyright © 2018 Maël Kervella
Maël Kervella's avatar
Maël Kervella committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#
# 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.

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

from rest_framework import serializers
25

Maël Kervella's avatar
Maël Kervella committed
26 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


33 34
# 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
35 36 37 38
API_NAMESPACE = 'api'


class NamespacedHRField(serializers.HyperlinkedRelatedField):
39 40 41
    """A `rest_framework.serializers.HyperlinkedRelatedField` subclass to
    automatically prefix view names with the API namespace.
    """
Maël Kervella's avatar
Maël Kervella committed
42 43 44 45 46 47 48
    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):
49 50 51
    """A `rest_framework.serializers.HyperlinkedIdentityField` subclass to
    automatically prefix view names with teh API namespace.
    """
Maël Kervella's avatar
Maël Kervella committed
52 53 54 55 56 57 58
    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):
59 60 61
    """A `rest_framework.serializers.HyperlinkedModelSerializer` subclass to
    automatically prefix view names with the API namespace.
    """
Maël Kervella's avatar
Maël Kervella committed
62 63
    serializer_related_field = NamespacedHRField
    serializer_url_field = NamespacedHIField
Maël Kervella's avatar
Maël Kervella committed
64

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

66
# COTISATIONS
67

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

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


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


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


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


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


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


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


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


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


Maël Kervella's avatar
Maël Kervella committed
140
class IpTypeSerializer(NamespacedHMSerializer):
141 142
    """Serialize `machines.models.IpType` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
143
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
144
        model = machines.IpType
Maël Kervella's avatar
Maël Kervella committed
145 146 147 148 149
        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
150
class VlanSerializer(NamespacedHMSerializer):
151 152
    """Serialize `machines.models.Vlan` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
153
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
154
        model = machines.Vlan
Maël Kervella's avatar
Maël Kervella committed
155 156 157
        fields = ('vlan_id', 'name', 'comment', 'api_url')


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


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


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


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


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


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


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


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

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


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


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


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


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


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


Maël Kervella's avatar
Maël Kervella committed
276
class OuverturePortListSerializer(NamespacedHMSerializer):
277 278
    """Serialize `machines.models.OuverturePortList` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
279
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
280
        model = machines.OuverturePortList
Maël Kervella's avatar
Maël Kervella committed
281 282 283 284
        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
285
class OuverturePortSerializer(NamespacedHMSerializer):
286 287
    """Serialize `machines.models.OuverturePort` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
288
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
289
        model = machines.OuverturePort
Maël Kervella's avatar
Maël Kervella committed
290 291 292
        fields = ('begin', 'end', 'port_list', 'protocole', 'io', 'api_url')


293
# PREFERENCES
294 295


Maël Kervella's avatar
Maël Kervella committed
296
class OptionalUserSerializer(NamespacedHMSerializer):
297 298
    """Serialize `preferences.models.OptionalUser` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
299 300 301 302 303 304 305 306 307 308
    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):
309 310
    """Serialize `preferences.models.OptionalMachine` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
311 312 313 314 315 316 317 318
    class Meta:
        model = preferences.OptionalMachine
        fields = ('password_machine', 'max_lambdauser_interfaces',
                  'max_lambdauser_aliases', 'ipv6_mode', 'create_machine',
                  'ipv6')


class OptionalTopologieSerializer(NamespacedHMSerializer):
319 320
    """Serialize `preferences.models.OptionalTopologie` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
321 322 323 324 325 326 327
    class Meta:
        model = preferences.OptionalTopologie
        fields = ('radius_general_policy', 'vlan_decision_ok',
                  'vlan_decision_nok')


class GeneralOptionSerializer(NamespacedHMSerializer):
328 329
    """Serialize `preferences.models.GeneralOption` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
330 331 332 333 334 335 336 337 338
    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')


class ServiceSerializer(NamespacedHMSerializer):
339 340
    """Serialize `preferences.models.Service` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
341 342 343 344 345 346
    class Meta:
        model = preferences.Service
        fields = ('name', 'url', 'description', 'image', 'api_url')


class AssoOptionSerializer(NamespacedHMSerializer):
347 348
    """Serialize `preferences.models.AssoOption` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
349 350 351 352 353 354 355 356
    class Meta:
        model = preferences.AssoOption
        fields = ('name', 'siret', 'adresse1', 'adresse2', 'contact',
                  'telephone', 'pseudo', 'utilisateur_asso', 'payment',
                  'payment_id', 'payment_pass', 'description')


class HomeOptionSerializer(NamespacedHMSerializer):
357 358
    """Serialize `preferences.models.HomeOption` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
359 360 361 362 363 364
    class Meta:
        model = preferences.HomeOption
        fields = ('facebook_url', 'twitter_url', 'twitter_account_name')


class MailMessageOptionSerializer(NamespacedHMSerializer):
365 366
    """Serialize `preferences.models.MailMessageOption` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
367 368 369
    class Meta:
        model = preferences.MailMessageOption
        fields = ('welcome_mail_fr', 'welcome_mail_en')
370 371


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

373
# TOPOLOGIE
Maël Kervella's avatar
Maël Kervella committed
374 375


Maël Kervella's avatar
Maël Kervella committed
376
class StackSerializer(NamespacedHMSerializer):
377 378
    """Serialize `topologie.models.Stack` objects
    """
Maël Kervella's avatar
Maël Kervella committed
379
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
380
        model = topologie.Stack
Maël Kervella's avatar
Maël Kervella committed
381 382 383 384
        fields = ('name', 'stack_id', 'details', 'member_id_min',
                  'member_id_max', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
385
class AccessPointSerializer(NamespacedHMSerializer):
386 387
    """Serialize `topologie.models.AccessPoint` 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.AccessPoint
Maël Kervella's avatar
Maël Kervella committed
390 391 392
        fields = ('user', 'name', 'active', 'location', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
393
class SwitchSerializer(NamespacedHMSerializer):
394 395
    """Serialize `topologie.models.Switch` objects
    """
Maël Kervella's avatar
Maël Kervella committed
396 397
    port_amount = serializers.IntegerField(source='number')
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
398
        model = topologie.Switch
Maël Kervella's avatar
Maël Kervella committed
399 400
        fields = ('user', 'name', 'active', 'port_amount', 'stack',
                  'stack_member_id', 'model', 'switchbay', 'api_url')
Maël Kervella's avatar
Maël Kervella committed
401 402


Maël Kervella's avatar
Maël Kervella committed
403
class ModelSwitchSerializer(NamespacedHMSerializer):
404 405
    """Serialize `topologie.models.ModelSwitch` objects
    """
Maël Kervella's avatar
Maël Kervella committed
406
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
407
        model = topologie.ModelSwitch
Maël Kervella's avatar
Maël Kervella committed
408 409 410
        fields = ('reference', 'constructor', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
411
class ConstructorSwitchSerializer(NamespacedHMSerializer):
412 413
    """Serialize `topologie.models.ConstructorSwitch` objects
    """
Maël Kervella's avatar
Maël Kervella committed
414
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
415
        model = topologie.ConstructorSwitch
Maël Kervella's avatar
Maël Kervella committed
416 417 418
        fields = ('name', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
419
class SwitchBaySerializer(NamespacedHMSerializer):
420 421
    """Serialize `topologie.models.SwitchBay` objects
    """
Maël Kervella's avatar
Maël Kervella committed
422
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
423
        model = topologie.SwitchBay
Maël Kervella's avatar
Maël Kervella committed
424 425 426
        fields = ('name', 'building', 'info', 'api_url')


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


Maël Kervella's avatar
Maël Kervella committed
435
class SwitchPortSerializer(NamespacedHMSerializer):
436 437
    """Serialize `topologie.models.Port` objects
    """
Maël Kervella's avatar
Maël Kervella committed
438
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
439
        model = topologie.Port
Maël Kervella's avatar
Maël Kervella committed
440 441
        fields = ('switch', 'port', 'room', 'machine_interface', 'related',
                  'radius', 'vlan_force', 'details', 'api_url')
Maël Kervella's avatar
Maël Kervella committed
442 443 444 445
        extra_kwargs = {
            'related': {'view_name': 'switchport-detail'},
            'api_url': {'view_name': 'switchport-detail'}
        }
Maël Kervella's avatar
Maël Kervella committed
446 447


Maël Kervella's avatar
Maël Kervella committed
448
class RoomSerializer(NamespacedHMSerializer):
449 450
    """Serialize `topologie.models.Room` objects
    """
Maël Kervella's avatar
Maël Kervella committed
451
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
452
        model = topologie.Room
Maël Kervella's avatar
Maël Kervella committed
453 454 455
        fields = ('name', 'details', 'api_url')


456
# USERS
457

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

Maël Kervella's avatar
Maël Kervella committed
459
class UserSerializer(NamespacedHMSerializer):
460 461
    """Serialize `users.models.User` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
462 463
    access = serializers.BooleanField(source='has_access')
    uid = serializers.IntegerField(source='uid_number')
Maël Kervella's avatar
Maël Kervella committed
464 465

    class Meta:
Maël Kervella's avatar
Maël Kervella committed
466
        model = users.User
Maël Kervella's avatar
Maël Kervella committed
467
        fields = ('name', 'pseudo', 'email', 'school', 'shell', 'comment',
Maël Kervella's avatar
Maël Kervella committed
468 469 470 471 472
                  '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
473

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

Maël Kervella's avatar
Maël Kervella committed
475
class ClubSerializer(NamespacedHMSerializer):
476 477
    """Serialize `users.models.Club` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
478 479 480
    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
481 482

    class Meta:
Maël Kervella's avatar
Maël Kervella committed
483
        model = users.Club
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
                  'state', 'registered', 'telephone', 'solde', 'room',
Maël Kervella's avatar
Maël Kervella committed
486 487
                  'access', 'end_access', 'administrators', 'members',
                  'mailing', 'uid', 'api_url')
Maël Kervella's avatar
Maël Kervella committed
488 489 490
        extra_kwargs = {
            'shell': {'view_name': 'shell-detail'}
        }
Maël Kervella's avatar
Maël Kervella committed
491 492


Maël Kervella's avatar
Maël Kervella committed
493
class AdherentSerializer(NamespacedHMSerializer):
494 495
    """Serialize `users.models.Adherent` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
496 497
    access = serializers.BooleanField(source='has_access')
    uid = serializers.IntegerField(source='uid_number')
Maël Kervella's avatar
Maël Kervella committed
498

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


Maël Kervella's avatar
Maël Kervella committed
509
class ServiceUserSerializer(NamespacedHMSerializer):
510 511
    """Serialize `users.models.ServiceUser` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
512
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
513
        model = users.ServiceUser
Maël Kervella's avatar
Maël Kervella committed
514
        fields = ('pseudo', 'access_group', 'comment', 'api_url')
Maël Kervella's avatar
Maël Kervella committed
515 516


Maël Kervella's avatar
Maël Kervella committed
517
class SchoolSerializer(NamespacedHMSerializer):
518 519
    """Serialize `users.models.School` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
520
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
521
        model = users.School
Maël Kervella's avatar
Maël Kervella committed
522
        fields = ('name', 'api_url')
Maël Kervella's avatar
Maël Kervella committed
523 524


Maël Kervella's avatar
Maël Kervella committed
525
class ListRightSerializer(NamespacedHMSerializer):
526 527
    """Serialize `users.models.ListRight` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
528
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
529
        model = users.ListRight
Maël Kervella's avatar
Maël Kervella committed
530
        fields = ('unix_name', 'gid', 'critical', 'details', 'api_url')
Maël Kervella's avatar
Maël Kervella committed
531 532


Maël Kervella's avatar
Maël Kervella committed
533
class ShellSerializer(NamespacedHMSerializer):
534 535
    """Serialize `users.models.ListShell` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
536
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
537
        model = users.ListShell
Maël Kervella's avatar
Maël Kervella committed
538
        fields = ('shell', 'api_url')
Maël Kervella's avatar
Maël Kervella committed
539 540 541
        extra_kwargs = {
            'api_url': {'view_name': 'shell-detail'}
        }
Maël Kervella's avatar
Maël Kervella committed
542 543


Maël Kervella's avatar
Maël Kervella committed
544
class BanSerializer(NamespacedHMSerializer):
545 546
    """Serialize `users.models.Ban` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
547
    active = serializers.BooleanField(source='is_active')
Maël Kervella's avatar
Maël Kervella committed
548 549

    class Meta:
Maël Kervella's avatar
Maël Kervella committed
550
        model = users.Ban
Maël Kervella's avatar
Maël Kervella committed
551 552
        fields = ('user', 'raison', 'date_start', 'date_end', 'state',
                  'active', 'api_url')
Maël Kervella's avatar
Maël Kervella committed
553 554


Maël Kervella's avatar
Maël Kervella committed
555
class WhitelistSerializer(NamespacedHMSerializer):
556 557
    """Serialize `users.models.Whitelist` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
558
    active = serializers.BooleanField(source='is_active')
Maël Kervella's avatar
Maël Kervella committed
559 560

    class Meta:
Maël Kervella's avatar
Maël Kervella committed
561
        model = users.Whitelist
Maël Kervella's avatar
Maël Kervella committed
562
        fields = ('user', 'raison', 'date_start', 'date_end', 'active', 'api_url')
Maël Kervella's avatar
Maël Kervella committed
563 564


565
# SERVICE REGEN
566 567 568


class ServiceRegenSerializer(NamespacedHMSerializer):
569 570
    """Serialize the data about the services to regen.
    """
571 572
    hostname = serializers.CharField(source='server.domain.name', read_only=True)
    service_name = serializers.CharField(source='service.service_type', read_only=True)
573
    need_regen = serializers.BooleanField()
574 575 576 577 578

    class Meta:
        model = machines.Service_link
        fields = ('hostname', 'service_name', 'need_regen', 'api_url')
        extra_kwargs = {
579
            'api_url': {'view_name': 'serviceregen-detail'}
580 581 582
        }


Maël Kervella's avatar
Maël Kervella committed
583 584 585 586
# DHCP


class HostMacIpSerializer(serializers.ModelSerializer):
587 588 589
    """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
590 591
    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
592
    mac_address = serializers.CharField(read_only=True)
Maël Kervella's avatar
Maël Kervella committed
593 594 595 596 597
    ipv4 = serializers.CharField(source='ipv4.ipv4', read_only=True)

    class Meta:
        model = machines.Interface
        fields = ('hostname', 'extension', 'mac_address', 'ipv4')
598 599 600 601 602 603


# DNS


class SOARecordSerializer(SOASerializer):
604 605 606
    """Serialize `machines.models.SOA` objects with the data needed to
    generate a SOA DNS record.
    """
607 608 609 610 611 612
    class Meta:
        model = machines.SOA
        fields = ('name', 'mail', 'refresh', 'retry', 'expire', 'ttl')


class OriginV4RecordSerializer(IpListSerializer):
613 614 615
    """Serialize `machines.models.IpList` objects with the data needed to
    generate an IPv4 Origin DNS record.
    """
616 617 618 619 620
    class Meta(IpListSerializer.Meta):
        fields = ('ipv4',)


class OriginV6RecordSerializer(Ipv6ListSerializer):
621 622 623
    """Serialize `machines.models.Ipv6List` objects with the data needed to
    generate an IPv6 Origin DNS record.
    """
624 625 626 627 628
    class Meta(Ipv6ListSerializer.Meta):
        fields = ('ipv6',)


class NSRecordSerializer(NsSerializer):
629 630 631
    """Serialize `machines.models.Ns` objects with the data needed to
    generate a NS DNS record.
    """
632 633 634 635 636 637 638
    target = serializers.CharField(source='ns.name', read_only=True)

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


class MXRecordSerializer(MxSerializer):
639 640 641
    """Serialize `machines.models.Mx` objects with the data needed to
    generate a MX DNS record.
    """
642 643 644 645 646 647 648
    target = serializers.CharField(source='name.name', read_only=True)

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


class TXTRecordSerializer(TxtSerializer):
649 650 651
    """Serialize `machines.models.Txt` objects with the data needed to
    generate a TXT DNS record.
    """
652 653 654 655 656
    class Meta(TxtSerializer.Meta):
        fields = ('field1', 'field2')


class SRVRecordSerializer(SrvSerializer):
657 658 659
    """Serialize `machines.models.Srv` objects with the data needed to
    generate a SRV DNS record.
    """
660 661 662 663 664 665 666
    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):
667 668 669
    """Serialize `machines.models.Interface` objects with the data needed to
    generate a A DNS record.
    """
670 671 672 673 674 675 676 677 678
    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):
679 680 681
    """Serialize `machines.models.Interface` objects with the data needed to
    generate a AAAA DNS record.
    """
682
    hostname = serializers.CharField(source='domain.name', read_only=True)
683
    ipv6 = Ipv6ListSerializer(many=True, read_only=True)
684 685 686 687 688 689 690

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


class CNAMERecordSerializer(serializers.ModelSerializer):
691 692 693
    """Serialize `machines.models.Domain` objects with the data needed to
    generate a CNAME DNS record.
    """
694 695 696 697 698 699 700 701
    alias = serializers.CharField(source='cname.name', read_only=True)
    hostname = serializers.CharField(source='name', read_only=True)

    class Meta:
        model = machines.Domain
        fields = ('alias', 'hostname')


702
class DNSZonesSerializer(serializers.ModelSerializer):
703 704
    """Serialize the data about DNS Zones.
    """
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
    soa = SOARecordSerializer()
    ns_records = NSRecordSerializer(many=True, source='ns_set')
    originv4 = OriginV4RecordSerializer(source='origin')
    originv6 = OriginV6RecordSerializer(source='origin_v6')
    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',
720
                  'aaaa_records', 'cname_records')
Maël Kervella's avatar
Maël Kervella committed
721 722


723
# MAILING
Maël Kervella's avatar
Maël Kervella committed
724 725 726


class MailingMemberSerializer(UserSerializer):
727 728
    """Serialize the data about a mailing member.
    """
Maël Kervella's avatar
Maël Kervella committed
729 730 731 732
    class Meta(UserSerializer.Meta):
        fields = ('name', 'pseudo', 'email')

class MailingSerializer(ClubSerializer):
733 734
    """Serialize the data about a mailing.
    """
Maël Kervella's avatar
Maël Kervella committed
735 736 737 738 739
    members = MailingMemberSerializer(many=True)
    admins = MailingMemberSerializer(source='administrators', many=True)

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