views.py 22.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
"""
28

29
import datetime
30

31
from django.conf import settings
32
from django.db.models import Q
Alexandre Iooss's avatar
Alexandre Iooss committed
33
from rest_framework import viewsets, generics, views
34
from rest_framework.authtoken.models import Token
Alexandre Iooss's avatar
Alexandre Iooss 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
43
from re2o.utils import all_active_interfaces, all_has_access
Maël Kervella's avatar
Maël Kervella committed
44
from . import serializers
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


class VenteViewSet(viewsets.ReadOnlyModelViewSet):
60 61
    """Exposes list and details of `cotisations.models.Vente` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
62 63
    queryset = cotisations.Vente.objects.all()
    serializer_class = serializers.VenteSerializer
64 65 66


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


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


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


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


94
# MACHINES
95 96 97


class MachineViewSet(viewsets.ReadOnlyModelViewSet):
98 99
    """Exposes list and details of `machines.models.Machine` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
100 101
    queryset = machines.Machine.objects.all()
    serializer_class = serializers.MachineSerializer
102 103 104


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


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


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


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


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


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


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


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


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

Alexandre Iooss's avatar
Alexandre Iooss committed
166

167 168 169 170 171
class DNameViewSet(viewsets.ReadOnlyModelViewSet):
    """Exposes list and details of `machines.models.DName` objects.
    """
    queryset = machines.DName.objects.all()
    serializer_class = serializers.DNameSerializer
172 173 174


class SrvViewSet(viewsets.ReadOnlyModelViewSet):
175 176
    """Exposes list and details of `machines.models.Srv` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
177 178
    queryset = machines.Srv.objects.all()
    serializer_class = serializers.SrvSerializer
179 180


181 182 183 184 185 186 187
class SshFpViewSet(viewsets.ReadOnlyModelViewSet):
    """Exposes list and details of `machines.models.SshFp` objects.
    """
    queryset = machines.SshFp.objects.all()
    serializer_class = serializers.SshFpSerializer


188
class InterfaceViewSet(viewsets.ReadOnlyModelViewSet):
189 190
    """Exposes list and details of `machines.models.Interface` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
191 192
    queryset = machines.Interface.objects.all()
    serializer_class = serializers.InterfaceSerializer
193 194 195


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


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


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


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


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


class OuverturePortListViewSet(viewsets.ReadOnlyModelViewSet):
231 232 233
    """Exposes list and details of `machines.models.OuverturePortList`
    objects.
    """
Maël Kervella's avatar
Maël Kervella committed
234 235
    queryset = machines.OuverturePortList.objects.all()
    serializer_class = serializers.OuverturePortListSerializer
236 237 238


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


245 246 247 248 249 250 251
class RoleViewSet(viewsets.ReadOnlyModelViewSet):
    """Exposes list and details of `machines.models.Machine` objects.
    """
    queryset = machines.Role.objects.all()
    serializer_class = serializers.RoleSerializer


252
# PREFERENCES
253 254
# Those views differ a bit because there is only one object
# to display, so we don't bother with the listing part
255

256
class OptionalUserView(generics.RetrieveAPIView):
257 258
    """Exposes details of `preferences.models.` settings.
    """
Alexandre Iooss's avatar
Alexandre Iooss committed
259 260
    permission_classes = (ACLPermission,)
    perms_map = {'GET': [preferences.OptionalUser.can_view_all]}
261 262 263 264 265 266 267
    serializer_class = serializers.OptionalUserSerializer

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


class OptionalMachineView(generics.RetrieveAPIView):
268 269
    """Exposes details of `preferences.models.OptionalMachine` settings.
    """
Alexandre Iooss's avatar
Alexandre Iooss committed
270 271
    permission_classes = (ACLPermission,)
    perms_map = {'GET': [preferences.OptionalMachine.can_view_all]}
272 273 274 275 276 277 278
    serializer_class = serializers.OptionalMachineSerializer

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


class OptionalTopologieView(generics.RetrieveAPIView):
279 280
    """Exposes details of `preferences.models.OptionalTopologie` settings.
    """
Alexandre Iooss's avatar
Alexandre Iooss committed
281 282
    permission_classes = (ACLPermission,)
    perms_map = {'GET': [preferences.OptionalTopologie.can_view_all]}
283 284 285 286 287 288 289
    serializer_class = serializers.OptionalTopologieSerializer

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


class GeneralOptionView(generics.RetrieveAPIView):
290 291
    """Exposes details of `preferences.models.GeneralOption` settings.
    """
Alexandre Iooss's avatar
Alexandre Iooss committed
292 293
    permission_classes = (ACLPermission,)
    perms_map = {'GET': [preferences.GeneralOption.can_view_all]}
294 295 296 297 298 299
    serializer_class = serializers.GeneralOptionSerializer

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


300
class HomeServiceViewSet(viewsets.ReadOnlyModelViewSet):
301 302
    """Exposes list and details of `preferences.models.Service` objects.
    """
303
    queryset = preferences.Service.objects.all()
304
    serializer_class = serializers.HomeServiceSerializer
305 306 307


class AssoOptionView(generics.RetrieveAPIView):
308 309
    """Exposes details of `preferences.models.AssoOption` settings.
    """
Alexandre Iooss's avatar
Alexandre Iooss committed
310 311
    permission_classes = (ACLPermission,)
    perms_map = {'GET': [preferences.AssoOption.can_view_all]}
312 313 314 315 316 317 318
    serializer_class = serializers.AssoOptionSerializer

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


class HomeOptionView(generics.RetrieveAPIView):
319 320
    """Exposes details of `preferences.models.HomeOption` settings.
    """
Alexandre Iooss's avatar
Alexandre Iooss committed
321 322
    permission_classes = (ACLPermission,)
    perms_map = {'GET': [preferences.HomeOption.can_view_all]}
323 324 325 326 327 328 329
    serializer_class = serializers.HomeOptionSerializer

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


class MailMessageOptionView(generics.RetrieveAPIView):
330 331
    """Exposes details of `preferences.models.MailMessageOption` settings.
    """
Alexandre Iooss's avatar
Alexandre Iooss committed
332 333
    permission_classes = (ACLPermission,)
    perms_map = {'GET': [preferences.MailMessageOption.can_view_all]}
334 335 336 337
    serializer_class = serializers.MailMessageOptionSerializer

    def get_object(self):
        return preferences.MailMessageOption.objects.first()
338 339


340
# TOPOLOGIE
341 342 343


class StackViewSet(viewsets.ReadOnlyModelViewSet):
344 345
    """Exposes list and details of `topologie.models.Stack` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
346 347
    queryset = topologie.Stack.objects.all()
    serializer_class = serializers.StackSerializer
348 349 350


class AccessPointViewSet(viewsets.ReadOnlyModelViewSet):
351 352
    """Exposes list and details of `topologie.models.AccessPoint` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
353 354
    queryset = topologie.AccessPoint.objects.all()
    serializer_class = serializers.AccessPointSerializer
355 356 357


class SwitchViewSet(viewsets.ReadOnlyModelViewSet):
358 359
    """Exposes list and details of `topologie.models.Switch` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
360 361
    queryset = topologie.Switch.objects.all()
    serializer_class = serializers.SwitchSerializer
362 363


364 365 366 367 368 369 370
class ServerViewSet(viewsets.ReadOnlyModelViewSet):
    """Exposes list and details of `topologie.models.Server` objects.
    """
    queryset = topologie.Server.objects.all()
    serializer_class = serializers.ServerSerializer


371
class ModelSwitchViewSet(viewsets.ReadOnlyModelViewSet):
372 373
    """Exposes list and details of `topologie.models.ModelSwitch` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
374 375
    queryset = topologie.ModelSwitch.objects.all()
    serializer_class = serializers.ModelSwitchSerializer
376 377 378


class ConstructorSwitchViewSet(viewsets.ReadOnlyModelViewSet):
379 380 381
    """Exposes list and details of `topologie.models.ConstructorSwitch`
    objects.
    """
Maël Kervella's avatar
Maël Kervella committed
382 383
    queryset = topologie.ConstructorSwitch.objects.all()
    serializer_class = serializers.ConstructorSwitchSerializer
384 385 386


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


class BuildingViewSet(viewsets.ReadOnlyModelViewSet):
394 395
    """Exposes list and details of `topologie.models.Building` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
396 397
    queryset = topologie.Building.objects.all()
    serializer_class = serializers.BuildingSerializer
398 399 400


class SwitchPortViewSet(viewsets.ReadOnlyModelViewSet):
401 402
    """Exposes list and details of `topologie.models.Port` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
403 404
    queryset = topologie.Port.objects.all()
    serializer_class = serializers.SwitchPortSerializer
405 406


407 408 409 410 411 412 413
class PortProfileViewSet(viewsets.ReadOnlyModelViewSet):
    """Exposes list and details of `topologie.models.PortProfile` objects.
    """
    queryset = topologie.PortProfile.objects.all()
    serializer_class = serializers.PortProfileSerializer


414
class RoomViewSet(viewsets.ReadOnlyModelViewSet):
415 416
    """Exposes list and details of `topologie.models.Room` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
417 418
    queryset = topologie.Room.objects.all()
    serializer_class = serializers.RoomSerializer
419 420


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

Alexandre Iooss's avatar
Alexandre Iooss committed
427

428
# USER
429 430


Maël Kervella's avatar
Maël Kervella committed
431
class UserViewSet(viewsets.ReadOnlyModelViewSet):
432 433
    """Exposes list and details of `users.models.Users` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
434 435
    queryset = users.User.objects.all()
    serializer_class = serializers.UserSerializer
Maël Kervella's avatar
Maël Kervella committed
436

Alexandre Iooss's avatar
Alexandre Iooss committed
437

438 439 440
class HomeCreationViewSet(viewsets.ReadOnlyModelViewSet):
    """Exposes infos of `users.models.Users` objects to create homes.
    """
detraz's avatar
detraz committed
441
    queryset = users.User.objects.exclude(Q(state=users.User.STATE_DISABLED) | Q(state=users.User.STATE_NOT_YET_ACTIVE))
442
    serializer_class = serializers.HomeCreationSerializer
Maël Kervella's avatar
Maël Kervella committed
443

Alexandre Iooss's avatar
Alexandre Iooss committed
444

Maël Kervella's avatar
Maël Kervella committed
445
class ClubViewSet(viewsets.ReadOnlyModelViewSet):
446 447
    """Exposes list and details of `users.models.Club` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
448 449
    queryset = users.Club.objects.all()
    serializer_class = serializers.ClubSerializer
Maël Kervella's avatar
Maël Kervella committed
450 451 452


class AdherentViewSet(viewsets.ReadOnlyModelViewSet):
453 454
    """Exposes list and details of `users.models.Adherent` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
455 456
    queryset = users.Adherent.objects.all()
    serializer_class = serializers.AdherentSerializer
Maël Kervella's avatar
Maël Kervella committed
457 458 459


class ServiceUserViewSet(viewsets.ReadOnlyModelViewSet):
460 461
    """Exposes list and details of `users.models.ServiceUser` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
462 463
    queryset = users.ServiceUser.objects.all()
    serializer_class = serializers.ServiceUserSerializer
Maël Kervella's avatar
Maël Kervella committed
464 465 466


class SchoolViewSet(viewsets.ReadOnlyModelViewSet):
467 468
    """Exposes list and details of `users.models.School` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
469 470
    queryset = users.School.objects.all()
    serializer_class = serializers.SchoolSerializer
Maël Kervella's avatar
Maël Kervella committed
471 472 473


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


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


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


class WhitelistViewSet(viewsets.ReadOnlyModelViewSet):
495 496
    """Exposes list and details of `users.models.Whitelist` objects.
    """
Maël Kervella's avatar
Maël Kervella committed
497 498
    queryset = users.Whitelist.objects.all()
    serializer_class = serializers.WhitelistSerializer
Maël Kervella's avatar
Maël Kervella committed
499

500

501 502
class EMailAddressViewSet(viewsets.ReadOnlyModelViewSet):
    """Exposes list and details of `users.models.EMailAddress` objects.
503
    """
504 505
    serializer_class = serializers.EMailAddressSerializer
    queryset = users.EMailAddress.objects.none()
506 507 508

    def get_queryset(self):
        if preferences.OptionalUser.get_cached_value(
Alexandre Iooss's avatar
Alexandre Iooss committed
509
                'local_email_accounts_enabled'):
510
            return (users.EMailAddress.objects
511 512
                    .filter(user__local_email_enabled=True))
        else:
513
            return users.EMailAddress.objects.none()
514 515


516
# SERVICE REGEN
517 518


519
class ServiceRegenViewSet(viewsets.ModelViewSet):
520 521
    """Exposes list and details of the services to regen
    """
522 523 524 525 526 527 528 529 530 531 532 533 534
    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

535 536 537 538 539 540
# Config des switches

class SwitchPortView(generics.ListAPIView):
    """Exposes the associations between hostname, mac address and IPv4 in
    order to build the DHCP lease files.
    """
541 542
    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")

543 544
    serializer_class = serializers.SwitchPortSerializer

545 546 547 548 549 550 551 552 553
# Rappel fin adhésion

class ReminderView(generics.ListAPIView):
    """Exposes the associations between hostname, mac address and IPv4 in
    order to build the DHCP lease files.
    """
    queryset = preferences.Reminder.objects.all()
    serializer_class = serializers.ReminderSerializer

554

555 556 557 558 559 560 561 562
class RoleView(generics.ListAPIView):
    """Exposes the associations between hostname, mac address and IPv4 in
    order to build the DHCP lease files.
    """
    queryset = machines.Role.objects.all().prefetch_related('servers')
    serializer_class = serializers.RoleSerializer


563
# LOCAL EMAILS
564 565


566 567
class LocalEmailUsersView(generics.ListAPIView):
    """Exposes all the aliases of the users that activated the internal address
568
    """
569
    serializer_class = serializers.LocalEmailUsersSerializer
570

571 572
    def get_queryset(self):
        if preferences.OptionalUser.get_cached_value(
Alexandre Iooss's avatar
Alexandre Iooss committed
573
                'local_email_accounts_enabled'):
574 575 576 577
            return (users.User.objects
                    .filter(local_email_enabled=True))
        else:
            return users.User.objects.none()
578 579


580
# DHCP
581

582

583
class HostMacIpView(generics.ListAPIView):
584 585 586
    """Exposes the associations between hostname, mac address and IPv4 in
    order to build the DHCP lease files.
    """
587 588 589 590
    queryset = all_active_interfaces()
    serializer_class = serializers.HostMacIpSerializer


Alexandre Iooss's avatar
Alexandre Iooss committed
591
# Firewall
592 593 594 595 596

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

Alexandre Iooss's avatar
Alexandre Iooss committed
597

598 599 600 601
class InterfacePortsOpenView(generics.ListAPIView):
    queryset = machines.Interface.objects.filter(port_lists__isnull=False).distinct()
    serializer_class = serializers.InterfacePortsOpenSerializer

Alexandre Iooss's avatar
Alexandre Iooss committed
602

603
# DNS
604

605

606
class DNSZonesView(generics.ListAPIView):
607
    """Exposes the detailed information about each extension (hostnames,
608 609
    IPs, DNS records, etc.) in order to build the DNS zone files.
    """
610 611 612 613 614 615 616 617
    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())
618 619
    serializer_class = serializers.DNSZonesSerializer

Alexandre Iooss's avatar
Alexandre Iooss committed
620

621 622 623 624 625 626 627 628
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


629
# MAILING
630 631 632


class StandardMailingView(views.APIView):
633 634 635
    """Exposes list and details of standard mailings (name and members) in
    order to building the corresponding mailing lists.
    """
636
    pagination_class = PageSizedPagination
Alexandre Iooss's avatar
Alexandre Iooss committed
637 638
    permission_classes = (ACLPermission,)
    perms_map = {'GET': [users.User.can_view_all]}
639 640

    def get(self, request, format=None):
641
        adherents_data = serializers.MailingMemberSerializer(all_has_access(), many=True).data
642 643 644 645 646 647 648
        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):
649 650 651
    """Exposes list and details of club mailings (name, members and admins) in
    order to build the corresponding mailing lists.
    """
652 653 654 655
    queryset = users.Club.objects.all()
    serializer_class = serializers.MailingSerializer


656 657 658
# TOKEN AUTHENTICATION


659
class ObtainExpiringAuthToken(ObtainAuthToken):
660 661 662 663 664 665
    """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.
    """
Alexandre Iooss's avatar
Alexandre Iooss committed
666

667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
    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,
685
            'expiration': token.created + token_duration
686
        })