views.py 23.7 KB
Newer Older
1
# -*- mode: python; coding: utf-8 -*-
Maël Kervella's avatar
Maël Kervella committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
# 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 Maël 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.

22
"""Defines the views of the API
Maël Kervella's avatar
Maël Kervella committed
23

24 25 26
All views inherits the `rest_framework.views.APIview` to respect the
REST API requirements such as dealing with HTTP status code, format of
the response (JSON or other), the CSRF exempting, ...
Maël Kervella's avatar
Maël Kervella committed
27
"""
Maël Kervella's avatar
Maël Kervella committed
28

29
import datetime
Maël Kervella's avatar
Maël Kervella committed
30

31
from django.conf import settings
32
from django.db.models import Q
erdnaxe's avatar
erdnaxe committed
33
from rest_framework import viewsets, generics, views
34
from rest_framework.authtoken.models import Token
erdnaxe's avatar
erdnaxe committed
35
from rest_framework.authtoken.views import ObtainAuthToken
Maël Kervella's avatar
Maël Kervella committed
36 37
from rest_framework.response import Response

Maël Kervella's avatar
Maël Kervella committed
38 39 40 41 42
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
Maël Kervella's avatar
Maël Kervella committed
43
from re2o.utils import all_active_interfaces, all_has_access
Maël Kervella's avatar
Maël Kervella committed
44
from . import serializers
Maël Kervella's avatar
Maël Kervella committed
45
from .pagination import PageSizedPagination
46
from .permissions import ACLPermission
47

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

49
# COTISATIONS
50 51 52


class FactureViewSet(viewsets.ReadOnlyModelViewSet):
53 54
    """Exposes list and details of `cotisations.models.Facture` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
55 56
    queryset = cotisations.Facture.objects.all()
    serializer_class = serializers.FactureSerializer
57

58 59 60 61 62 63
class FactureViewSet(viewsets.ReadOnlyModelViewSet):
    """Exposes list and details of `cotisations.models.Facture` objects.
    """
    queryset = cotisations.BaseInvoice.objects.all()
    serializer_class = serializers.BaseInvoiceSerializer

64 65

class VenteViewSet(viewsets.ReadOnlyModelViewSet):
66 67
    """Exposes list and details of `cotisations.models.Vente` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
68 69
    queryset = cotisations.Vente.objects.all()
    serializer_class = serializers.VenteSerializer
70 71 72


class ArticleViewSet(viewsets.ReadOnlyModelViewSet):
73 74
    """Exposes list and details of `cotisations.models.Article` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
75 76
    queryset = cotisations.Article.objects.all()
    serializer_class = serializers.ArticleSerializer
77 78 79


class BanqueViewSet(viewsets.ReadOnlyModelViewSet):
80 81
    """Exposes list and details of `cotisations.models.Banque` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
82 83
    queryset = cotisations.Banque.objects.all()
    serializer_class = serializers.BanqueSerializer
84 85 86


class PaiementViewSet(viewsets.ReadOnlyModelViewSet):
87 88
    """Exposes list and details of `cotisations.models.Paiement` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
89 90
    queryset = cotisations.Paiement.objects.all()
    serializer_class = serializers.PaiementSerializer
91 92 93


class CotisationViewSet(viewsets.ReadOnlyModelViewSet):
94 95
    """Exposes list and details of `cotisations.models.Cotisation` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
96 97
    queryset = cotisations.Cotisation.objects.all()
    serializer_class = serializers.CotisationSerializer
98 99


100
# MACHINES
101 102 103


class MachineViewSet(viewsets.ReadOnlyModelViewSet):
104 105
    """Exposes list and details of `machines.models.Machine` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
106 107
    queryset = machines.Machine.objects.all()
    serializer_class = serializers.MachineSerializer
108 109 110


class MachineTypeViewSet(viewsets.ReadOnlyModelViewSet):
111 112
    """Exposes list and details of `machines.models.MachineType` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
113 114
    queryset = machines.MachineType.objects.all()
    serializer_class = serializers.MachineTypeSerializer
115 116 117


class IpTypeViewSet(viewsets.ReadOnlyModelViewSet):
118 119
    """Exposes list and details of `machines.models.IpType` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
120 121
    queryset = machines.IpType.objects.all()
    serializer_class = serializers.IpTypeSerializer
122 123 124


class VlanViewSet(viewsets.ReadOnlyModelViewSet):
125 126
    """Exposes list and details of `machines.models.Vlan` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
127 128
    queryset = machines.Vlan.objects.all()
    serializer_class = serializers.VlanSerializer
129 130 131


class NasViewSet(viewsets.ReadOnlyModelViewSet):
132 133
    """Exposes list and details of `machines.models.Nas` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
134 135
    queryset = machines.Nas.objects.all()
    serializer_class = serializers.NasSerializer
136 137 138


class SOAViewSet(viewsets.ReadOnlyModelViewSet):
139 140
    """Exposes list and details of `machines.models.SOA` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
141 142
    queryset = machines.SOA.objects.all()
    serializer_class = serializers.SOASerializer
143 144 145


class ExtensionViewSet(viewsets.ReadOnlyModelViewSet):
146 147
    """Exposes list and details of `machines.models.Extension` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
148 149
    queryset = machines.Extension.objects.all()
    serializer_class = serializers.ExtensionSerializer
150 151 152


class MxViewSet(viewsets.ReadOnlyModelViewSet):
153 154
    """Exposes list and details of `machines.models.Mx` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
155 156
    queryset = machines.Mx.objects.all()
    serializer_class = serializers.MxSerializer
157 158 159


class NsViewSet(viewsets.ReadOnlyModelViewSet):
160 161
    """Exposes list and details of `machines.models.Ns` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
162 163
    queryset = machines.Ns.objects.all()
    serializer_class = serializers.NsSerializer
164 165 166


class TxtViewSet(viewsets.ReadOnlyModelViewSet):
167 168
    """Exposes list and details of `machines.models.Txt` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
169 170
    queryset = machines.Txt.objects.all()
    serializer_class = serializers.TxtSerializer
Charlie Jacomme's avatar
Charlie Jacomme committed
171

erdnaxe's avatar
erdnaxe committed
172

Charlie Jacomme's avatar
Charlie Jacomme committed
173 174 175 176 177
class DNameViewSet(viewsets.ReadOnlyModelViewSet):
    """Exposes list and details of `machines.models.DName` objects.
    """
    queryset = machines.DName.objects.all()
    serializer_class = serializers.DNameSerializer
178 179 180


class SrvViewSet(viewsets.ReadOnlyModelViewSet):
181 182
    """Exposes list and details of `machines.models.Srv` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
183 184
    queryset = machines.Srv.objects.all()
    serializer_class = serializers.SrvSerializer
185 186


187 188 189 190 191 192 193
class SshFpViewSet(viewsets.ReadOnlyModelViewSet):
    """Exposes list and details of `machines.models.SshFp` objects.
    """
    queryset = machines.SshFp.objects.all()
    serializer_class = serializers.SshFpSerializer


194
class InterfaceViewSet(viewsets.ReadOnlyModelViewSet):
195 196
    """Exposes list and details of `machines.models.Interface` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
197 198
    queryset = machines.Interface.objects.all()
    serializer_class = serializers.InterfaceSerializer
199 200 201


class Ipv6ListViewSet(viewsets.ReadOnlyModelViewSet):
202 203
    """Exposes list and details of `machines.models.Ipv6List` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
204 205
    queryset = machines.Ipv6List.objects.all()
    serializer_class = serializers.Ipv6ListSerializer
206 207 208


class DomainViewSet(viewsets.ReadOnlyModelViewSet):
209 210
    """Exposes list and details of `machines.models.Domain` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
211 212
    queryset = machines.Domain.objects.all()
    serializer_class = serializers.DomainSerializer
213 214 215


class IpListViewSet(viewsets.ReadOnlyModelViewSet):
216 217
    """Exposes list and details of `machines.models.IpList` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
218 219
    queryset = machines.IpList.objects.all()
    serializer_class = serializers.IpListSerializer
220 221 222


class ServiceViewSet(viewsets.ReadOnlyModelViewSet):
223 224
    """Exposes list and details of `machines.models.Service` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
225 226
    queryset = machines.Service.objects.all()
    serializer_class = serializers.ServiceSerializer
227 228


229
class ServiceLinkViewSet(viewsets.ReadOnlyModelViewSet):
230 231
    """Exposes list and details of `machines.models.Service_link` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
232 233
    queryset = machines.Service_link.objects.all()
    serializer_class = serializers.ServiceLinkSerializer
234 235 236


class OuverturePortListViewSet(viewsets.ReadOnlyModelViewSet):
237 238 239
    """Exposes list and details of `machines.models.OuverturePortList`
    objects.
    """
Maël Kervella's avatar
Maël Kervella committed
240 241
    queryset = machines.OuverturePortList.objects.all()
    serializer_class = serializers.OuverturePortListSerializer
242 243 244


class OuverturePortViewSet(viewsets.ReadOnlyModelViewSet):
245 246
    """Exposes list and details of `machines.models.OuverturePort` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
247 248
    queryset = machines.OuverturePort.objects.all()
    serializer_class = serializers.OuverturePortSerializer
249 250


251 252 253 254 255 256 257
class RoleViewSet(viewsets.ReadOnlyModelViewSet):
    """Exposes list and details of `machines.models.Machine` objects.
    """
    queryset = machines.Role.objects.all()
    serializer_class = serializers.RoleSerializer


258
# PREFERENCES
259 260
# Those views differ a bit because there is only one object
# to display, so we don't bother with the listing part
261

262
class OptionalUserView(generics.RetrieveAPIView):
263 264
    """Exposes details of `preferences.models.` settings.
    """
erdnaxe's avatar
erdnaxe committed
265 266
    permission_classes = (ACLPermission,)
    perms_map = {'GET': [preferences.OptionalUser.can_view_all]}
267 268 269 270 271 272 273
    serializer_class = serializers.OptionalUserSerializer

    def get_object(self):
        return preferences.OptionalUser.objects.first()


class OptionalMachineView(generics.RetrieveAPIView):
274 275
    """Exposes details of `preferences.models.OptionalMachine` settings.
    """
erdnaxe's avatar
erdnaxe committed
276 277
    permission_classes = (ACLPermission,)
    perms_map = {'GET': [preferences.OptionalMachine.can_view_all]}
278 279 280 281 282 283 284
    serializer_class = serializers.OptionalMachineSerializer

    def get_object(self):
        return preferences.OptionalMachine.objects.first()


class OptionalTopologieView(generics.RetrieveAPIView):
285 286
    """Exposes details of `preferences.models.OptionalTopologie` settings.
    """
erdnaxe's avatar
erdnaxe committed
287 288
    permission_classes = (ACLPermission,)
    perms_map = {'GET': [preferences.OptionalTopologie.can_view_all]}
289 290 291 292 293 294
    serializer_class = serializers.OptionalTopologieSerializer

    def get_object(self):
        return preferences.OptionalTopologie.objects.first()


295 296 297 298 299 300 301 302 303 304 305
class RadiusOptionView(generics.RetrieveAPIView):
    """Exposes details of `preferences.models.OptionalTopologie` settings.
    """
    permission_classes = (ACLPermission,)
    perms_map = {'GET': [preferences.RadiusOption.can_view_all]}
    serializer_class = serializers.RadiusOptionSerializer

    def get_object(self):
        return preferences.RadiusOption.objects.first()


306
class GeneralOptionView(generics.RetrieveAPIView):
307 308
    """Exposes details of `preferences.models.GeneralOption` settings.
    """
erdnaxe's avatar
erdnaxe committed
309 310
    permission_classes = (ACLPermission,)
    perms_map = {'GET': [preferences.GeneralOption.can_view_all]}
311 312 313 314 315 316
    serializer_class = serializers.GeneralOptionSerializer

    def get_object(self):
        return preferences.GeneralOption.objects.first()


317
class HomeServiceViewSet(viewsets.ReadOnlyModelViewSet):
318 319
    """Exposes list and details of `preferences.models.Service` objects.
    """
320
    queryset = preferences.Service.objects.all()
321
    serializer_class = serializers.HomeServiceSerializer
322 323 324


class AssoOptionView(generics.RetrieveAPIView):
325 326
    """Exposes details of `preferences.models.AssoOption` settings.
    """
erdnaxe's avatar
erdnaxe committed
327 328
    permission_classes = (ACLPermission,)
    perms_map = {'GET': [preferences.AssoOption.can_view_all]}
329 330 331 332 333 334 335
    serializer_class = serializers.AssoOptionSerializer

    def get_object(self):
        return preferences.AssoOption.objects.first()


class HomeOptionView(generics.RetrieveAPIView):
336 337
    """Exposes details of `preferences.models.HomeOption` settings.
    """
erdnaxe's avatar
erdnaxe committed
338 339
    permission_classes = (ACLPermission,)
    perms_map = {'GET': [preferences.HomeOption.can_view_all]}
340 341 342 343 344 345 346
    serializer_class = serializers.HomeOptionSerializer

    def get_object(self):
        return preferences.HomeOption.objects.first()


class MailMessageOptionView(generics.RetrieveAPIView):
347 348
    """Exposes details of `preferences.models.MailMessageOption` settings.
    """
erdnaxe's avatar
erdnaxe committed
349 350
    permission_classes = (ACLPermission,)
    perms_map = {'GET': [preferences.MailMessageOption.can_view_all]}
351 352 353 354
    serializer_class = serializers.MailMessageOptionSerializer

    def get_object(self):
        return preferences.MailMessageOption.objects.first()
355 356


357
# TOPOLOGIE
358 359 360


class StackViewSet(viewsets.ReadOnlyModelViewSet):
361 362
    """Exposes list and details of `topologie.models.Stack` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
363 364
    queryset = topologie.Stack.objects.all()
    serializer_class = serializers.StackSerializer
365 366 367


class AccessPointViewSet(viewsets.ReadOnlyModelViewSet):
368 369
    """Exposes list and details of `topologie.models.AccessPoint` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
370 371
    queryset = topologie.AccessPoint.objects.all()
    serializer_class = serializers.AccessPointSerializer
372 373 374


class SwitchViewSet(viewsets.ReadOnlyModelViewSet):
375 376
    """Exposes list and details of `topologie.models.Switch` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
377 378
    queryset = topologie.Switch.objects.all()
    serializer_class = serializers.SwitchSerializer
379 380


381 382 383 384 385 386 387
class ServerViewSet(viewsets.ReadOnlyModelViewSet):
    """Exposes list and details of `topologie.models.Server` objects.
    """
    queryset = topologie.Server.objects.all()
    serializer_class = serializers.ServerSerializer


388
class ModelSwitchViewSet(viewsets.ReadOnlyModelViewSet):
389 390
    """Exposes list and details of `topologie.models.ModelSwitch` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
391 392
    queryset = topologie.ModelSwitch.objects.all()
    serializer_class = serializers.ModelSwitchSerializer
393 394 395


class ConstructorSwitchViewSet(viewsets.ReadOnlyModelViewSet):
396 397 398
    """Exposes list and details of `topologie.models.ConstructorSwitch`
    objects.
    """
Maël Kervella's avatar
Maël Kervella committed
399 400
    queryset = topologie.ConstructorSwitch.objects.all()
    serializer_class = serializers.ConstructorSwitchSerializer
401 402 403


class SwitchBayViewSet(viewsets.ReadOnlyModelViewSet):
404 405
    """Exposes list and details of `topologie.models.SwitchBay` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
406 407
    queryset = topologie.SwitchBay.objects.all()
    serializer_class = serializers.SwitchBaySerializer
408 409 410


class BuildingViewSet(viewsets.ReadOnlyModelViewSet):
411 412
    """Exposes list and details of `topologie.models.Building` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
413 414
    queryset = topologie.Building.objects.all()
    serializer_class = serializers.BuildingSerializer
415 416 417


class SwitchPortViewSet(viewsets.ReadOnlyModelViewSet):
418 419
    """Exposes list and details of `topologie.models.Port` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
420 421
    queryset = topologie.Port.objects.all()
    serializer_class = serializers.SwitchPortSerializer
422 423


424 425 426 427 428 429 430
class PortProfileViewSet(viewsets.ReadOnlyModelViewSet):
    """Exposes list and details of `topologie.models.PortProfile` objects.
    """
    queryset = topologie.PortProfile.objects.all()
    serializer_class = serializers.PortProfileSerializer


431
class RoomViewSet(viewsets.ReadOnlyModelViewSet):
432 433
    """Exposes list and details of `topologie.models.Room` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
434 435
    queryset = topologie.Room.objects.all()
    serializer_class = serializers.RoomSerializer
436 437


438 439 440 441 442 443
class PortProfileViewSet(viewsets.ReadOnlyModelViewSet):
    """Exposes list and details of `topologie.models.PortProfile` objects.
    """
    queryset = topologie.PortProfile.objects.all()
    serializer_class = serializers.PortProfileSerializer

erdnaxe's avatar
erdnaxe committed
444

445
# USER
446 447


Maël Kervella's avatar
Maël Kervella committed
448
class UserViewSet(viewsets.ReadOnlyModelViewSet):
449 450
    """Exposes list and details of `users.models.Users` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
451 452
    queryset = users.User.objects.all()
    serializer_class = serializers.UserSerializer
Maël Kervella's avatar
Maël Kervella committed
453

erdnaxe's avatar
erdnaxe committed
454

455 456 457
class HomeCreationViewSet(viewsets.ReadOnlyModelViewSet):
    """Exposes infos of `users.models.Users` objects to create homes.
    """
detraz's avatar
detraz committed
458
    queryset = users.User.objects.exclude(Q(state=users.User.STATE_DISABLED) | Q(state=users.User.STATE_NOT_YET_ACTIVE))
detraz's avatar
detraz committed
459 460 461 462 463 464 465 466 467 468 469 470 471
    serializer_class = serializers.BasicUserSerializer


class NormalUserViewSet(viewsets.ReadOnlyModelViewSet):
    """Exposes infos of `users.models.Users`without specific rights objects."""
    queryset = users.User.objects.exclude(groups__listright__critical=True).distinct()
    serializer_class = serializers.BasicUserSerializer


class CriticalUserViewSet(viewsets.ReadOnlyModelViewSet):
    """Exposes infos of `users.models.Users`without specific rights objects."""
    queryset = users.User.objects.filter(groups__listright__critical=True).distinct()
    serializer_class = serializers.BasicUserSerializer
Maël Kervella's avatar
Maël Kervella committed
472

erdnaxe's avatar
erdnaxe committed
473

Maël Kervella's avatar
Maël Kervella committed
474
class ClubViewSet(viewsets.ReadOnlyModelViewSet):
475 476
    """Exposes list and details of `users.models.Club` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
477 478
    queryset = users.Club.objects.all()
    serializer_class = serializers.ClubSerializer
Maël Kervella's avatar
Maël Kervella committed
479 480 481


class AdherentViewSet(viewsets.ReadOnlyModelViewSet):
482 483
    """Exposes list and details of `users.models.Adherent` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
484 485
    queryset = users.Adherent.objects.all()
    serializer_class = serializers.AdherentSerializer
Maël Kervella's avatar
Maël Kervella committed
486 487 488


class ServiceUserViewSet(viewsets.ReadOnlyModelViewSet):
489 490
    """Exposes list and details of `users.models.ServiceUser` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
491 492
    queryset = users.ServiceUser.objects.all()
    serializer_class = serializers.ServiceUserSerializer
Maël Kervella's avatar
Maël Kervella committed
493 494 495


class SchoolViewSet(viewsets.ReadOnlyModelViewSet):
496 497
    """Exposes list and details of `users.models.School` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
498 499
    queryset = users.School.objects.all()
    serializer_class = serializers.SchoolSerializer
Maël Kervella's avatar
Maël Kervella committed
500 501 502


class ListRightViewSet(viewsets.ReadOnlyModelViewSet):
503 504
    """Exposes list and details of `users.models.ListRight` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
505 506
    queryset = users.ListRight.objects.all()
    serializer_class = serializers.ListRightSerializer
Maël Kervella's avatar
Maël Kervella committed
507 508 509


class ShellViewSet(viewsets.ReadOnlyModelViewSet):
510 511
    """Exposes list and details of `users.models.ListShell` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
512 513
    queryset = users.ListShell.objects.all()
    serializer_class = serializers.ShellSerializer
Maël Kervella's avatar
Maël Kervella committed
514 515 516


class BanViewSet(viewsets.ReadOnlyModelViewSet):
517 518
    """Exposes list and details of `users.models.Ban` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
519 520
    queryset = users.Ban.objects.all()
    serializer_class = serializers.BanSerializer
Maël Kervella's avatar
Maël Kervella committed
521 522 523


class WhitelistViewSet(viewsets.ReadOnlyModelViewSet):
524 525
    """Exposes list and details of `users.models.Whitelist` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
526 527
    queryset = users.Whitelist.objects.all()
    serializer_class = serializers.WhitelistSerializer
Maël Kervella's avatar
Maël Kervella committed
528

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

530 531
class EMailAddressViewSet(viewsets.ReadOnlyModelViewSet):
    """Exposes list and details of `users.models.EMailAddress` objects.
532
    """
533 534
    serializer_class = serializers.EMailAddressSerializer
    queryset = users.EMailAddress.objects.none()
535 536 537

    def get_queryset(self):
        if preferences.OptionalUser.get_cached_value(
erdnaxe's avatar
erdnaxe committed
538
                'local_email_accounts_enabled'):
539
            return (users.EMailAddress.objects
540 541
                    .filter(user__local_email_enabled=True))
        else:
542
            return users.EMailAddress.objects.none()
543 544


545
# SERVICE REGEN
546 547


548
class ServiceRegenViewSet(viewsets.ModelViewSet):
549 550
    """Exposes list and details of the services to regen
    """
551 552 553 554 555 556 557 558 559 560 561 562 563
    serializer_class = serializers.ServiceRegenSerializer

    def get_queryset(self):
        queryset = machines.Service_link.objects.select_related(
            'server__domain'
        ).select_related(
            'service'
        )
        if 'hostname' in self.request.GET:
            hostname = self.request.GET['hostname']
            queryset = queryset.filter(server__domain__name__iexact=hostname)
        return queryset

564 565 566
# Config des switches

class SwitchPortView(generics.ListAPIView):
Gabriel Detraz's avatar
Gabriel Detraz committed
567 568
    """Output each port of a switch, to be serialized with
    additionnal informations (profiles etc)
569
    """
570 571
    queryset = topologie.Switch.objects.all().select_related("switchbay").select_related("model__constructor").prefetch_related("ports__custom_profile__vlan_tagged").prefetch_related("ports__custom_profile__vlan_untagged").prefetch_related("ports__machine_interface__domain__extension").prefetch_related("ports__room")

572 573
    serializer_class = serializers.SwitchPortSerializer

574 575 576
# Rappel fin adhésion

class ReminderView(generics.ListAPIView):
Gabriel Detraz's avatar
Gabriel Detraz committed
577
    """Output for users to remind an end of their subscription.
578 579 580 581
    """
    queryset = preferences.Reminder.objects.all()
    serializer_class = serializers.ReminderSerializer

582

583
class RoleView(generics.ListAPIView):
Gabriel Detraz's avatar
Gabriel Detraz committed
584
    """Output of roles for each server
585 586 587 588 589
    """
    queryset = machines.Role.objects.all().prefetch_related('servers')
    serializer_class = serializers.RoleSerializer


590
# LOCAL EMAILS
591 592


593 594
class LocalEmailUsersView(generics.ListAPIView):
    """Exposes all the aliases of the users that activated the internal address
595
    """
596
    serializer_class = serializers.LocalEmailUsersSerializer
597

598 599
    def get_queryset(self):
        if preferences.OptionalUser.get_cached_value(
erdnaxe's avatar
erdnaxe committed
600
                'local_email_accounts_enabled'):
601 602 603 604
            return (users.User.objects
                    .filter(local_email_enabled=True))
        else:
            return users.User.objects.none()
605 606


607
# DHCP
Maël Kervella's avatar
Maël Kervella committed
608

609

Maël Kervella's avatar
Maël Kervella committed
610
class HostMacIpView(generics.ListAPIView):
611 612 613
    """Exposes the associations between hostname, mac address and IPv4 in
    order to build the DHCP lease files.
    """
Maël Kervella's avatar
Maël Kervella committed
614 615 616 617
    queryset = all_active_interfaces()
    serializer_class = serializers.HostMacIpSerializer


erdnaxe's avatar
erdnaxe committed
618
# Firewall
619 620 621 622 623

class SubnetPortsOpenView(generics.ListAPIView):
    queryset = machines.IpType.objects.all()
    serializer_class = serializers.SubnetPortsOpenSerializer

erdnaxe's avatar
erdnaxe committed
624

625 626 627 628
class InterfacePortsOpenView(generics.ListAPIView):
    queryset = machines.Interface.objects.filter(port_lists__isnull=False).distinct()
    serializer_class = serializers.InterfacePortsOpenSerializer

erdnaxe's avatar
erdnaxe committed
629

630
# DNS
631

632

633
class DNSZonesView(generics.ListAPIView):
634
    """Exposes the detailed information about each extension (hostnames,
635 636
    IPs, DNS records, etc.) in order to build the DNS zone files.
    """
637 638 639 640 641 642 643 644
    queryset = (machines.Extension.objects
                .prefetch_related('soa')
                .prefetch_related('ns_set').prefetch_related('ns_set__ns')
                .prefetch_related('origin')
                .prefetch_related('mx_set').prefetch_related('mx_set__name')
                .prefetch_related('txt_set')
                .prefetch_related('srv_set').prefetch_related('srv_set__target')
                .all())
645 646
    serializer_class = serializers.DNSZonesSerializer

erdnaxe's avatar
erdnaxe committed
647

648 649 650 651 652 653 654 655
class DNSReverseZonesView(generics.ListAPIView):
    """Exposes the detailed information about each extension (hostnames, 
    IPs, DNS records, etc.) in order to build the DNS zone files.
    """
    queryset = (machines.IpType.objects.all())
    serializer_class = serializers.DNSReverseZonesSerializer


656
# MAILING
Maël Kervella's avatar
Maël Kervella committed
657 658 659


class StandardMailingView(views.APIView):
660 661 662
    """Exposes list and details of standard mailings (name and members) in
    order to building the corresponding mailing lists.
    """
Maël Kervella's avatar
Maël Kervella committed
663
    pagination_class = PageSizedPagination
erdnaxe's avatar
erdnaxe committed
664 665
    permission_classes = (ACLPermission,)
    perms_map = {'GET': [users.User.can_view_all]}
Maël Kervella's avatar
Maël Kervella committed
666 667

    def get(self, request, format=None):
668
        adherents_data = serializers.MailingMemberSerializer(all_has_access(), many=True).data
Maël Kervella's avatar
Maël Kervella committed
669 670 671 672 673 674 675
        data = [{'name': 'adherents', 'members': adherents_data}]
        paginator = self.pagination_class()
        paginator.paginate_queryset(data, request)
        return paginator.get_paginated_response(data)


class ClubMailingView(generics.ListAPIView):
676 677 678
    """Exposes list and details of club mailings (name, members and admins) in
    order to build the corresponding mailing lists.
    """
Maël Kervella's avatar
Maël Kervella committed
679 680 681 682
    queryset = users.Club.objects.all()
    serializer_class = serializers.MailingSerializer


683 684 685
# TOKEN AUTHENTICATION


686
class ObtainExpiringAuthToken(ObtainAuthToken):
687 688 689 690 691 692
    """Exposes a view to obtain a authentication token.

    This view as the same behavior as the
    `rest_framework.auth_token.views.ObtainAuthToken` view except that the
    expiration time is send along with the token as an addtional information.
    """
erdnaxe's avatar
erdnaxe committed
693

694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
    def post(self, request, *args, **kwargs):
        serializer = self.serializer_class(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = serializer.validated_data['user']
        token, created = Token.objects.get_or_create(user=user)

        token_duration = datetime.timedelta(
            seconds=settings.API_TOKEN_DURATION
        )
        utc_now = datetime.datetime.now(datetime.timezone.utc)
        if not created and token.created < utc_now - token_duration:
            token.delete()
            token = Token.objects.create(user=user)
            token.created = datetime.datetime.utcnow()
            token.save()

        return Response({
            'token': token.key,
712
            'expiration': token.created + token_duration
713
        })