8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-23 11:53:12 +00:00
re2o/api/views.py

641 lines
21 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
from rest_framework.authtoken.views import ObtainAuthToken
from rest_framework.authtoken.models import Token
2018-04-19 22:00:49 +00:00
from rest_framework.response import Response
2018-06-10 15:12:42 +00:00
from rest_framework import viewsets, generics, views
2018-04-19 22:00:49 +00:00
2018-04-24 15:57:55 +00:00
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
2018-06-10 15:12:42 +00:00
from re2o.utils import all_active_interfaces, all_has_access
2018-05-24 23:07:23 +00:00
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
class FactureViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `cotisations.models.Facture` objects.
"""
2018-04-24 15:57:55 +00:00
queryset = cotisations.Facture.objects.all()
serializer_class = serializers.FactureSerializer
2018-04-21 22:36:10 +00:00
class VenteViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `cotisations.models.Vente` objects.
"""
2018-04-24 15:57:55 +00:00
queryset = cotisations.Vente.objects.all()
serializer_class = serializers.VenteSerializer
2018-04-21 22:36:10 +00:00
class ArticleViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `cotisations.models.Article` objects.
"""
2018-04-24 15:57:55 +00:00
queryset = cotisations.Article.objects.all()
serializer_class = serializers.ArticleSerializer
2018-04-21 22:36:10 +00:00
class BanqueViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `cotisations.models.Banque` objects.
"""
2018-04-24 15:57:55 +00:00
queryset = cotisations.Banque.objects.all()
serializer_class = serializers.BanqueSerializer
2018-04-21 22:36:10 +00:00
class PaiementViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `cotisations.models.Paiement` objects.
"""
2018-04-24 15:57:55 +00:00
queryset = cotisations.Paiement.objects.all()
serializer_class = serializers.PaiementSerializer
2018-04-21 22:36:10 +00:00
class CotisationViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `cotisations.models.Cotisation` objects.
"""
2018-04-24 15:57:55 +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
class MachineViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `machines.models.Machine` objects.
"""
2018-04-24 15:57:55 +00:00
queryset = machines.Machine.objects.all()
serializer_class = serializers.MachineSerializer
2018-04-22 00:36:06 +00:00
class MachineTypeViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `machines.models.MachineType` objects.
"""
2018-04-24 15:57:55 +00:00
queryset = machines.MachineType.objects.all()
serializer_class = serializers.MachineTypeSerializer
2018-04-22 00:36:06 +00:00
class IpTypeViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `machines.models.IpType` objects.
"""
2018-04-24 15:57:55 +00:00
queryset = machines.IpType.objects.all()
serializer_class = serializers.IpTypeSerializer
2018-04-22 00:36:06 +00:00
class VlanViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `machines.models.Vlan` objects.
"""
2018-04-24 15:57:55 +00:00
queryset = machines.Vlan.objects.all()
serializer_class = serializers.VlanSerializer
2018-04-22 00:36:06 +00:00
class NasViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `machines.models.Nas` objects.
"""
2018-04-24 15:57:55 +00:00
queryset = machines.Nas.objects.all()
serializer_class = serializers.NasSerializer
2018-04-22 00:36:06 +00:00
class SOAViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `machines.models.SOA` objects.
"""
2018-04-24 15:57:55 +00:00
queryset = machines.SOA.objects.all()
serializer_class = serializers.SOASerializer
2018-04-22 00:36:06 +00:00
class ExtensionViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `machines.models.Extension` objects.
"""
2018-04-24 15:57:55 +00:00
queryset = machines.Extension.objects.all()
serializer_class = serializers.ExtensionSerializer
2018-04-22 00:36:06 +00:00
class MxViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `machines.models.Mx` objects.
"""
2018-04-24 15:57:55 +00:00
queryset = machines.Mx.objects.all()
serializer_class = serializers.MxSerializer
2018-04-22 00:36:06 +00:00
class NsViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `machines.models.Ns` objects.
"""
2018-04-24 15:57:55 +00:00
queryset = machines.Ns.objects.all()
serializer_class = serializers.NsSerializer
2018-04-22 00:36:06 +00:00
class TxtViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `machines.models.Txt` objects.
"""
2018-04-24 15:57:55 +00:00
queryset = machines.Txt.objects.all()
serializer_class = serializers.TxtSerializer
2018-06-25 15:35:40 +00:00
class DNameViewSet(viewsets.ReadOnlyModelViewSet):
"""Exposes list and details of `machines.models.DName` objects.
"""
queryset = machines.DName.objects.all()
serializer_class = serializers.DNameSerializer
2018-04-22 00:36:06 +00:00
class SrvViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `machines.models.Srv` objects.
"""
2018-04-24 15:57:55 +00:00
queryset = machines.Srv.objects.all()
serializer_class = serializers.SrvSerializer
2018-04-22 00:36:06 +00:00
class SshFpViewSet(viewsets.ReadOnlyModelViewSet):
"""Exposes list and details of `machines.models.SshFp` objects.
"""
queryset = machines.SshFp.objects.all()
serializer_class = serializers.SshFpSerializer
2018-04-22 00:36:06 +00:00
class InterfaceViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `machines.models.Interface` objects.
"""
2018-04-24 15:57:55 +00:00
queryset = machines.Interface.objects.all()
serializer_class = serializers.InterfaceSerializer
2018-04-22 00:36:06 +00:00
class Ipv6ListViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `machines.models.Ipv6List` objects.
"""
2018-04-24 15:57:55 +00:00
queryset = machines.Ipv6List.objects.all()
serializer_class = serializers.Ipv6ListSerializer
2018-04-22 00:36:06 +00:00
class DomainViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `machines.models.Domain` objects.
"""
2018-04-24 15:57:55 +00:00
queryset = machines.Domain.objects.all()
serializer_class = serializers.DomainSerializer
2018-04-22 00:36:06 +00:00
class IpListViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `machines.models.IpList` objects.
"""
2018-04-24 15:57:55 +00:00
queryset = machines.IpList.objects.all()
serializer_class = serializers.IpListSerializer
2018-04-22 00:36:06 +00:00
class ServiceViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `machines.models.Service` objects.
"""
2018-04-24 15:57:55 +00:00
queryset = machines.Service.objects.all()
serializer_class = serializers.ServiceSerializer
2018-04-22 00:36:06 +00:00
class ServiceLinkViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `machines.models.Service_link` objects.
"""
2018-04-24 15:57:55 +00:00
queryset = machines.Service_link.objects.all()
serializer_class = serializers.ServiceLinkSerializer
2018-04-22 00:36:06 +00:00
class OuverturePortListViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `machines.models.OuverturePortList`
objects.
"""
2018-04-24 15:57:55 +00:00
queryset = machines.OuverturePortList.objects.all()
serializer_class = serializers.OuverturePortListSerializer
2018-04-22 00:36:06 +00:00
class OuverturePortViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `machines.models.OuverturePort` objects.
"""
2018-04-24 15:57:55 +00:00
queryset = machines.OuverturePort.objects.all()
serializer_class = serializers.OuverturePortSerializer
2018-04-22 00:36:06 +00:00
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
2018-06-16 18:35:08 +00:00
class OptionalUserView(generics.RetrieveAPIView):
2018-06-17 01:06:58 +00:00
"""Exposes details of `preferences.models.` settings.
"""
2018-06-16 18:35:08 +00:00
permission_classes = (ACLPermission, )
perms_map = {'GET' : [preferences.OptionalUser.can_view_all]}
serializer_class = serializers.OptionalUserSerializer
def get_object(self):
return preferences.OptionalUser.objects.first()
class OptionalMachineView(generics.RetrieveAPIView):
2018-06-17 01:06:58 +00:00
"""Exposes details of `preferences.models.OptionalMachine` settings.
"""
2018-06-16 18:35:08 +00:00
permission_classes = (ACLPermission, )
perms_map = {'GET' : [preferences.OptionalMachine.can_view_all]}
serializer_class = serializers.OptionalMachineSerializer
def get_object(self):
return preferences.OptionalMachine.objects.first()
class OptionalTopologieView(generics.RetrieveAPIView):
2018-06-17 01:06:58 +00:00
"""Exposes details of `preferences.models.OptionalTopologie` settings.
"""
2018-06-16 18:35:08 +00:00
permission_classes = (ACLPermission, )
perms_map = {'GET' : [preferences.OptionalTopologie.can_view_all]}
serializer_class = serializers.OptionalTopologieSerializer
def get_object(self):
return preferences.OptionalTopologie.objects.first()
class GeneralOptionView(generics.RetrieveAPIView):
2018-06-17 01:06:58 +00:00
"""Exposes details of `preferences.models.GeneralOption` settings.
"""
2018-06-16 18:35:08 +00:00
permission_classes = (ACLPermission, )
perms_map = {'GET' : [preferences.GeneralOption.can_view_all]}
serializer_class = serializers.GeneralOptionSerializer
def get_object(self):
return preferences.GeneralOption.objects.first()
class HomeServiceViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `preferences.models.Service` objects.
"""
2018-06-16 18:35:08 +00:00
queryset = preferences.Service.objects.all()
serializer_class = serializers.HomeServiceSerializer
2018-06-16 18:35:08 +00:00
class AssoOptionView(generics.RetrieveAPIView):
2018-06-17 01:06:58 +00:00
"""Exposes details of `preferences.models.AssoOption` settings.
"""
2018-06-16 18:35:08 +00:00
permission_classes = (ACLPermission, )
perms_map = {'GET' : [preferences.AssoOption.can_view_all]}
serializer_class = serializers.AssoOptionSerializer
def get_object(self):
return preferences.AssoOption.objects.first()
class HomeOptionView(generics.RetrieveAPIView):
2018-06-17 01:06:58 +00:00
"""Exposes details of `preferences.models.HomeOption` settings.
"""
2018-06-16 18:35:08 +00:00
permission_classes = (ACLPermission, )
perms_map = {'GET' : [preferences.HomeOption.can_view_all]}
serializer_class = serializers.HomeOptionSerializer
def get_object(self):
return preferences.HomeOption.objects.first()
class MailMessageOptionView(generics.RetrieveAPIView):
2018-06-17 01:06:58 +00:00
"""Exposes details of `preferences.models.MailMessageOption` settings.
"""
2018-06-16 18:35:08 +00:00
permission_classes = (ACLPermission, )
perms_map = {'GET' : [preferences.MailMessageOption.can_view_all]}
serializer_class = serializers.MailMessageOptionSerializer
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
class StackViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `topologie.models.Stack` objects.
"""
2018-04-24 15:57:55 +00:00
queryset = topologie.Stack.objects.all()
serializer_class = serializers.StackSerializer
2018-04-24 13:59:18 +00:00
class AccessPointViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `topologie.models.AccessPoint` objects.
"""
2018-04-24 15:57:55 +00:00
queryset = topologie.AccessPoint.objects.all()
serializer_class = serializers.AccessPointSerializer
2018-04-24 13:59:18 +00:00
class SwitchViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `topologie.models.Switch` objects.
"""
2018-04-24 15:57:55 +00:00
queryset = topologie.Switch.objects.all()
serializer_class = serializers.SwitchSerializer
2018-04-24 13:59:18 +00:00
2018-06-20 12:13:23 +00:00
class ServerViewSet(viewsets.ReadOnlyModelViewSet):
"""Exposes list and details of `topologie.models.Server` objects.
"""
queryset = topologie.Server.objects.all()
serializer_class = serializers.ServerSerializer
2018-04-24 13:59:18 +00:00
class ModelSwitchViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `topologie.models.ModelSwitch` objects.
"""
2018-04-24 15:57:55 +00:00
queryset = topologie.ModelSwitch.objects.all()
serializer_class = serializers.ModelSwitchSerializer
2018-04-24 13:59:18 +00:00
class ConstructorSwitchViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `topologie.models.ConstructorSwitch`
objects.
"""
2018-04-24 15:57:55 +00:00
queryset = topologie.ConstructorSwitch.objects.all()
serializer_class = serializers.ConstructorSwitchSerializer
2018-04-24 13:59:18 +00:00
class SwitchBayViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `topologie.models.SwitchBay` objects.
"""
2018-04-24 15:57:55 +00:00
queryset = topologie.SwitchBay.objects.all()
serializer_class = serializers.SwitchBaySerializer
2018-04-24 13:59:18 +00:00
class BuildingViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `topologie.models.Building` objects.
"""
2018-04-24 15:57:55 +00:00
queryset = topologie.Building.objects.all()
serializer_class = serializers.BuildingSerializer
2018-04-24 13:59:18 +00:00
class SwitchPortViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `topologie.models.Port` objects.
"""
2018-04-24 15:57:55 +00:00
queryset = topologie.Port.objects.all()
serializer_class = serializers.SwitchPortSerializer
2018-04-24 13:59:18 +00:00
class RoomViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `topologie.models.Room` objects.
"""
2018-04-24 15:57:55 +00:00
queryset = topologie.Room.objects.all()
serializer_class = serializers.RoomSerializer
2018-04-24 13:59:18 +00:00
class PortProfileViewSet(viewsets.ReadOnlyModelViewSet):
"""Exposes list and details of `topologie.models.PortProfile` objects.
"""
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
2018-04-19 22:00:49 +00:00
class UserViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `users.models.Users` objects.
"""
2018-04-24 15:57:55 +00:00
queryset = users.User.objects.all()
serializer_class = serializers.UserSerializer
2018-04-19 22:00:49 +00:00
class HomeCreationViewSet(viewsets.ReadOnlyModelViewSet):
"""Exposes infos of `users.models.Users` objects to create homes.
"""
queryset = users.User.objects.exclude(Q(state=User.STATE_DISABLED) | Q(state=User.STATE_NOT_YET_ACTIVE))
serializer_class = serializers.HomeCreationSerializer
2018-04-19 22:00:49 +00:00
class ClubViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `users.models.Club` objects.
"""
2018-04-24 15:57:55 +00:00
queryset = users.Club.objects.all()
serializer_class = serializers.ClubSerializer
2018-04-19 22:00:49 +00:00
class AdherentViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `users.models.Adherent` objects.
"""
2018-04-24 15:57:55 +00:00
queryset = users.Adherent.objects.all()
serializer_class = serializers.AdherentSerializer
2018-04-19 22:00:49 +00:00
class ServiceUserViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `users.models.ServiceUser` objects.
"""
2018-04-24 15:57:55 +00:00
queryset = users.ServiceUser.objects.all()
serializer_class = serializers.ServiceUserSerializer
2018-04-19 22:00:49 +00:00
class SchoolViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `users.models.School` objects.
"""
2018-04-24 15:57:55 +00:00
queryset = users.School.objects.all()
serializer_class = serializers.SchoolSerializer
2018-04-19 22:00:49 +00:00
class ListRightViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `users.models.ListRight` objects.
"""
2018-04-24 15:57:55 +00:00
queryset = users.ListRight.objects.all()
serializer_class = serializers.ListRightSerializer
2018-04-19 22:00:49 +00:00
class ShellViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `users.models.ListShell` objects.
"""
2018-04-24 15:57:55 +00:00
queryset = users.ListShell.objects.all()
serializer_class = serializers.ShellSerializer
2018-04-19 22:00:49 +00:00
class BanViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `users.models.Ban` objects.
"""
2018-04-24 15:57:55 +00:00
queryset = users.Ban.objects.all()
serializer_class = serializers.BanSerializer
2018-04-19 22:00:49 +00:00
class WhitelistViewSet(viewsets.ReadOnlyModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of `users.models.Whitelist` objects.
"""
2018-04-24 15:57:55 +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
2018-08-01 11:06:25 +00:00
class EMailAddressViewSet(viewsets.ReadOnlyModelViewSet):
"""Exposes list and details of `users.models.EMailAddress` objects.
2018-06-30 13:18:08 +00:00
"""
2018-08-01 11:06:25 +00:00
serializer_class = serializers.EMailAddressSerializer
queryset = users.EMailAddress.objects.none()
2018-07-30 15:00:41 +00:00
def get_queryset(self):
if preferences.OptionalUser.get_cached_value(
'local_email_accounts_enabled'):
2018-08-01 11:06:25 +00:00
return (users.EMailAddress.objects
2018-07-30 15:00:41 +00:00
.filter(user__local_email_enabled=True))
else:
2018-08-01 11:06:25 +00:00
return users.EMailAddress.objects.none()
2018-06-30 13:18:08 +00:00
2018-06-17 01:06:58 +00:00
# SERVICE REGEN
class ServiceRegenViewSet(viewsets.ModelViewSet):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of the services to regen
"""
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
2018-07-30 15:00:41 +00:00
# LOCAL EMAILS
2018-07-30 15:00:41 +00:00
class LocalEmailUsersView(generics.ListAPIView):
"""Exposes all the aliases of the users that activated the internal address
"""
2018-07-30 15:00:41 +00:00
serializer_class = serializers.LocalEmailUsersSerializer
2018-07-30 15:00:41 +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
2018-05-24 23:07:23 +00:00
class HostMacIpView(generics.ListAPIView):
2018-06-17 01:06:58 +00:00
"""Exposes the associations between hostname, mac address and IPv4 in
order to build the DHCP lease files.
"""
2018-05-24 23:07:23 +00:00
queryset = all_active_interfaces()
serializer_class = serializers.HostMacIpSerializer
#Firewall
class SubnetPortsOpenView(generics.ListAPIView):
queryset = machines.IpType.objects.all()
serializer_class = serializers.SubnetPortsOpenSerializer
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
class DNSZonesView(generics.ListAPIView):
"""Exposes the detailed information about each extension (hostnames,
2018-06-17 01:06:58 +00:00
IPs, DNS records, etc.) in order to build the DNS zone files.
"""
2018-06-23 21:19:11 +00:00
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())
2018-06-10 00:47:25 +00:00
serializer_class = serializers.DNSZonesSerializer
2018-07-14 08:59:17 +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-06-10 00:47:25 +00:00
2018-06-17 01:06:58 +00:00
# MAILING
2018-06-10 15:12:42 +00:00
class StandardMailingView(views.APIView):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of standard mailings (name and members) in
order to building the corresponding mailing lists.
"""
2018-06-10 15:12:42 +00:00
pagination_class = PageSizedPagination
2018-06-13 22:39:37 +00:00
permission_classes = (ACLPermission, )
perms_map = {'GET' : [users.User.can_view_all]}
2018-06-10 15:12:42 +00:00
def get(self, request, format=None):
2018-06-13 22:39:37 +00:00
adherents_data = serializers.MailingMemberSerializer(all_has_access(), many=True).data
2018-06-10 15:12:42 +00:00
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):
2018-06-17 01:06:58 +00:00
"""Exposes list and details of club mailings (name, members and admins) in
order to build the corresponding mailing lists.
"""
2018-06-10 15:12:42 +00:00
queryset = users.Club.objects.all()
serializer_class = serializers.MailingSerializer
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,
2018-05-24 17:56:00 +00:00
'expiration': token.created + token_duration
})