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

Maël Kervella's avatar
Maël Kervella committed
21
"""
Maël Kervella's avatar
Maël Kervella committed
22 23
Serializers for the API app
"""
Maël Kervella's avatar
Maël Kervella committed
24 25

from rest_framework import serializers
26

Maël Kervella's avatar
Maël Kervella committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
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


API_NAMESPACE = 'api'


class NamespacedHRField(serializers.HyperlinkedRelatedField):
    """ A HyperlinkedRelatedField subclass to automatically prefix
    view names with a namespace """
    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):
    """ A HyperlinkedIdentityField subclass to automatically prefix
    view names with a namespace """
    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):
    """ A HyperlinkedModelSerializer subclass to use `NamespacedHRField` as
    field and automatically prefix view names with a namespace """
    serializer_related_field = NamespacedHRField
    serializer_url_field = NamespacedHIField
Maël Kervella's avatar
Maël Kervella committed
60

Maël Kervella's avatar
Maël Kervella committed
61 62

# COTISATIONS APP
63

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

class FactureSerializer(NamespacedHMSerializer):
66
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
67
        model = cotisations.Facture
68 69 70 71
        fields = ('user', 'paiement', 'banque', 'cheque', 'date', 'valid',
                  'control', 'prix_total', 'name', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
72
class VenteSerializer(NamespacedHMSerializer):
73
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
74
        model = cotisations.Vente
75 76 77 78
        fields = ('facture', 'number', 'name', 'prix', 'duration',
                  'type_cotisation', 'prix_total', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
79
class ArticleSerializer(NamespacedHMSerializer):
80
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
81
        model = cotisations.Article
82 83 84 85
        fields = ('name', 'prix', 'duration', 'type_user',
                  'type_cotisation', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
86
class BanqueSerializer(NamespacedHMSerializer):
87
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
88
        model = cotisations.Banque
89 90 91
        fields = ('name', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
92
class PaiementSerializer(NamespacedHMSerializer):
93
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
94
        model = cotisations.Paiement
95 96 97
        fields = ('moyen', 'type_paiement', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
98
class CotisationSerializer(NamespacedHMSerializer):
99
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
100
        model = cotisations.Cotisation
101 102 103 104
        fields = ('vente', 'type_cotisation', 'date_start', 'date_end',
                  'api_url')


Maël Kervella's avatar
Maël Kervella committed
105 106 107
# MACHINES APP


Maël Kervella's avatar
Maël Kervella committed
108
class MachineSerializer(NamespacedHMSerializer):
Maël Kervella's avatar
Maël Kervella committed
109
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
110
        model = machines.Machine
Maël Kervella's avatar
Maël Kervella committed
111 112 113
        fields = ('user', 'name', 'active', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
114
class MachineTypeSerializer(NamespacedHMSerializer):
Maël Kervella's avatar
Maël Kervella committed
115
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
116
        model = machines.MachineType
Maël Kervella's avatar
Maël Kervella committed
117 118 119
        fields = ('type', 'ip_type', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
120
class IpTypeSerializer(NamespacedHMSerializer):
Maël Kervella's avatar
Maël Kervella committed
121
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
122
        model = machines.IpType
Maël Kervella's avatar
Maël Kervella committed
123 124 125 126 127
        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
128
class VlanSerializer(NamespacedHMSerializer):
Maël Kervella's avatar
Maël Kervella committed
129
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
130
        model = machines.Vlan
Maël Kervella's avatar
Maël Kervella committed
131 132 133
        fields = ('vlan_id', 'name', 'comment', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
134
class NasSerializer(NamespacedHMSerializer):
Maël Kervella's avatar
Maël Kervella committed
135
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
136
        model = machines.Nas
Maël Kervella's avatar
Maël Kervella committed
137 138 139 140
        fields = ('name', 'nas_type', 'machine_type', 'port_access_mode',
                  'autocapture_mac', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
141
class SOASerializer(NamespacedHMSerializer):
Maël Kervella's avatar
Maël Kervella committed
142
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
143
        model = machines.SOA
Maël Kervella's avatar
Maël Kervella committed
144 145 146 147
        fields = ('name', 'mail', 'refresh', 'retry', 'expire', 'ttl',
                  'api_url')


Maël Kervella's avatar
Maël Kervella committed
148
class ExtensionSerializer(NamespacedHMSerializer):
Maël Kervella's avatar
Maël Kervella committed
149
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
150
        model = machines.Extension
Maël Kervella's avatar
Maël Kervella committed
151 152 153 154
        fields = ('name', 'need_infra', 'origin', 'origin_v6', 'soa',
                  'api_url')


Maël Kervella's avatar
Maël Kervella committed
155
class MxSerializer(NamespacedHMSerializer):
Maël Kervella's avatar
Maël Kervella committed
156
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
157
        model = machines.Mx
Maël Kervella's avatar
Maël Kervella committed
158 159 160
        fields = ('zone', 'priority', 'name', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
161
class NsSerializer(NamespacedHMSerializer):
Maël Kervella's avatar
Maël Kervella committed
162
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
163
        model = machines.Ns
Maël Kervella's avatar
Maël Kervella committed
164 165 166
        fields = ('zone', 'ns', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
167
class TxtSerializer(NamespacedHMSerializer):
Maël Kervella's avatar
Maël Kervella committed
168
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
169
        model = machines.Txt
Maël Kervella's avatar
Maël Kervella committed
170 171 172
        fields = ('zone', 'field1', 'field2', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
173
class SrvSerializer(NamespacedHMSerializer):
Maël Kervella's avatar
Maël Kervella committed
174
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
175
        model = machines.Srv
Maël Kervella's avatar
Maël Kervella committed
176 177 178 179
        fields = ('service', 'protocole', 'extension', 'ttl', 'priority',
                  'weight', 'port', 'target', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
180
class InterfaceSerializer(NamespacedHMSerializer):
Maël Kervella's avatar
Maël Kervella committed
181
    mac_address = serializers.CharField()
Maël Kervella's avatar
Maël Kervella committed
182 183 184
    active = serializers.BooleanField(source='is_active')

    class Meta:
Maël Kervella's avatar
Maël Kervella committed
185
        model = machines.Interface
Maël Kervella's avatar
Maël Kervella committed
186 187 188 189
        fields = ('ipv4', 'mac_address', 'machine', 'type', 'details',
                  'port_lists', 'active', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
190
class Ipv6ListSerializer(NamespacedHMSerializer):
Maël Kervella's avatar
Maël Kervella committed
191
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
192
        model = machines.Ipv6List
Maël Kervella's avatar
Maël Kervella committed
193 194 195 196
        fields = ('ipv6', 'interface', 'slaac_ip', 'date_end',
                  'api_url')


Maël Kervella's avatar
Maël Kervella committed
197
class DomainSerializer(NamespacedHMSerializer):
Maël Kervella's avatar
Maël Kervella committed
198
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
199
        model = machines.Domain
Maël Kervella's avatar
Maël Kervella committed
200 201 202 203
        fields = ('interface_parent', 'name', 'extension', 'cname',
                  'api_url')


Maël Kervella's avatar
Maël Kervella committed
204
class IpListSerializer(NamespacedHMSerializer):
Maël Kervella's avatar
Maël Kervella committed
205
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
206
        model = machines.IpList
Maël Kervella's avatar
Maël Kervella committed
207 208 209
        fields = ('ipv4', 'ip_type', 'need_infra', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
210
class ServiceSerializer(NamespacedHMSerializer):
Maël Kervella's avatar
Maël Kervella committed
211
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
212
        model = machines.Service
Maël Kervella's avatar
Maël Kervella committed
213 214 215 216
        fields = ('service_type', 'min_time_regen', 'regular_time_regen',
                  'servers', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
217
class ServiceLinkSerializer(NamespacedHMSerializer):
218 219
    need_regen = serializers.BooleanField()

Maël Kervella's avatar
Maël Kervella committed
220
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
221
        model = machines.Service_link
Maël Kervella's avatar
Maël Kervella committed
222 223
        fields = ('service', 'server', 'last_regen', 'asked_regen',
                  'need_regen', 'api_url')
Maël Kervella's avatar
Maël Kervella committed
224 225 226
        extra_kwargs = {
            'api_url': {'view_name': 'servicelink-detail'}
        }
Maël Kervella's avatar
Maël Kervella committed
227 228


Maël Kervella's avatar
Maël Kervella committed
229
class OuverturePortListSerializer(NamespacedHMSerializer):
Maël Kervella's avatar
Maël Kervella committed
230
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
231
        model = machines.OuverturePortList
Maël Kervella's avatar
Maël Kervella committed
232 233 234 235
        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
236
class OuverturePortSerializer(NamespacedHMSerializer):
Maël Kervella's avatar
Maël Kervella committed
237
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
238
        model = machines.OuverturePort
Maël Kervella's avatar
Maël Kervella committed
239 240 241
        fields = ('begin', 'end', 'port_list', 'protocole', 'io', 'api_url')


242 243 244
# PREFERENCES APP


Maël Kervella's avatar
Maël Kervella committed
245
# class OptionalUserSerializer(NamespacedHMSerializer):
246 247 248
#     tel_mandatory = serializers.BooleanField(source='is_tel_mandatory')
# 
#     class Meta:
Maël Kervella's avatar
Maël Kervella committed
249
#         model = preferences.OptionalUser
250 251 252 253 254 255
#         fields = ('tel_mandatory', 'user_solde', 'solde_negatif', 'max_solde',
#                   'min_online_payement', 'gpg_fingerprint',
#                   'all_can_create_club', 'self_adhesion', 'shell_default',
#                   'api_url')
# 
# 
Maël Kervella's avatar
Maël Kervella committed
256
# class OptionalMachineSerializer(NamespacedHMSerializer):
257
#     class Meta:
Maël Kervella's avatar
Maël Kervella committed
258
#         model = preferences.OptionalMachine
259 260 261 262 263
#         fields = ('password_machine', 'max_lambdauser_interfaces',
#                   'max_lambdauser_aliases', 'ipv6_mode', 'create_machine',
#                   'ipv6', 'api_url')
# 
# 
Maël Kervella's avatar
Maël Kervella committed
264
# class OptionalTopologieSerializer(NamespacedHMSerializer):
265
#     class Meta:
Maël Kervella's avatar
Maël Kervella committed
266
#         model = preferences.OptionalTopologie
267 268 269 270
#         fields = ('radius_general_policy', 'vlan_decision_ok',
#                   'vlan_decision_no', 'api_url')
# 
# 
Maël Kervella's avatar
Maël Kervella committed
271
# class GeneralOptionSerializer(NamespacedHMSerializer):
272
#     class Meta:
Maël Kervella's avatar
Maël Kervella committed
273
#         model = preferences.GeneralOption
274 275 276 277 278 279
#         fields = ('general_message', 'search_display_page',
#                   'pagination_number', 'pagination_large_number',
#                   'req_expire_hrs', 'site_name', 'email_from', 'GTU_sum_up',
#                   'GTU', 'api_url')
# 
# 
Maël Kervella's avatar
Maël Kervella committed
280
# class ServiceOptionSerializer(NamespacedHMSerializer):
281
#     class Meta:
Maël Kervella's avatar
Maël Kervella committed
282
#         model = preferences.ServiceOption
283 284 285
#         fields = ('name', 'url', 'description', 'image', 'api_url')
# 
# 
Maël Kervella's avatar
Maël Kervella committed
286
# class AssoOptionSerializer(NamespacedHMSerializer):
287
#     class Meta:
Maël Kervella's avatar
Maël Kervella committed
288
#         model = preferences.AssoOption
289 290 291 292 293
#         fields = ('name', 'siret', 'adresse1', 'adresse2', 'contact',
#                   'telephone', 'pseudo', 'utilisateur_asso', 'payement',
#                   'payement_id', 'payement_pass', 'description', 'api_url')
# 
# 
Maël Kervella's avatar
Maël Kervella committed
294
# class HomeOptionSerializer(NamespacedHMSerializer):
295
#     class Meta:
Maël Kervella's avatar
Maël Kervella committed
296
#         model = preferences.HomeOption
297 298 299 300
#         fields = ('facebook_url', 'twitter_url', 'twitter_account_name',
#                   'api_url')
# 
# 
Maël Kervella's avatar
Maël Kervella committed
301
# class MailMessageOptionSerializer(NamespacedHMSerializer):
302
#     class Meta:
Maël Kervella's avatar
Maël Kervella committed
303
#         model = preferences.MailMessageOption
304 305 306
#         fields = ('welcome_mail_fr', 'welcome_mail_en', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
307 308 309 310

# TOPOLOGIE APP


Maël Kervella's avatar
Maël Kervella committed
311
class StackSerializer(NamespacedHMSerializer):
Maël Kervella's avatar
Maël Kervella committed
312
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
313
        model = topologie.Stack
Maël Kervella's avatar
Maël Kervella committed
314 315 316 317
        fields = ('name', 'stack_id', 'details', 'member_id_min',
                  'member_id_max', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
318
class AccessPointSerializer(NamespacedHMSerializer):
Maël Kervella's avatar
Maël Kervella committed
319
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
320
        model = topologie.AccessPoint
Maël Kervella's avatar
Maël Kervella committed
321 322 323
        fields = ('user', 'name', 'active', 'location', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
324
class SwitchSerializer(NamespacedHMSerializer):
Maël Kervella's avatar
Maël Kervella committed
325 326
    port_amount = serializers.IntegerField(source='number')
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
327
        model = topologie.Switch
Maël Kervella's avatar
Maël Kervella committed
328 329
        fields = ('user', 'name', 'active', 'port_amount', 'stack',
                  'stack_member_id', 'model', 'switchbay', 'api_url')
Maël Kervella's avatar
Maël Kervella committed
330 331


Maël Kervella's avatar
Maël Kervella committed
332
class ModelSwitchSerializer(NamespacedHMSerializer):
Maël Kervella's avatar
Maël Kervella committed
333
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
334
        model = topologie.ModelSwitch
Maël Kervella's avatar
Maël Kervella committed
335 336 337
        fields = ('reference', 'constructor', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
338
class ConstructorSwitchSerializer(NamespacedHMSerializer):
Maël Kervella's avatar
Maël Kervella committed
339
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
340
        model = topologie.ConstructorSwitch
Maël Kervella's avatar
Maël Kervella committed
341 342 343
        fields = ('name', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
344
class SwitchBaySerializer(NamespacedHMSerializer):
Maël Kervella's avatar
Maël Kervella committed
345
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
346
        model = topologie.SwitchBay
Maël Kervella's avatar
Maël Kervella committed
347 348 349
        fields = ('name', 'building', 'info', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
350
class BuildingSerializer(NamespacedHMSerializer):
Maël Kervella's avatar
Maël Kervella committed
351
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
352
        model = topologie.Building
Maël Kervella's avatar
Maël Kervella committed
353 354 355
        fields = ('name', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
356
class SwitchPortSerializer(NamespacedHMSerializer):
Maël Kervella's avatar
Maël Kervella committed
357
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
358
        model = topologie.Port
Maël Kervella's avatar
Maël Kervella committed
359 360
        fields = ('switch', 'port', 'room', 'machine_interface', 'related',
                  'radius', 'vlan_force', 'details', 'api_url')
Maël Kervella's avatar
Maël Kervella committed
361 362 363 364
        extra_kwargs = {
            'related': {'view_name': 'switchport-detail'},
            'api_url': {'view_name': 'switchport-detail'}
        }
Maël Kervella's avatar
Maël Kervella committed
365 366


Maël Kervella's avatar
Maël Kervella committed
367
class RoomSerializer(NamespacedHMSerializer):
Maël Kervella's avatar
Maël Kervella committed
368
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
369
        model = topologie.Room
Maël Kervella's avatar
Maël Kervella committed
370 371 372
        fields = ('name', 'details', 'api_url')


Maël Kervella's avatar
Maël Kervella committed
373
# USERS APP
374

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

Maël Kervella's avatar
Maël Kervella committed
376
class UserSerializer(NamespacedHMSerializer):
Maël Kervella's avatar
Maël Kervella committed
377 378
    access = serializers.BooleanField(source='has_access')
    uid = serializers.IntegerField(source='uid_number')
Maël Kervella's avatar
Maël Kervella committed
379 380

    class Meta:
Maël Kervella's avatar
Maël Kervella committed
381
        model = users.User
Maël Kervella's avatar
Maël Kervella committed
382
        fields = ('name', 'pseudo', 'email', 'school', 'shell', 'comment',
Maël Kervella's avatar
Maël Kervella committed
383 384 385 386 387
                  '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
388

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

Maël Kervella's avatar
Maël Kervella committed
390
class ClubSerializer(NamespacedHMSerializer):
Maël Kervella's avatar
Maël Kervella committed
391 392 393
    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
394 395

    class Meta:
Maël Kervella's avatar
Maël Kervella committed
396
        model = users.Club
Maël Kervella's avatar
Maël Kervella committed
397
        fields = ('name', 'pseudo', 'email', 'school', 'shell', 'comment',
Maël Kervella's avatar
Maël Kervella committed
398
                  'state', 'registered', 'telephone', 'solde', 'room',
Maël Kervella's avatar
Maël Kervella committed
399 400
                  'access', 'end_access', 'administrators', 'members',
                  'mailing', 'uid', 'api_url')
Maël Kervella's avatar
Maël Kervella committed
401 402 403
        extra_kwargs = {
            'shell': {'view_name': 'shell-detail'}
        }
Maël Kervella's avatar
Maël Kervella committed
404 405


Maël Kervella's avatar
Maël Kervella committed
406
class AdherentSerializer(NamespacedHMSerializer):
Maël Kervella's avatar
Maël Kervella committed
407 408
    access = serializers.BooleanField(source='has_access')
    uid = serializers.IntegerField(source='uid_number')
Maël Kervella's avatar
Maël Kervella committed
409

Maël Kervella's avatar
Maël Kervella committed
410
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
411
        model = users.Adherent
Maël Kervella's avatar
Maël Kervella committed
412
        fields = ('name', 'surname', 'pseudo', 'email', 'school', 'shell',
Maël Kervella's avatar
Maël Kervella committed
413
                  'comment', 'state', 'registered', 'telephone', 'room',
Maël Kervella's avatar
Maël Kervella committed
414
                  'solde', 'access', 'end_access', 'uid', 'api_url')
Maël Kervella's avatar
Maël Kervella committed
415 416 417
        extra_kwargs = {
            'shell': {'view_name': 'shell-detail'}
        }
Maël Kervella's avatar
Maël Kervella committed
418 419


Maël Kervella's avatar
Maël Kervella committed
420
class ServiceUserSerializer(NamespacedHMSerializer):
Maël Kervella's avatar
Maël Kervella committed
421
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
422
        model = users.ServiceUser
Maël Kervella's avatar
Maël Kervella committed
423
        fields = ('pseudo', 'access_group', 'comment', 'api_url')
Maël Kervella's avatar
Maël Kervella committed
424 425


Maël Kervella's avatar
Maël Kervella committed
426
class SchoolSerializer(NamespacedHMSerializer):
Maël Kervella's avatar
Maël Kervella committed
427
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
428
        model = users.School
Maël Kervella's avatar
Maël Kervella committed
429
        fields = ('name', 'api_url')
Maël Kervella's avatar
Maël Kervella committed
430 431


Maël Kervella's avatar
Maël Kervella committed
432
class ListRightSerializer(NamespacedHMSerializer):
Maël Kervella's avatar
Maël Kervella committed
433
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
434
        model = users.ListRight
Maël Kervella's avatar
Maël Kervella committed
435
        fields = ('unix_name', 'gid', 'critical', 'details', 'api_url')
Maël Kervella's avatar
Maël Kervella committed
436 437


Maël Kervella's avatar
Maël Kervella committed
438
class ShellSerializer(NamespacedHMSerializer):
Maël Kervella's avatar
Maël Kervella committed
439
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
440
        model = users.ListShell
Maël Kervella's avatar
Maël Kervella committed
441
        fields = ('shell', 'api_url')
Maël Kervella's avatar
Maël Kervella committed
442 443 444
        extra_kwargs = {
            'api_url': {'view_name': 'shell-detail'}
        }
Maël Kervella's avatar
Maël Kervella committed
445 446


Maël Kervella's avatar
Maël Kervella committed
447
class BanSerializer(NamespacedHMSerializer):
Maël Kervella's avatar
Maël Kervella committed
448
    active = serializers.BooleanField(source='is_active')
Maël Kervella's avatar
Maël Kervella committed
449 450

    class Meta:
Maël Kervella's avatar
Maël Kervella committed
451
        model = users.Ban
Maël Kervella's avatar
Maël Kervella committed
452 453
        fields = ('user', 'raison', 'date_start', 'date_end', 'state',
                  'active', 'api_url')
Maël Kervella's avatar
Maël Kervella committed
454 455


Maël Kervella's avatar
Maël Kervella committed
456
class WhitelistSerializer(NamespacedHMSerializer):
Maël Kervella's avatar
Maël Kervella committed
457
    active = serializers.BooleanField(source='is_active')
Maël Kervella's avatar
Maël Kervella committed
458 459

    class Meta:
Maël Kervella's avatar
Maël Kervella committed
460
        model = users.Whitelist
Maël Kervella's avatar
Maël Kervella committed
461
        fields = ('user', 'raison', 'date_start', 'date_end', 'active', 'api_url')
Maël Kervella's avatar
Maël Kervella committed
462 463


464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
# Services


class ServiceRegenSerializer(NamespacedHMSerializer):
    hostname = serializers.CharField(source='server.domain.name', read_only=True)
    service_name = serializers.CharField(source='service.service_type', read_only=True)

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


Maël Kervella's avatar
Maël Kervella committed
479 480 481 482 483 484
# DHCP


class HostMacIpSerializer(serializers.ModelSerializer):
    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
485
    mac_address = serializers.CharField(read_only=True)
Maël Kervella's avatar
Maël Kervella committed
486 487 488 489 490
    ipv4 = serializers.CharField(source='ipv4.ipv4', read_only=True)

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