8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-10-05 18:42:12 +00:00
re2o/api/views.py

814 lines
25 KiB
Python
Raw Normal View History

2018-06-30 01:25:46 +00:00
# -*- mode: python; coding: utf-8 -*-
2018-03-17 17:20:31 +00:00
# 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.
2018-06-17 01:06:58 +00:00
"""Defines the views of the API
2018-03-17 17:20:31 +00:00
2018-06-17 01:06:58 +00:00
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, ...
2018-03-17 17:20:31 +00:00
"""
2018-03-17 17:50:03 +00:00
import datetime
2018-03-17 17:50:03 +00:00
from django.conf import settings
from django.db.models import Q
2020-01-25 22:37:17 +00:00
from django.contrib.auth.models import Group
from rest_framework import viewsets, generics, views
from rest_framework.authtoken.models import Token
from rest_framework.authtoken.views import ObtainAuthToken
2018-04-19 22:00:49 +00:00
from rest_framework.response import Response
2020-04-20 18:43:11 +00:00
# import cotisations.models as cotisations
2018-04-24 15:57:55 +00:00
import machines.models as machines
import preferences.models as preferences
import topologie.models as topologie
import users.models as users
2018-06-10 15:12:42 +00:00
from re2o.utils import all_active_interfaces, all_has_access
2018-04-24 15:57:55 +00:00
from . import serializers
2018-06-10 15:12:42 +00:00
from .pagination import PageSizedPagination
2018-06-13 22:39:37 +00:00
from .permissions import ACLPermission
2018-03-17 17:57:11 +00:00
2018-04-13 19:35:52 +00:00
2018-06-17 01:06:58 +00:00
# COTISATIONS
2018-04-21 22:36:10 +00:00
2020-04-20 18:43:11 +00:00
# class FactureViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `cotisations.models.Facture` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = cotisations.Facture.objects.all()
# serializer_class = serializers.FactureSerializer
2018-04-21 22:36:10 +00:00
2020-04-20 18:43:11 +00:00
# class FactureViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `cotisations.models.Facture` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = cotisations.BaseInvoice.objects.all()
# serializer_class = serializers.BaseInvoiceSerializer
2018-10-12 11:05:38 +00:00
2018-04-21 22:36:10 +00:00
2020-04-20 18:43:11 +00:00
# class VenteViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `cotisations.models.Vente` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = cotisations.Vente.objects.all()
# serializer_class = serializers.VenteSerializer
2018-04-21 22:36:10 +00:00
2020-04-20 18:43:11 +00:00
# class ArticleViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `cotisations.models.Article` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = cotisations.Article.objects.all()
# serializer_class = serializers.ArticleSerializer
2018-04-21 22:36:10 +00:00
2020-04-20 18:43:11 +00:00
# class BanqueViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `cotisations.models.Banque` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = cotisations.Banque.objects.all()
# serializer_class = serializers.BanqueSerializer
2018-04-21 22:36:10 +00:00
2020-04-20 18:43:11 +00:00
# class PaiementViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `cotisations.models.Paiement` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = cotisations.Paiement.objects.all()
# serializer_class = serializers.PaiementSerializer
2018-04-21 22:36:10 +00:00
2020-04-20 18:43:11 +00:00
# class CotisationViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `cotisations.models.Cotisation` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = cotisations.Cotisation.objects.all()
# serializer_class = serializers.CotisationSerializer
2018-04-21 22:36:10 +00:00
2018-06-17 01:06:58 +00:00
# MACHINES
2018-04-22 00:36:06 +00:00
2020-04-20 18:43:11 +00:00
# class MachineViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `machines.models.Machine` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = machines.Machine.objects.all()
# serializer_class = serializers.MachineSerializer
2018-04-22 00:36:06 +00:00
2020-04-20 18:43:11 +00:00
# class MachineTypeViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `machines.models.MachineType` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = machines.MachineType.objects.all()
# serializer_class = serializers.MachineTypeSerializer
2018-04-22 00:36:06 +00:00
2020-04-20 18:43:11 +00:00
# class IpTypeViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `machines.models.IpType` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = machines.IpType.objects.all()
# serializer_class = serializers.IpTypeSerializer
2018-04-22 00:36:06 +00:00
2020-04-20 18:43:11 +00:00
# class VlanViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `machines.models.Vlan` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = machines.Vlan.objects.all()
# serializer_class = serializers.VlanSerializer
2018-04-22 00:36:06 +00:00
2020-04-20 18:43:11 +00:00
# class NasViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `machines.models.Nas` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = machines.Nas.objects.all()
# serializer_class = serializers.NasSerializer
2018-04-22 00:36:06 +00:00
2020-04-20 18:43:11 +00:00
# class SOAViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `machines.models.SOA` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = machines.SOA.objects.all()
# serializer_class = serializers.SOASerializer
2018-04-22 00:36:06 +00:00
2020-04-20 18:43:11 +00:00
# class ExtensionViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `machines.models.Extension` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = machines.Extension.objects.all()
# serializer_class = serializers.ExtensionSerializer
2018-04-22 00:36:06 +00:00
2020-04-20 18:43:11 +00:00
# class MxViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `machines.models.Mx` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = machines.Mx.objects.all()
# serializer_class = serializers.MxSerializer
2018-04-22 00:36:06 +00:00
2020-04-20 18:43:11 +00:00
# class NsViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `machines.models.Ns` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = machines.Ns.objects.all()
# serializer_class = serializers.NsSerializer
2018-04-22 00:36:06 +00:00
2020-04-20 18:43:11 +00:00
# class TxtViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `machines.models.Txt` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = machines.Txt.objects.all()
# serializer_class = serializers.TxtSerializer
2018-06-25 15:35:40 +00:00
2020-04-20 18:43:11 +00:00
# class DNameViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `machines.models.DName` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = machines.DName.objects.all()
# serializer_class = serializers.DNameSerializer
2018-04-22 00:36:06 +00:00
2020-04-20 18:43:11 +00:00
# class SrvViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `machines.models.Srv` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = machines.Srv.objects.all()
# serializer_class = serializers.SrvSerializer
2018-04-22 00:36:06 +00:00
2020-04-20 18:43:11 +00:00
# class SshFpViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `machines.models.SshFp` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = machines.SshFp.objects.all()
# serializer_class = serializers.SshFpSerializer
2020-04-20 18:43:11 +00:00
# class InterfaceViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `machines.models.Interface` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = machines.Interface.objects.all()
# serializer_class = serializers.InterfaceSerializer
2018-04-22 00:36:06 +00:00
2020-04-20 18:43:11 +00:00
# class Ipv6ListViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `machines.models.Ipv6List` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = machines.Ipv6List.objects.all()
# serializer_class = serializers.Ipv6ListSerializer
2018-04-22 00:36:06 +00:00
2020-04-20 18:43:11 +00:00
# class DomainViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `machines.models.Domain` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = machines.Domain.objects.all()
# serializer_class = serializers.DomainSerializer
2018-04-22 00:36:06 +00:00
2020-04-20 18:43:11 +00:00
# class IpListViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `machines.models.IpList` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = machines.IpList.objects.all()
# serializer_class = serializers.IpListSerializer
2018-04-22 00:36:06 +00:00
2020-04-20 18:43:11 +00:00
# class ServiceViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `machines.models.Service` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = machines.Service.objects.all()
# serializer_class = serializers.ServiceSerializer
2018-04-22 00:36:06 +00:00
2020-04-20 18:43:11 +00:00
# class ServiceLinkViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `machines.models.Service_link` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = machines.Service_link.objects.all()
# serializer_class = serializers.ServiceLinkSerializer
2018-04-22 00:36:06 +00:00
2020-04-20 18:43:11 +00:00
# class OuverturePortListViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `machines.models.OuverturePortList`
# objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = machines.OuverturePortList.objects.all()
# serializer_class = serializers.OuverturePortListSerializer
2018-04-22 00:36:06 +00:00
2020-04-20 18:43:11 +00:00
# class OuverturePortViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `machines.models.OuverturePort` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = machines.OuverturePort.objects.all()
# serializer_class = serializers.OuverturePortSerializer
2018-04-22 00:36:06 +00:00
2020-04-20 18:43:11 +00:00
# class RoleViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `machines.models.Machine` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = machines.Role.objects.all()
# serializer_class = serializers.RoleSerializer
2018-06-17 01:06:58 +00:00
# PREFERENCES
2018-06-16 18:35:08 +00:00
# Those views differ a bit because there is only one object
# to display, so we don't bother with the listing part
2018-04-22 18:28:01 +00:00
2020-04-20 18:43:11 +00:00
# class OptionalUserView(generics.RetrieveAPIView):
# """Exposes details of `preferences.models.` settings.
# """
2020-04-20 18:43:11 +00:00
# permission_classes = (ACLPermission,)
# perms_map = {"GET": [preferences.OptionalUser.can_view_all]}
# serializer_class = serializers.OptionalUserSerializer
2018-06-16 18:35:08 +00:00
2020-04-20 18:43:11 +00:00
# def get_object(self):
# return preferences.OptionalUser.objects.first()
2018-06-16 18:35:08 +00:00
2020-04-20 18:43:11 +00:00
# class OptionalMachineView(generics.RetrieveAPIView):
# """Exposes details of `preferences.models.OptionalMachine` settings.
# """
2020-04-20 18:43:11 +00:00
# permission_classes = (ACLPermission,)
# perms_map = {"GET": [preferences.OptionalMachine.can_view_all]}
# serializer_class = serializers.OptionalMachineSerializer
2018-06-16 18:35:08 +00:00
2020-04-20 18:43:11 +00:00
# def get_object(self):
# return preferences.OptionalMachine.objects.first()
2018-06-16 18:35:08 +00:00
2020-04-20 18:43:11 +00:00
# class OptionalTopologieView(generics.RetrieveAPIView):
# """Exposes details of `preferences.models.OptionalTopologie` settings.
# """
2020-04-20 18:43:11 +00:00
# permission_classes = (ACLPermission,)
# perms_map = {"GET": [preferences.OptionalTopologie.can_view_all]}
# serializer_class = serializers.OptionalTopologieSerializer
2018-06-16 18:35:08 +00:00
2020-04-20 18:43:11 +00:00
# def get_object(self):
# return preferences.OptionalTopologie.objects.first()
2018-06-16 18:35:08 +00:00
2020-04-20 18:43:11 +00:00
# class RadiusOptionView(generics.RetrieveAPIView):
# """Exposes details of `preferences.models.OptionalTopologie` settings.
# """
2020-04-20 18:43:11 +00:00
# permission_classes = (ACLPermission,)
# perms_map = {"GET": [preferences.RadiusOption.can_view_all]}
# serializer_class = serializers.RadiusOptionSerializer
2020-04-20 18:43:11 +00:00
# def get_object(self):
# return preferences.RadiusOption.objects.first()
2020-04-20 18:43:11 +00:00
# class GeneralOptionView(generics.RetrieveAPIView):
# """Exposes details of `preferences.models.GeneralOption` settings.
# """
2020-04-20 18:43:11 +00:00
# permission_classes = (ACLPermission,)
# perms_map = {"GET": [preferences.GeneralOption.can_view_all]}
# serializer_class = serializers.GeneralOptionSerializer
2018-06-16 18:35:08 +00:00
2020-04-20 18:43:11 +00:00
# def get_object(self):
# return preferences.GeneralOption.objects.first()
2018-06-16 18:35:08 +00:00
2020-04-20 18:43:11 +00:00
# class HomeServiceViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `preferences.models.Service` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = preferences.Service.objects.all()
# serializer_class = serializers.HomeServiceSerializer
2018-06-16 18:35:08 +00:00
2020-04-20 18:43:11 +00:00
# class AssoOptionView(generics.RetrieveAPIView):
# """Exposes details of `preferences.models.AssoOption` settings.
# """
2020-04-20 18:43:11 +00:00
# permission_classes = (ACLPermission,)
# perms_map = {"GET": [preferences.AssoOption.can_view_all]}
# serializer_class = serializers.AssoOptionSerializer
2018-06-16 18:35:08 +00:00
2020-04-20 18:43:11 +00:00
# def get_object(self):
# return preferences.AssoOption.objects.first()
2018-06-16 18:35:08 +00:00
2020-04-20 18:43:11 +00:00
# class HomeOptionView(generics.RetrieveAPIView):
# """Exposes details of `preferences.models.HomeOption` settings.
# """
2020-04-20 18:43:11 +00:00
# permission_classes = (ACLPermission,)
# perms_map = {"GET": [preferences.HomeOption.can_view_all]}
# serializer_class = serializers.HomeOptionSerializer
2018-06-16 18:35:08 +00:00
2020-04-20 18:43:11 +00:00
# def get_object(self):
# return preferences.HomeOption.objects.first()
2018-06-16 18:35:08 +00:00
2020-04-20 18:43:11 +00:00
# class MailMessageOptionView(generics.RetrieveAPIView):
# """Exposes details of `preferences.models.MailMessageOption` settings.
# """
2020-04-20 18:43:11 +00:00
# permission_classes = (ACLPermission,)
# perms_map = {"GET": [preferences.MailMessageOption.can_view_all]}
# serializer_class = serializers.MailMessageOptionSerializer
2018-06-16 18:35:08 +00:00
2020-04-20 18:43:11 +00:00
# def get_object(self):
# return preferences.MailMessageOption.objects.first()
2018-04-22 18:28:01 +00:00
2018-06-17 01:06:58 +00:00
# TOPOLOGIE
2018-04-24 13:59:18 +00:00
2020-04-20 18:43:11 +00:00
# class StackViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `topologie.models.Stack` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = topologie.Stack.objects.all()
# serializer_class = serializers.StackSerializer
2018-04-24 13:59:18 +00:00
2020-04-20 18:43:11 +00:00
# class AccessPointViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `topologie.models.AccessPoint` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = topologie.AccessPoint.objects.all()
# serializer_class = serializers.AccessPointSerializer
2018-04-24 13:59:18 +00:00
2020-04-20 18:43:11 +00:00
# class SwitchViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `topologie.models.Switch` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = topologie.Switch.objects.all()
# serializer_class = serializers.SwitchSerializer
2018-04-24 13:59:18 +00:00
2020-04-20 18:43:11 +00:00
# class ServerViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `topologie.models.Server` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = topologie.Server.objects.all()
# serializer_class = serializers.ServerSerializer
2018-06-20 12:13:23 +00:00
2020-04-20 18:43:11 +00:00
# class ModelSwitchViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `topologie.models.ModelSwitch` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = topologie.ModelSwitch.objects.all()
# serializer_class = serializers.ModelSwitchSerializer
2018-04-24 13:59:18 +00:00
2020-04-20 18:43:11 +00:00
# class ConstructorSwitchViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `topologie.models.ConstructorSwitch`
# objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = topologie.ConstructorSwitch.objects.all()
# serializer_class = serializers.ConstructorSwitchSerializer
2018-04-24 13:59:18 +00:00
2020-04-20 18:43:11 +00:00
# class SwitchBayViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `topologie.models.SwitchBay` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = topologie.SwitchBay.objects.all()
# serializer_class = serializers.SwitchBaySerializer
2018-04-24 13:59:18 +00:00
2020-04-20 18:43:11 +00:00
# class BuildingViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `topologie.models.Building` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = topologie.Building.objects.all()
# serializer_class = serializers.BuildingSerializer
2018-04-24 13:59:18 +00:00
2020-04-20 18:43:11 +00:00
# class SwitchPortViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `topologie.models.Port` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = topologie.Port.objects.all()
# serializer_class = serializers.SwitchPortSerializer
2018-04-24 13:59:18 +00:00
2020-04-20 18:43:11 +00:00
# class PortProfileViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `topologie.models.PortProfile` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = topologie.PortProfile.objects.all()
# serializer_class = serializers.PortProfileSerializer
2018-07-02 09:00:32 +00:00
2020-04-20 18:43:11 +00:00
# class RoomViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `topologie.models.Room` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = topologie.Room.objects.all()
# serializer_class = serializers.RoomSerializer
2018-04-24 13:59:18 +00:00
2020-04-20 18:43:11 +00:00
# class PortProfileViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `topologie.models.PortProfile` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = topologie.PortProfile.objects.all()
# serializer_class = serializers.PortProfileSerializer
2018-06-17 01:06:58 +00:00
# USER
2018-04-21 22:36:10 +00:00
2020-04-20 18:43:11 +00:00
# class UserViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `users.models.Users` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = users.User.objects.all()
# serializer_class = serializers.UserSerializer
2018-04-19 22:00:49 +00:00
2020-04-20 18:43:11 +00:00
# class HomeCreationViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes infos of `users.models.Users` objects to create homes.
# """
2020-04-20 18:43:11 +00:00
# queryset = users.User.objects.exclude(
# Q(state=users.User.STATE_DISABLED)
# | Q(state=users.User.STATE_NOT_YET_ACTIVE)
# | Q(state=users.User.STATE_FULL_ARCHIVE)
# )
# serializer_class = serializers.BasicUserSerializer
2018-11-17 18:51:37 +00:00
2020-04-20 18:43:11 +00:00
# class NormalUserViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes infos of `users.models.Users`without specific rights objects."""
2020-04-20 18:43:11 +00:00
# queryset = users.User.objects.exclude(groups__listright__critical=True).distinct()
# serializer_class = serializers.BasicUserSerializer
2018-11-17 18:51:37 +00:00
2020-04-20 18:43:11 +00:00
# class CriticalUserViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes infos of `users.models.Users`without specific rights objects."""
2020-04-20 18:43:11 +00:00
# queryset = users.User.objects.filter(groups__listright__critical=True).distinct()
# serializer_class = serializers.BasicUserSerializer
2018-04-19 22:00:49 +00:00
2020-04-20 18:43:11 +00:00
# class ClubViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `users.models.Club` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = users.Club.objects.all()
# serializer_class = serializers.ClubSerializer
2018-04-19 22:00:49 +00:00
2020-04-20 18:43:11 +00:00
# class AdherentViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `users.models.Adherent` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = users.Adherent.objects.all()
# serializer_class = serializers.AdherentSerializer
2018-04-19 22:00:49 +00:00
2020-04-20 18:43:11 +00:00
# class ServiceUserViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `users.models.ServiceUser` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = users.ServiceUser.objects.all()
# serializer_class = serializers.ServiceUserSerializer
2018-04-19 22:00:49 +00:00
2020-04-20 18:43:11 +00:00
# class SchoolViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `users.models.School` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = users.School.objects.all()
# serializer_class = serializers.SchoolSerializer
2018-04-19 22:00:49 +00:00
2020-04-20 18:43:11 +00:00
# class ListRightViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `users.models.ListRight` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = users.ListRight.objects.all()
# serializer_class = serializers.ListRightSerializer
2018-04-19 22:00:49 +00:00
2020-04-20 18:43:11 +00:00
# class ShellViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `users.models.ListShell` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = users.ListShell.objects.all()
# serializer_class = serializers.ShellSerializer
2018-04-19 22:00:49 +00:00
2020-04-20 18:43:11 +00:00
# class BanViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `users.models.Ban` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = users.Ban.objects.all()
# serializer_class = serializers.BanSerializer
2018-04-19 22:00:49 +00:00
2020-04-20 18:43:11 +00:00
# class WhitelistViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `users.models.Whitelist` objects.
# """
2020-04-20 18:43:11 +00:00
# queryset = users.Whitelist.objects.all()
# serializer_class = serializers.WhitelistSerializer
2018-04-19 22:00:49 +00:00
2018-05-24 23:07:23 +00:00
2020-04-20 18:43:11 +00:00
# class EMailAddressViewSet(viewsets.ReadOnlyModelViewSet):
# """Exposes list and details of `users.models.EMailAddress` objects.
# """
2020-04-20 18:43:11 +00:00
# serializer_class = serializers.EMailAddressSerializer
# queryset = users.EMailAddress.objects.none()
2018-07-30 15:00:41 +00:00
2020-04-20 18:43:11 +00:00
# def get_queryset(self):
# if preferences.OptionalUser.get_cached_value("local_email_accounts_enabled"):
# return users.EMailAddress.objects.filter(user__local_email_enabled=True)
# else:
# return users.EMailAddress.objects.none()
2018-06-30 13:18:08 +00:00
2018-06-17 01:06:58 +00:00
# SERVICE REGEN
2020-04-20 18:43:11 +00:00
# class ServiceRegenViewSet(viewsets.ModelViewSet):
# """Exposes list and details of the services to regen
# """
2020-04-20 18:43:11 +00:00
# serializer_class = serializers.ServiceRegenSerializer
2020-04-20 18:43:11 +00:00
# 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
2018-07-02 09:00:32 +00:00
# Config des switches
2020-04-20 18:43:11 +00:00
# class SwitchPortView(generics.ListAPIView):
# """Output each port of a switch, to be serialized with
# additionnal informations (profiles etc)
# """
2020-04-20 18:43:11 +00:00
# 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")
# )
2018-07-08 21:39:15 +00:00
2020-04-20 18:43:11 +00:00
# serializer_class = serializers.SwitchPortSerializer
2018-07-02 09:00:32 +00:00
# Rappel fin adhésion
class ReminderView(generics.ListAPIView):
2018-11-15 14:47:15 +00:00
"""Output for users to remind an end of their subscription.
"""
queryset = preferences.Reminder.objects.all()
serializer_class = serializers.ReminderSerializer
2020-04-20 18:43:11 +00:00
# class RoleView(generics.ListAPIView):
# """Output of roles for each server
# """
2020-04-20 18:43:11 +00:00
# queryset = machines.Role.objects.all().prefetch_related("servers")
# serializer_class = serializers.RoleSerializer
2018-07-30 15:00:41 +00:00
# LOCAL EMAILS
2020-04-20 18:43:11 +00:00
# class LocalEmailUsersView(generics.ListAPIView):
# """Exposes all the aliases of the users that activated the internal address
# """
2020-04-20 18:43:11 +00:00
# serializer_class = serializers.LocalEmailUsersSerializer
2020-04-20 18:43:11 +00:00
# def get_queryset(self):
# if preferences.OptionalUser.get_cached_value("local_email_accounts_enabled"):
# return users.User.objects.filter(local_email_enabled=True)
# else:
# return users.User.objects.none()
2018-06-17 01:06:58 +00:00
# DHCP
2018-05-24 23:07:23 +00:00
2018-07-30 15:00:41 +00:00
2020-04-20 18:43:11 +00:00
# class HostMacIpView(generics.ListAPIView):
# """Exposes the associations between hostname, mac address and IPv4 in
# order to build the DHCP lease files.
# """
2020-04-20 18:43:11 +00:00
# serializer_class = serializers.HostMacIpSerializer
2018-05-24 23:07:23 +00:00
2020-04-20 18:43:11 +00:00
# def get_queryset(self):
# return all_active_interfaces()
2019-01-10 22:15:36 +00:00
2018-05-24 23:07:23 +00:00
# Firewall
2020-04-20 18:43:11 +00:00
# class SubnetPortsOpenView(generics.ListAPIView):
# queryset = machines.IpType.objects.all()
# serializer_class = serializers.SubnetPortsOpenSerializer
2020-04-20 18:43:11 +00:00
# class InterfacePortsOpenView(generics.ListAPIView):
# queryset = machines.Interface.objects.filter(port_lists__isnull=False).distinct()
# serializer_class = serializers.InterfacePortsOpenSerializer
2018-06-17 01:06:58 +00:00
# DNS
2018-06-10 00:47:25 +00:00
2018-07-30 15:00:41 +00:00
2020-04-20 18:43:11 +00:00
# class DNSZonesView(generics.ListAPIView):
# """Exposes the detailed information about each extension (hostnames,
# IPs, DNS records, etc.) in order to build the DNS zone files.
# """
# 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()
# )
# serializer_class = serializers.DNSZonesSerializer
2020-04-20 18:43:11 +00:00
# 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
2018-07-14 08:59:17 +00:00
2018-06-17 01:06:58 +00:00
# MAILING
2018-06-10 15:12:42 +00:00
2020-04-20 18:43:11 +00:00
# class StandardMailingView(views.APIView):
# """Exposes list and details of standard mailings (name and members) in
# order to building the corresponding mailing lists.
# """
2020-04-20 18:43:11 +00:00
# pagination_class = PageSizedPagination
# permission_classes = (ACLPermission,)
# perms_map = {"GET": [users.User.can_view_all]}
2018-06-10 15:12:42 +00:00
2020-04-20 18:43:11 +00:00
# def get(self, request, format=None):
# adherents_data = serializers.MailingMemberSerializer(
# all_has_access(), many=True
# ).data
2020-01-25 22:37:17 +00:00
2020-04-20 18:43:11 +00:00
# data = [{"name": "adherents", "members": adherents_data}]
# groups = Group.objects.all()
# for group in groups:
# group_data = serializers.MailingMemberSerializer(
# group.user_set.all(), many=True
# ).data
# data.append({"name": group.name, "members": group_data})
# paginator = self.pagination_class()
# paginator.paginate_queryset(data, request)
# return paginator.get_paginated_response(data)
# class ClubMailingView(generics.ListAPIView):
# """Exposes list and details of club mailings (name, members and admins) in
# order to build the corresponding mailing lists.
# """
# queryset = users.Club.objects.all()
# serializer_class = serializers.MailingSerializer
2018-06-10 15:12:42 +00:00
2018-06-17 01:06:58 +00:00
# TOKEN AUTHENTICATION
class ObtainExpiringAuthToken(ObtainAuthToken):
2018-06-17 01:06:58 +00:00
"""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.
"""
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, "expiration": token.created + token_duration}
)