serializers.py 35.9 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 27 28 29 30 31 32 33 34

from cotisations.models import (
    Facture,
    Vente,
    Article,
    Banque,
    Paiement,
    Cotisation
)
Maël Kervella's avatar
Maël Kervella committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
from machines.models import (
    Machine,
    MachineType,
    IpType,
    Vlan,
    Nas,
    SOA,
    Extension,
    Mx,
    Ns,
    Txt,
    Srv,
    Interface,
    Ipv6List,
    Domain,
    IpList,
    Service,
    Service_link,
    OuverturePortList,
    OuverturePort
)
56 57 58 59 60 61 62 63 64 65 66
from preferences.models import (
    OptionalUser,
    OptionalMachine,
    OptionalTopologie,
    GeneralOption,
    AssoOption,
    HomeOption,
    MailMessageOption
)
# Avoid duplicate names
from preferences.models import Service as ServiceOption
Maël Kervella's avatar
Maël Kervella committed
67 68 69 70 71 72 73 74 75 76 77
from topologie.models import (
    Stack,
    AccessPoint,
    Switch,
    ModelSwitch,
    ConstructorSwitch,
    SwitchBay,
    Building,
    Room
)
from topologie.models import Port as SwitchPort
Maël Kervella's avatar
Maël Kervella committed
78 79 80 81 82 83 84 85 86 87 88
from users.models import (
    User,
    Club,
    Adherent,
    ServiceUser,
    School,
    ListRight,
    ListShell,
    Ban,
    Whitelist
)
Maël Kervella's avatar
Maël Kervella committed
89

Maël Kervella's avatar
Maël Kervella committed
90 91

# COTISATIONS APP
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

class FactureSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Facture
        fields = ('user', 'paiement', 'banque', 'cheque', 'date', 'valid',
                  'control', 'prix_total', 'name', 'api_url')
        extra_kwargs = {
            'user': {'view_name': 'api:user-detail'},
            'paiement': {'view_name': 'api:paiement-detail'},
            'banque': {'view_name': 'api:banque-detail'},
            'api_url': {'view_name': 'api:facture-detail'}
        }


class VenteSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Vente
        fields = ('facture', 'number', 'name', 'prix', 'duration',
                  'type_cotisation', 'prix_total', 'api_url')
        extra_kwargs = {
            'facture': {'view_name': 'api:facture-detail'},
            'api_url': {'view_name': 'api:vente-detail'}
        }


class ArticleSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Article
        fields = ('name', 'prix', 'duration', 'type_user',
                  'type_cotisation', 'api_url')
        extra_kwargs = {
            'api_url': {'view_name': 'api:article-detail'}
        }


class BanqueSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Banque
        fields = ('name', 'api_url')
        extra_kwargs = {
            'api_url': {'view_name': 'api:banque-detail'}
        }


class PaiementSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Paiement
        fields = ('moyen', 'type_paiement', 'api_url')
        extra_kwargs = {
            'api_url': {'view_name': 'api:paiement-detail'}
        }


class CotisationSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Cotisation
        fields = ('vente', 'type_cotisation', 'date_start', 'date_end',
                  'api_url')
        extra_kwargs = {
            'vente': {'view_name': 'api:vente-detail'},
            'api_url': {'view_name': 'api:cotisation-detail'}
        }


Maël Kervella's avatar
Maël Kervella committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
# MACHINES APP


class MachineSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Machine
        fields = ('user', 'name', 'active', 'api_url')
        extra_kwargs = {
            'user': {'view_name': 'api:user-detail'},
            'api_url': {'view_name': 'api:machine-detail'}
        }


class MachineTypeSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = MachineType
        fields = ('type', 'ip_type', 'api_url')
        extra_kwargs = {
            'ip_type': {'view_name': 'api:iptype-detail'},
            'api_url': {'view_name': 'api:machinetype-detail'}
        }


class IpTypeSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = IpType
        fields = ('type', 'extension', 'need_infra', 'domaine_ip_start',
                  'domaine_ip_stop', 'prefix_v6', 'vlan', 'ouverture_ports',
                  'api_url')
        extra_kwargs = {
            'extension': {'view_name': 'api:extension-detail'},
            'vlan': {'view_name': 'api:vlan-detail'},
            'ouverture_ports': {'view_name': 'api:ouvertureportlist-detail'},
            'api_url': {'view_name': 'api:iptype-detail'}
        }


class VlanSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Vlan
        fields = ('vlan_id', 'name', 'comment', 'api_url')
        extra_kwargs = {
            'api_url': {'view_name': 'api:vlan-detail'}
        }


class NasSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Nas
        fields = ('name', 'nas_type', 'machine_type', 'port_access_mode',
                  'autocapture_mac', 'api_url')
        extra_kwargs = {
            'nas_type': {'view_name': 'api:machinetype-detail'},
            'machine_type': {'view_name': 'api:machinetype-detail'},
            'api_url': {'view_name': 'api:nas-detail'}
        }


class SOASerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = SOA
        fields = ('name', 'mail', 'refresh', 'retry', 'expire', 'ttl',
                  'api_url')
        extra_kwargs = {
            'api_url': {'view_name': 'api:soa-detail'}
        }


class ExtensionSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Extension
        fields = ('name', 'need_infra', 'origin', 'origin_v6', 'soa',
                  'api_url')
        extra_kwargs = {
            'origin': {'view_name': 'api:iplist-detail'},
            'soa': {'view_name': 'api:soa-detail'},
            'api_url': {'view_name': 'api:extension-detail'}
        }


class MxSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Mx
        fields = ('zone', 'priority', 'name', 'api_url')
        extra_kwargs = {
            'zone': {'view_name': 'api:extension-detail'},
            'name': {'view_name': 'api:domain-detail'},
            'api_url': {'view_name': 'api:mx-detail'}
        }


class NsSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Ns
        fields = ('zone', 'ns', 'api_url')
        extra_kwargs = {
            'zone': {'view_name': 'api:extension-detail'},
            'ns': {'view_name': 'api:domain-detail'},
            'api_url': {'view_name': 'api:ns-detail'}
        }


class TxtSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Txt
        fields = ('zone', 'field1', 'field2', 'api_url')
        extra_kwargs = {
            'zone': {'view_name': 'api:extension-detail'},
            'api_url': {'view_name': 'api:txt-detail'}
        }


class SrvSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Srv
        fields = ('service', 'protocole', 'extension', 'ttl', 'priority',
                  'weight', 'port', 'target', 'api_url')
        extra_kwargs = {
            'extension': {'view_name': 'api:extension-detail'},
            'target': {'view_name': 'api:domain-detail'},
            'api_url': {'view_name': 'api:mx-detail'}
        }


class InterfaceSerializer(serializers.HyperlinkedModelSerializer):
    active = serializers.BooleanField(source='is_active')

    class Meta:
        model = Interface
        fields = ('ipv4', 'mac_address', 'machine', 'type', 'details',
                  'port_lists', 'active', 'api_url')
        extra_kwargs = {
            'ipv4': {'view_name': 'api:iplist-detail'},
            'machine': {'view_name': 'api:machine-detail'},
            'type': {'view_name': 'api:machinetype-detail'},
            'port_lists': {'view_name': 'api:ouvertureportlist-detail'},
            'api_url': {'view_name': 'api:interface-detail'}
        }


class Ipv6ListSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Ipv6List
        fields = ('ipv6', 'interface', 'slaac_ip', 'date_end',
                  'api_url')
        extra_kwargs = {
            'interface': {'view_name': 'api:interface-detail'},
            'api_url': {'view_name': 'api:ipv6list-detail'}
        }


class DomainSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Domain
        fields = ('interface_parent', 'name', 'extension', 'cname',
                  'api_url')
        extra_kwargs = {
            'interface_parent': {'view_name': 'api:interface-detail'},
            'extension': {'view_name': 'api:extension-detail'},
            'cname': {'view_name': 'api:domain-detail'},
            'api_url': {'view_name': 'api:domain-detail'}
        }


class IpListSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = IpList
        fields = ('ipv4', 'ip_type', 'need_infra', 'api_url')
        extra_kwargs = {
            'ip_type': {'view_name': 'api:iptype-detail'},
            'api_url': {'view_name': 'api:iplist-detail'}
        }


class ServiceSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Service
        fields = ('service_type', 'min_time_regen', 'regular_time_regen',
                  'servers', 'api_url')
        extra_kwargs = {
            'servers': {'view_name': 'api:interface-detail'},
            'api_url': {'view_name': 'api:service-detail'}
        }


class ServiceLinkSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Service_link
        fields = ('service', 'server', 'last_regen', 'asked_regen',
                  'need_regen', 'api_url')
        extra_kwargs = {
            'service': {'view_name': 'api:service-detail'},
            'server': {'view_name': 'api:interface-detail'},
            'api_url': {'view_name': 'api:servicelink-detail'}
        }


class OuverturePortListSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = OuverturePortList
        fields = ('name', 'tcp_ports_in', 'udp_ports_in', 'tcp_ports_out',
                  'udp_ports_out', 'api_url')
        extra_kwargs = {
            'tcp_ports_in': {'view_name': 'api:ouvertureport-detail'},
            'udp_ports_in': {'view_name': 'api:ouvertureport-detail'},
            'tcp_ports_out': {'view_name': 'api:ouvertureport-detail'},
            'udp_ports_out': {'view_name': 'api:ouvertureport-detail'},
            'api_url': {'view_name': 'api:ouvertureportlist-detail'}
        }


class OuverturePortSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = OuverturePort
        fields = ('begin', 'end', 'port_list', 'protocole', 'io', 'api_url')
        extra_kwargs = {
            'port_list': {'view_name': 'api:ouvertureportlist-detail'},
            'api_url': {'view_name': 'api:ouvertureport-detail'}
        }


377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
# PREFERENCES APP


# class OptionalUserSerializer(serializers.HyperlinkedModelSerializer):
#     tel_mandatory = serializers.BooleanField(source='is_tel_mandatory')
# 
#     class Meta:
#         model = OptionalUser
#         fields = ('tel_mandatory', 'user_solde', 'solde_negatif', 'max_solde',
#                   'min_online_payement', 'gpg_fingerprint',
#                   'all_can_create_club', 'self_adhesion', 'shell_default',
#                   'api_url')
#         extra_kwargs = {
#             'shell_default': {'view_name': 'api:shell-detail'},
#             'api_url': {'view_name': 'api:optionaluser-detail'}
#         }
# 
# 
# class OptionalMachineSerializer(serializers.HyperlinkedModelSerializer):
#     class Meta:
#         model = OptionalMachine
#         fields = ('password_machine', 'max_lambdauser_interfaces',
#                   'max_lambdauser_aliases', 'ipv6_mode', 'create_machine',
#                   'ipv6', 'api_url')
#         extra_kwargs = {
#             'api_url': {'view_name': 'api:optionalmachine-detail'}
#         }
# 
# 
# class OptionalTopologieSerializer(serializers.HyperlinkedModelSerializer):
#     class Meta:
#         model = OptionalTopologie
#         fields = ('radius_general_policy', 'vlan_decision_ok',
#                   'vlan_decision_no', 'api_url')
#         extra_kwargs = {
#             'vlan_decision_ok': {'view_name': 'api:vlan-detail'},
#             'vlan_decision_nok': {'view_name': 'api:vlan-detail'},
#             'api_url': {'view_name': 'api:optionaltopologie-detail'}
#         }
# 
# 
# class GeneralOptionSerializer(serializers.HyperlinkedModelSerializer):
#     class Meta:
#         model = GeneralOption
#         fields = ('general_message', 'search_display_page',
#                   'pagination_number', 'pagination_large_number',
#                   'req_expire_hrs', 'site_name', 'email_from', 'GTU_sum_up',
#                   'GTU', 'api_url')
#         extra_kwargs = {
#             'api_url': {'view_name': 'api:generaloption-detail'}
#         }
# 
# 
# class ServiceOptionSerializer(serializers.HyperlinkedModelSerializer):
#     class Meta:
#         model = ServiceOption
#         fields = ('name', 'url', 'description', 'image', 'api_url')
#         extra_kwargs = {
#             'api_url': {'view_name': 'api:serviceoption-detail'}
#         }
# 
# 
# class AssoOptionSerializer(serializers.HyperlinkedModelSerializer):
#     class Meta:
#         model = AssoOption
#         fields = ('name', 'siret', 'adresse1', 'adresse2', 'contact',
#                   'telephone', 'pseudo', 'utilisateur_asso', 'payement',
#                   'payement_id', 'payement_pass', 'description', 'api_url')
#         extra_kwargs = {
#             'utilisateur_asso': {'view_name': 'api:user-detail'},
#             'api_url': {'view_name': 'api:assooption-detail'}
#         }
# 
# 
# class HomeOptionSerializer(serializers.HyperlinkedModelSerializer):
#     class Meta:
#         model = HomeOption
#         fields = ('facebook_url', 'twitter_url', 'twitter_account_name',
#                   'api_url')
#         extra_kwargs = {
#             'api_url': {'view_name': 'api:homeoption-detail'}
#         }
# 
# 
# class MailMessageOptionSerializer(serializers.HyperlinkedModelSerializer):
#     class Meta:
#         model = MailMessageOption
#         fields = ('welcome_mail_fr', 'welcome_mail_en', 'api_url')
#         extra_kwargs = {
#             'api_url': {'view_name': 'api:mailmessageoption-detail'}
#         }


Maël Kervella's avatar
Maël Kervella committed
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569

# TOPOLOGIE APP


class StackSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Stack
        fields = ('name', 'stack_id', 'details', 'member_id_min',
                  'member_id_max', 'api_url')
        extra_kwargs = {
            'api_url': {'view_name': 'api:stack-detail'}
        }


class AccessPointSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = AccessPoint
        fields = ('user', 'name', 'active', 'location', 'api_url')
        extra_kwargs = {
            'user': {'view_name': 'api:user-detail'},
            'api_url': {'view_name': 'api:accesspoint-detail'}
        }


class SwitchSerializer(serializers.HyperlinkedModelSerializer):
    port_amount = serializers.IntegerField(source='number')
    class Meta:
        model = Switch
        fields = ('port_amount', 'stack', 'stack_member_id', 'model',
                  'switchbay', 'api_url')
        extra_kwargs = {
            'stack': {'view_name': 'api:stack-detail'},
            'model': {'view_name': 'api:modelswitch-detail'},
            'switchbay': {'view_name': 'api:switchbay-detail'},
            'api_url': {'view_name': 'api:switch-detail'}
        }


class ModelSwitchSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = ModelSwitch
        fields = ('reference', 'constructor', 'api_url')
        extra_kwargs = {
            'constructor': {'view_name': 'api:constructorswitch-detail'},
            'api_url': {'view_name': 'api:modelswitch-detail'}
        }


class ConstructorSwitchSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = ConstructorSwitch
        fields = ('name', 'api_url')
        extra_kwargs = {
                'api_url': {'view_name': 'api:constructorswitch-detail'}
        }


class SwitchBaySerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = SwitchBay
        fields = ('name', 'building', 'info', 'api_url')
        extra_kwargs = {
            'building': {'view_name': 'api:building-detail'},
            'api_url': {'view_name': 'api:switchbay-detail'}
        }


class BuildingSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Building
        fields = ('name', 'api_url')
        extra_kwargs = {
            'api_url': {'view_name': 'api:building-detail'}
        }


class SwitchPortSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = SwitchPort
        fields = ('switch', 'port', 'room', 'machine_interface', 'related',
                  'radius', 'vlan_force', 'details', 'api_url')
        extra_kwargs = {
            'switch': {'view_name': 'api:switch-detail'},
            'room': {'view_name': 'api:room-detail'},
            'machine_interface': {'view_name': 'api:interface-detail'},
            'related': {'view_name': 'api:switchport-detail'},
            'vlan_force': {'view_name': 'api:vlan-detail'},
            'api_url': {'view_name': 'api:switchport-detail'}
        }


class RoomSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Room
        fields = ('name', 'details', 'api_url')
        extra_kwargs = {
            'api_url': {'view_name': 'api:room-detail'}
        }


Maël Kervella's avatar
Maël Kervella committed
570
# USERS APP
571

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

Maël Kervella's avatar
Maël Kervella committed
573 574 575
class UserSerializer(serializers.HyperlinkedModelSerializer):
    access = serializers.BooleanField(source='has_access')
    uid = serializers.IntegerField(source='uid_number')
Maël Kervella's avatar
Maël Kervella committed
576 577

    class Meta:
Maël Kervella's avatar
Maël Kervella committed
578 579 580 581 582 583 584 585 586 587
        model = User
        fields = ('name', 'pseudo', 'email', 'school', 'shell', 'comment',
                  'state', 'registered', 'telephone', 'solde', #'room',
                  'access', 'end_access', 'uid', 'class_name', 'api_url')
        extra_kwargs = {
            'school': {'view_name': 'api:school-detail'},
            'shell': {'view_name': 'api:shell-detail'},
            #'room': {'view_name': 'api:room-detail'},
            'api_url': {'view_name': 'api:user-detail'}
        }
Maël Kervella's avatar
Maël Kervella committed
588

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

Maël Kervella's avatar
Maël Kervella committed
590 591 592 593
class ClubSerializer(serializers.HyperlinkedModelSerializer):
    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
594 595 596

    class Meta:
        model = Club
Maël Kervella's avatar
Maël Kervella committed
597 598 599 600 601 602 603 604 605 606 607 608
        fields = ('name', 'pseudo', 'email', 'school', 'shell', 'comment',
                  'state', 'registered', 'telephone', 'solde', #'room',
                  'access', 'end_access', 'administrators', 'members',
                  'mailing', 'uid', 'api_url')
        extra_kwargs = {
            'school': {'view_name': 'api:school-detail'},
            'shell': {'view_name': 'api:shell-detail'},
            #'room': {'view_name': 'api:room-detail'},
            'administrators': {'view_name': 'api:adherent-detail'},
            'members': {'view_name': 'api:adherent-detail'},
            'api_url': {'view_name': 'api:club-detail'}
        }
Maël Kervella's avatar
Maël Kervella committed
609 610


Maël Kervella's avatar
Maël Kervella committed
611 612 613
class AdherentSerializer(serializers.HyperlinkedModelSerializer):
    access = serializers.BooleanField(source='has_access')
    uid = serializers.IntegerField(source='uid_number')
Maël Kervella's avatar
Maël Kervella committed
614

Maël Kervella's avatar
Maël Kervella committed
615 616
    class Meta:
        model = Adherent
Maël Kervella's avatar
Maël Kervella committed
617 618 619 620 621 622 623 624 625
        fields = ('name', 'surname', 'pseudo', 'email', 'school', 'shell',
                  'comment', 'state', 'registered', 'telephone', #'room',
                  'solde', 'access', 'end_access', 'uid', 'api_url')
        extra_kwargs = {
            'school': {'view_name': 'api:school-detail'},
            'shell': {'view_name': 'api:shell-detail'},
            #'room': {'view_name': 'api:room-detail'},
            'api_url': {'view_name': 'api:adherent-detail'}
        }
Maël Kervella's avatar
Maël Kervella committed
626 627


Maël Kervella's avatar
Maël Kervella committed
628
class ServiceUserSerializer(serializers.HyperlinkedModelSerializer):
Maël Kervella's avatar
Maël Kervella committed
629
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
630 631 632 633 634
        model = ServiceUser
        fields = ('pseudo', 'access_group', 'comment', 'api_url')
        extra_kwargs = {
            'api_url': {'view_name': 'api:serviceuser-detail'}
        }
Maël Kervella's avatar
Maël Kervella committed
635 636


Maël Kervella's avatar
Maël Kervella committed
637
class SchoolSerializer(serializers.HyperlinkedModelSerializer):
Maël Kervella's avatar
Maël Kervella committed
638
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
639 640 641 642 643
        model = School
        fields = ('name', 'api_url')
        extra_kwargs = {
            'api_url': {'view_name': 'api:school-detail'}
        }
Maël Kervella's avatar
Maël Kervella committed
644 645


Maël Kervella's avatar
Maël Kervella committed
646
class ListRightSerializer(serializers.HyperlinkedModelSerializer):
Maël Kervella's avatar
Maël Kervella committed
647
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
648 649 650 651 652
        model = ListRight
        fields = ('unix_name', 'gid', 'critical', 'details', 'api_url')
        extra_kwargs = {
            'api_url': {'view_name': 'api:listright-detail'}
        }
Maël Kervella's avatar
Maël Kervella committed
653 654


Maël Kervella's avatar
Maël Kervella committed
655
class ShellSerializer(serializers.HyperlinkedModelSerializer):
Maël Kervella's avatar
Maël Kervella committed
656
    class Meta:
Maël Kervella's avatar
Maël Kervella committed
657 658 659 660 661
        model = ListShell
        fields = ('shell', 'api_url')
        extra_kwargs = {
            'api_url': {'view_name': 'api:shell-detail'}
        }
Maël Kervella's avatar
Maël Kervella committed
662 663


Maël Kervella's avatar
Maël Kervella committed
664 665
class BanSerializer(serializers.HyperlinkedModelSerializer):
    active = serializers.BooleanField(source='is_active')
Maël Kervella's avatar
Maël Kervella committed
666 667

    class Meta:
Maël Kervella's avatar
Maël Kervella committed
668 669 670 671 672 673 674
        model = Ban
        fields = ('user', 'raison', 'date_start', 'date_end', 'state',
                  'active', 'api_url')
        extra_kwargs = {
            'user': {'view_name': 'api:user-detail'},
            'api_url': {'view_name': 'api:ban-detail'}
        }
Maël Kervella's avatar
Maël Kervella committed
675 676


Maël Kervella's avatar
Maël Kervella committed
677 678
class WhitelistSerializer(serializers.HyperlinkedModelSerializer):
    active = serializers.BooleanField(source='is_active')
Maël Kervella's avatar
Maël Kervella committed
679 680

    class Meta:
Maël Kervella's avatar
Maël Kervella committed
681 682 683 684 685 686
        model = Whitelist
        fields = ('user', 'raison', 'date_start', 'date_end', 'active', 'api_url')
        extra_kwargs = {
            'user': {'view_name': 'api:user-detail'},
            'api_url': {'view_name': 'api:whitelist-detail'}
        }
Maël Kervella's avatar
Maël Kervella committed
687

Maël Kervella's avatar
Maël Kervella committed
688 689


Maël Kervella's avatar
Maël Kervella committed
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 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 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
# class ServiceLinkSerializer(serializers.ModelSerializer):
#     """ Serializer for the ServiceLink objects """
# 
#     name = serializers.CharField(source='service.service_type')
# 
#     class Meta:
#         model = Service_link
#         fields = ('name',)
# 
# 
# class MailingSerializer(serializers.ModelSerializer):
#     """ Serializer to build Mailing objects """
# 
#     name = serializers.CharField(source='pseudo')
# 
#     class Meta:
#         model = Club
#         fields = ('name',)
# 
# 
# class MailingMemberSerializer(serializers.ModelSerializer):
#     """ Serializer fot the Adherent objects (who belong to a
#     Mailing) """
# 
#     class Meta:
#         model = Adherent
#         fields = ('email',)
# 
# 
# class IpTypeField(serializers.RelatedField):
#     """ Serializer for an IpType object field """
# 
#     def to_representation(self, value):
#         return value.type
# 
#     def to_internal_value(self, data):
#         pass
# 
# 
# class IpListSerializer(serializers.ModelSerializer):
#     """ Serializer for an Ipv4List obejct using the IpType serialization """
# 
#     ip_type = IpTypeField(read_only=True)
# 
#     class Meta:
#         model = IpList
#         fields = ('ipv4', 'ip_type')
# 
# 
# class Ipv6ListSerializer(serializers.ModelSerializer):
#     """ Serializer for an Ipv6List object """
# 
#     class Meta:
#         model = Ipv6List
#         fields = ('ipv6', 'slaac_ip')
# 
# 
# class InterfaceSerializer(serializers.ModelSerializer):
#     """ Serializer for an Interface object. Use SerializerMethodField
#     to get ForeignKey values """
# 
#     ipv4 = IpListSerializer(read_only=True)
#     # TODO : use serializer.RelatedField to avoid duplicate code
#     mac_address = serializers.SerializerMethodField('get_macaddress')
#     domain = serializers.SerializerMethodField('get_dns')
#     extension = serializers.SerializerMethodField('get_interface_extension')
# 
#     class Meta:
#         model = Interface
#         fields = ('ipv4', 'mac_address', 'domain', 'extension')
# 
#     @staticmethod
#     def get_dns(obj):
#         """ The name of the associated  DNS object """
#         return obj.domain.name
# 
#     @staticmethod
#     def get_interface_extension(obj):
#         """ The name of the associated Interface object """
#         return obj.domain.extension.name
# 
#     @staticmethod
#     def get_macaddress(obj):
#         """ The string representation of the associated MAC address """
#         return str(obj.mac_address)
# 
# 
# class FullInterfaceSerializer(serializers.ModelSerializer):
#     """ Serializer for an Interface obejct. Use SerializerMethodField
#     to get ForeignKey values """
# 
#     ipv4 = IpListSerializer(read_only=True)
#     ipv6 = Ipv6ListSerializer(read_only=True, many=True)
#     # TODO : use serializer.RelatedField to avoid duplicate code
#     mac_address = serializers.SerializerMethodField('get_macaddress')
#     domain = serializers.SerializerMethodField('get_dns')
#     extension = serializers.SerializerMethodField('get_interface_extension')
# 
#     class Meta:
#         model = Interface
#         fields = ('ipv4', 'ipv6', 'mac_address', 'domain', 'extension')
# 
#     @staticmethod
#     def get_dns(obj):
#         """ The name of the associated DNS object """
#         return obj.domain.name
# 
#     @staticmethod
#     def get_interface_extension(obj):
#         """ The name of the associated Extension object """
#         return obj.domain.extension.name
# 
#     @staticmethod
#     def get_macaddress(obj):
#         """ The string representation of the associated MAC address """
#         return str(obj.mac_address)
# 
# 
# class ExtensionNameField(serializers.RelatedField):
#     """ Serializer for Extension object field """
# 
#     def to_representation(self, value):
#         return value.name
# 
#     def to_internal_value(self, data):
#         pass
# 
# 
# class TypeSerializer(serializers.ModelSerializer):
#     """ Serializer for an IpType object. Use SerializerMethodField to
#     get ForeignKey values """
# 
#     extension = ExtensionNameField(read_only=True)
#     ouverture_ports_tcp_in = serializers\
#         .SerializerMethodField('get_port_policy_input_tcp')
#     ouverture_ports_tcp_out = serializers\
#         .SerializerMethodField('get_port_policy_output_tcp')
#     ouverture_ports_udp_in = serializers\
#         .SerializerMethodField('get_port_policy_input_udp')
#     ouverture_ports_udp_out = serializers\
#         .SerializerMethodField('get_port_policy_output_udp')
# 
#     class Meta:
#         model = IpType
#         fields = ('type', 'extension', 'domaine_ip_start', 'domaine_ip_stop',
#                   'prefix_v6',
#                   'ouverture_ports_tcp_in', 'ouverture_ports_tcp_out',
#                   'ouverture_ports_udp_in', 'ouverture_ports_udp_out',)
# 
#     @staticmethod
#     def get_port_policy(obj, protocole, io):
#         """ Generic utility function to get the policy for a given
#         port, protocole and IN or OUT """
#         if obj.ouverture_ports is None:
#             return []
#         return map(
#             str,
#             obj.ouverture_ports.ouvertureport_set.filter(
#                 protocole=protocole
#             ).filter(io=io)
#         )
# 
#     def get_port_policy_input_tcp(self, obj):
#         """Renvoie la liste des ports ouverts en entrée tcp"""
#         return self.get_port_policy(obj, OuverturePort.TCP, OuverturePort.IN)
# 
#     def get_port_policy_output_tcp(self, obj):
#         """Renvoie la liste des ports ouverts en sortie tcp"""
#         return self.get_port_policy(obj, OuverturePort.TCP, OuverturePort.OUT)
# 
#     def get_port_policy_input_udp(self, obj):
#         """Renvoie la liste des ports ouverts en entrée udp"""
#         return self.get_port_policy(obj, OuverturePort.UDP, OuverturePort.IN)
# 
#     def get_port_policy_output_udp(self, obj):
#         """Renvoie la liste des ports ouverts en sortie udp"""
#         return self.get_port_policy(obj, OuverturePort.UDP, OuverturePort.OUT)
# 
# 
# class ExtensionSerializer(serializers.ModelSerializer):
#     """Serialisation d'une extension : origin_ip et la zone sont
#     des foreign_key donc evalués en get_..."""
#     origin = serializers.SerializerMethodField('get_origin_ip')
#     zone_entry = serializers.SerializerMethodField('get_zone_name')
#     soa = serializers.SerializerMethodField('get_soa_data')
# 
#     class Meta:
#         model = Extension
#         fields = ('name', 'origin', 'origin_v6', 'zone_entry', 'soa')
# 
#     @staticmethod
#     def get_origin_ip(obj):
#         """ The IP of the associated origin for the zone """
#         return obj.origin.ipv4
# 
#     @staticmethod
#     def get_zone_name(obj):
#         """ The name of the associated zone """
#         return str(obj.dns_entry)
# 
#     @staticmethod
#     def get_soa_data(obj):
#         """ The representation of the associated SOA """
#         return {'mail': obj.soa.dns_soa_mail, 'param': obj.soa.dns_soa_param}
# 
# 
# class MxSerializer(serializers.ModelSerializer):
#     """Serialisation d'un MX, evaluation du nom, de la zone
#     et du serveur cible, etant des foreign_key"""
#     name = serializers.SerializerMethodField('get_entry_name')
#     zone = serializers.SerializerMethodField('get_zone_name')
#     mx_entry = serializers.SerializerMethodField('get_mx_name')
# 
#     class Meta:
#         model = Mx
#         fields = ('zone', 'priority', 'name', 'mx_entry')
# 
#     @staticmethod
#     def get_entry_name(obj):
#         """ The name of the DNS MX entry """
#         return str(obj.name)
# 
#     @staticmethod
#     def get_zone_name(obj):
#         """ The name of the associated zone of the MX record """
#         return obj.zone.name
# 
#     @staticmethod
#     def get_mx_name(obj):
#         """ The string representation of the entry to add to the DNS """
#         return str(obj.dns_entry)
# 
# 
# class TxtSerializer(serializers.ModelSerializer):
#     """Serialisation d'un txt : zone cible et l'entrée txt
#     sont evaluées à part"""
#     zone = serializers.SerializerMethodField('get_zone_name')
#     txt_entry = serializers.SerializerMethodField('get_txt_name')
# 
#     class Meta:
#         model = Txt
#         fields = ('zone', 'txt_entry', 'field1', 'field2')
# 
#     @staticmethod
#     def get_zone_name(obj):
#         """ The name of the associated zone """
#         return str(obj.zone.name)
# 
#     @staticmethod
#     def get_txt_name(obj):
#         """ The string representation of the entry to add to the DNS """
#         return str(obj.dns_entry)
# 
# 
# class SrvSerializer(serializers.ModelSerializer):
#     """Serialisation d'un srv : zone cible et l'entrée txt"""
#     extension = serializers.SerializerMethodField('get_extension_name')
#     srv_entry = serializers.SerializerMethodField('get_srv_name')
# 
#     class Meta:
#         model = Srv
#         fields = (
#             'service',
#             'protocole',
#             'extension',
#             'ttl',
#             'priority',
#             'weight',
#             'port',
#             'target',
#             'srv_entry'
#         )
# 
#     @staticmethod
#     def get_extension_name(obj):
#         """ The name of the associated extension """
#         return str(obj.extension.name)
# 
#     @staticmethod
#     def get_srv_name(obj):
#         """ The string representation of the entry to add to the DNS """
#         return str(obj.dns_entry)
# 
# 
# class NsSerializer(serializers.ModelSerializer):
#     """Serialisation d'un NS : la zone, l'entrée ns complète et le serveur
#     ns sont évalués à part"""
#     zone = serializers.SerializerMethodField('get_zone_name')
#     ns = serializers.SerializerMethodField('get_domain_name')
#     ns_entry = serializers.SerializerMethodField('get_text_name')
# 
#     class Meta:
#         model = Ns
#         fields = ('zone', 'ns', 'ns_entry')
# 
#     @staticmethod
#     def get_zone_name(obj):
#         """ The name of the associated zone """
#         return obj.zone.name
# 
#     @staticmethod
#     def get_domain_name(obj):
#         """ The name of the associated NS target """
#         return str(obj.ns)
# 
#     @staticmethod
#     def get_text_name(obj):
#         """ The string representation of the entry to add to the DNS """
#         return str(obj.dns_entry)
# 
# 
# class DomainSerializer(serializers.ModelSerializer):
#     """Serialisation d'un domain, extension, cname sont des foreign_key,
#     et l'entrée complète, sont évalués à part"""
#     extension = serializers.SerializerMethodField('get_zone_name')
#     cname = serializers.SerializerMethodField('get_alias_name')
#     cname_entry = serializers.SerializerMethodField('get_cname_name')
# 
#     class Meta:
#         model = Domain
#         fields = ('name', 'extension', 'cname', 'cname_entry')
# 
#     @staticmethod
#     def get_zone_name(obj):
#         """ The name of the associated zone """
#         return obj.extension.name
# 
#     @staticmethod
#     def get_alias_name(obj):
#         """ The name of the associated alias """
#         return str(obj.cname)
# 
#     @staticmethod
#     def get_cname_name(obj):
#         """ The name of the associated CNAME target """
#         return str(obj.dns_entry)
# 
# 
# class ServicesSerializer(serializers.ModelSerializer):
#     """Evaluation d'un Service, et serialisation"""
#     server = serializers.SerializerMethodField('get_server_name')
#     service = serializers.SerializerMethodField('get_service_name')
#     need_regen = serializers.SerializerMethodField('get_regen_status')
# 
#     class Meta:
#         model = Service_link
#         fields = ('server', 'service', 'need_regen')
# 
#     @staticmethod
#     def get_server_name(obj):
#         """ The name of the associated server """
#         return str(obj.server.domain.name)
# 
#     @staticmethod
#     def get_service_name(obj):
#         """ The name of the service name """
#         return str(obj.service)
# 
#     @staticmethod
#     def get_regen_status(obj):
#         """ The string representation of the regen status """
#         return obj.need_regen()
# 
# 
# class OuverturePortsSerializer(serializers.Serializer):
#     """Serialisation de l'ouverture des ports"""
#     ipv4 = serializers.SerializerMethodField()
#     ipv6 = serializers.SerializerMethodField()
# 
#     def create(self, validated_data):
#         """ Creates a new object based on the un-serialized data.
#         Used to implement an abstract inherited method """
#         pass
# 
#     def update(self, instance, validated_data):
#         """ Updates an object based on the un-serialized data.
#         Used to implement an abstract inherited method """
#         pass
# 
#     @staticmethod
#     def get_ipv4():
#         """ The representation of the policy for the IPv4 addresses """
#         return {
#             i.ipv4.ipv4: {
#                 "tcp_in": [j.tcp_ports_in() for j in i.port_lists.all()],
#                 "tcp_out": [j.tcp_ports_out()for j in i.port_lists.all()],
#                 "udp_in": [j.udp_ports_in() for j in i.port_lists.all()],
#                 "udp_out": [j.udp_ports_out() for j in i.port_lists.all()],
#             }
#             for i in Interface.objects.all() if i.ipv4
#         }
# 
#     @staticmethod
#     def get_ipv6():
#         """ The representation of the policy for the IPv6 addresses """
#         return {
#             i.ipv6: {
#                 "tcp_in": [j.tcp_ports_in() for j in i.port_lists.all()],
#                 "tcp_out": [j.tcp_ports_out()for j in i.port_lists.all()],
#                 "udp_in": [j.udp_ports_in() for j in i.port_lists.all()],
#                 "udp_out": [j.udp_ports_out() for j in i.port_lists.all()],
#             }
#             for i in Interface.objects.all() if i.ipv6
#         }