8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-05 09:26:27 +00:00
re2o/api/views.py

336 lines
10 KiB
Python
Raw Normal View History

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.
"""api.views
The views for the API app. They should all return JSON data and not fallback on
HTML pages such as the login and index pages for a better integration.
"""
2018-03-17 17:50:03 +00:00
import datetime
2018-03-17 17:50:03 +00:00
from django.conf import settings
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
from rest_framework import viewsets, status
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-04-21 22:36:10 +00:00
2018-04-24 15:57:55 +00:00
from . import serializers
2018-03-17 17:57:11 +00:00
2018-04-13 19:35:52 +00:00
2018-04-22 00:36:06 +00:00
# COTISATIONS APP
2018-04-21 22:36:10 +00:00
class FactureViewSet(viewsets.ReadOnlyModelViewSet):
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-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-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-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-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-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-04-22 00:36:06 +00:00
# MACHINES APP
class MachineViewSet(viewsets.ReadOnlyModelViewSet):
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-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-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-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-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-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-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-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-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-04-24 15:57:55 +00:00
queryset = machines.Txt.objects.all()
serializer_class = serializers.TxtSerializer
2018-04-22 00:36:06 +00:00
class SrvViewSet(viewsets.ReadOnlyModelViewSet):
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 InterfaceViewSet(viewsets.ReadOnlyModelViewSet):
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-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-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-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-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-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-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-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-04-22 18:28:01 +00:00
# PREFERENCES APP
# class OptionalUserViewSet(viewsets.ReadOnlyModelViewSet):
2018-04-24 15:57:55 +00:00
# queryset = preferences.OptionalUser.objects.all()
# serializer_class = serializers.OptionalUserSerializer
2018-04-22 18:28:01 +00:00
#
#
# class OptionalMachineViewSet(viewsets.ReadOnlyModelViewSet):
2018-04-24 15:57:55 +00:00
# queryset = preferences.OptionalMachine.objects.all()
# serializer_class = serializers.OptionalMachineSerializer
2018-04-22 18:28:01 +00:00
#
#
# class OptionalTopologieViewSet(viewsets.ReadOnlyModelViewSet):
2018-04-24 15:57:55 +00:00
# queryset = preferences.OptionalTopologie.objects.all()
# serializer_class = serializers.OptionalTopologieSerializer
2018-04-22 18:28:01 +00:00
#
#
# class GeneralOptionViewSet(viewsets.ReadOnlyModelViewSet):
2018-04-24 15:57:55 +00:00
# queryset = preferences.GeneralOption.objects.all()
# serializer_class = serializers.GeneralOptionSerializer
2018-04-22 18:28:01 +00:00
#
#
# class ServiceOptionViewSet(viewsets.ReadOnlyModelViewSet):
2018-04-24 15:57:55 +00:00
# queryset = preferences.ServiceOption.objects.all()
# serializer_class = serializers.ServiceOptionSerializer
2018-04-22 18:28:01 +00:00
#
#
# class AssoOptionViewSet(viewsets.ReadOnlyModelViewSet):
2018-04-24 15:57:55 +00:00
# queryset = preferences.AssoOption.objects.all()
# serializer_class = serializers.AssoOptionSerializer
2018-04-22 18:28:01 +00:00
#
#
# class HomeOptionViewSet(viewsets.ReadOnlyModelViewSet):
2018-04-24 15:57:55 +00:00
# queryset = preferences.HomeOption.objects.all()
# serializer_class = serializers.HomeOptionSerializer
2018-04-22 18:28:01 +00:00
#
#
# class MailMessageOptionViewSet(viewsets.ReadOnlyModelViewSet):
2018-04-24 15:57:55 +00:00
# queryset = preferences.MailMessageOption.objects.all()
# serializer_class = serializers.MailMessageOptionSerializer
2018-04-22 18:28:01 +00:00
2018-04-24 13:59:18 +00:00
# TOPOLOGIE APP
class StackViewSet(viewsets.ReadOnlyModelViewSet):
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-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-04-24 15:57:55 +00:00
queryset = topologie.Switch.objects.all()
serializer_class = serializers.SwitchSerializer
2018-04-24 13:59:18 +00:00
class ModelSwitchViewSet(viewsets.ReadOnlyModelViewSet):
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-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-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-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-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-04-24 15:57:55 +00:00
queryset = topologie.Room.objects.all()
serializer_class = serializers.RoomSerializer
2018-04-24 13:59:18 +00:00
2018-04-21 22:36:10 +00:00
# USER APP
2018-04-19 22:00:49 +00:00
class UserViewSet(viewsets.ReadOnlyModelViewSet):
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 ClubViewSet(viewsets.ReadOnlyModelViewSet):
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-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-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-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-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-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-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-04-24 15:57:55 +00:00
queryset = users.Whitelist.objects.all()
serializer_class = serializers.WhitelistSerializer
2018-04-19 22:00:49 +00:00
# Subclass the standard rest_framework.auth_token.views.ObtainAuthToken
# in order to renew the lease of the token and add expiration time
class ObtainExpiringAuthToken(ObtainAuthToken):
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_date': token.created + token_duration
})