Ce serveur Gitlab sera éteint le 30 juin 2020, pensez à migrer vos projets vers les serveurs gitlab-research.centralesupelec.fr et gitlab-student.centralesupelec.fr !

views.py 11.4 KB
Newer Older
Maël Kervella's avatar
Maël Kervella committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
# 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.
"""
26

27
import datetime
28

29 30 31 32
from django.conf import settings

from rest_framework.authtoken.views import ObtainAuthToken
from rest_framework.authtoken.models import Token
Maël Kervella's avatar
Maël Kervella committed
33
from rest_framework.response import Response
34
from rest_framework import viewsets, generics
Maël Kervella's avatar
Maël Kervella committed
35

Maël Kervella's avatar
Maël Kervella committed
36 37 38 39 40
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
41

42 43
from re2o.utils import all_active_interfaces

Maël Kervella's avatar
Maël Kervella committed
44
from . import serializers
45

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

47
# COTISATIONS APP
48 49 50


class FactureViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
51 52
    queryset = cotisations.Facture.objects.all()
    serializer_class = serializers.FactureSerializer
53 54 55


class VenteViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
56 57
    queryset = cotisations.Vente.objects.all()
    serializer_class = serializers.VenteSerializer
58 59 60


class ArticleViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
61 62
    queryset = cotisations.Article.objects.all()
    serializer_class = serializers.ArticleSerializer
63 64 65


class BanqueViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
66 67
    queryset = cotisations.Banque.objects.all()
    serializer_class = serializers.BanqueSerializer
68 69 70


class PaiementViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
71 72
    queryset = cotisations.Paiement.objects.all()
    serializer_class = serializers.PaiementSerializer
73 74 75


class CotisationViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
76 77
    queryset = cotisations.Cotisation.objects.all()
    serializer_class = serializers.CotisationSerializer
78 79


80 81 82 83
# MACHINES APP


class MachineViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
84 85
    queryset = machines.Machine.objects.all()
    serializer_class = serializers.MachineSerializer
86 87 88


class MachineTypeViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
89 90
    queryset = machines.MachineType.objects.all()
    serializer_class = serializers.MachineTypeSerializer
91 92 93


class IpTypeViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
94 95
    queryset = machines.IpType.objects.all()
    serializer_class = serializers.IpTypeSerializer
96 97 98


class VlanViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
99 100
    queryset = machines.Vlan.objects.all()
    serializer_class = serializers.VlanSerializer
101 102 103


class NasViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
104 105
    queryset = machines.Nas.objects.all()
    serializer_class = serializers.NasSerializer
106 107 108


class SOAViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
109 110
    queryset = machines.SOA.objects.all()
    serializer_class = serializers.SOASerializer
111 112 113


class ExtensionViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
114 115
    queryset = machines.Extension.objects.all()
    serializer_class = serializers.ExtensionSerializer
116 117 118


class MxViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
119 120
    queryset = machines.Mx.objects.all()
    serializer_class = serializers.MxSerializer
121 122 123


class NsViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
124 125
    queryset = machines.Ns.objects.all()
    serializer_class = serializers.NsSerializer
126 127 128


class TxtViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
129 130
    queryset = machines.Txt.objects.all()
    serializer_class = serializers.TxtSerializer
131 132 133


class SrvViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
134 135
    queryset = machines.Srv.objects.all()
    serializer_class = serializers.SrvSerializer
136 137 138


class InterfaceViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
139 140
    queryset = machines.Interface.objects.all()
    serializer_class = serializers.InterfaceSerializer
141 142 143


class Ipv6ListViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
144 145
    queryset = machines.Ipv6List.objects.all()
    serializer_class = serializers.Ipv6ListSerializer
146 147 148


class DomainViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
149 150
    queryset = machines.Domain.objects.all()
    serializer_class = serializers.DomainSerializer
151 152 153


class IpListViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
154 155
    queryset = machines.IpList.objects.all()
    serializer_class = serializers.IpListSerializer
156 157 158


class ServiceViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
159 160
    queryset = machines.Service.objects.all()
    serializer_class = serializers.ServiceSerializer
161 162


163
class ServiceLinkViewSet(viewsets.ModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
164 165
    queryset = machines.Service_link.objects.all()
    serializer_class = serializers.ServiceLinkSerializer
166 167 168


class OuverturePortListViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
169 170
    queryset = machines.OuverturePortList.objects.all()
    serializer_class = serializers.OuverturePortListSerializer
171 172 173


class OuverturePortViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
174 175
    queryset = machines.OuverturePort.objects.all()
    serializer_class = serializers.OuverturePortSerializer
176 177


178 179 180
# PREFERENCES APP

# class OptionalUserViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
181 182
#     queryset = preferences.OptionalUser.objects.all()
#     serializer_class = serializers.OptionalUserSerializer
183 184 185
# 
# 
# class OptionalMachineViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
186 187
#     queryset = preferences.OptionalMachine.objects.all()
#     serializer_class = serializers.OptionalMachineSerializer
188 189 190
# 
# 
# class OptionalTopologieViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
191 192
#     queryset = preferences.OptionalTopologie.objects.all()
#     serializer_class = serializers.OptionalTopologieSerializer
193 194 195
# 
# 
# class GeneralOptionViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
196 197
#     queryset = preferences.GeneralOption.objects.all()
#     serializer_class = serializers.GeneralOptionSerializer
198 199 200
# 
# 
# class ServiceOptionViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
201 202
#     queryset = preferences.ServiceOption.objects.all()
#     serializer_class = serializers.ServiceOptionSerializer
203 204 205
# 
# 
# class AssoOptionViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
206 207
#     queryset = preferences.AssoOption.objects.all()
#     serializer_class = serializers.AssoOptionSerializer
208 209 210
# 
# 
# class HomeOptionViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
211 212
#     queryset = preferences.HomeOption.objects.all()
#     serializer_class = serializers.HomeOptionSerializer
213 214 215
# 
# 
# class MailMessageOptionViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
216 217
#     queryset = preferences.MailMessageOption.objects.all()
#     serializer_class = serializers.MailMessageOptionSerializer
218 219


220 221 222 223
# TOPOLOGIE APP


class StackViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
224 225
    queryset = topologie.Stack.objects.all()
    serializer_class = serializers.StackSerializer
226 227 228


class AccessPointViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
229 230
    queryset = topologie.AccessPoint.objects.all()
    serializer_class = serializers.AccessPointSerializer
231 232 233


class SwitchViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
234 235
    queryset = topologie.Switch.objects.all()
    serializer_class = serializers.SwitchSerializer
236 237 238


class ModelSwitchViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
239 240
    queryset = topologie.ModelSwitch.objects.all()
    serializer_class = serializers.ModelSwitchSerializer
241 242 243


class ConstructorSwitchViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
244 245
    queryset = topologie.ConstructorSwitch.objects.all()
    serializer_class = serializers.ConstructorSwitchSerializer
246 247 248


class SwitchBayViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
249 250
    queryset = topologie.SwitchBay.objects.all()
    serializer_class = serializers.SwitchBaySerializer
251 252 253


class BuildingViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
254 255
    queryset = topologie.Building.objects.all()
    serializer_class = serializers.BuildingSerializer
256 257 258


class SwitchPortViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
259 260
    queryset = topologie.Port.objects.all()
    serializer_class = serializers.SwitchPortSerializer
261 262 263


class RoomViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
264 265
    queryset = topologie.Room.objects.all()
    serializer_class = serializers.RoomSerializer
266 267


268 269 270
# USER APP


Maël Kervella's avatar
Maël Kervella committed
271
class UserViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
272 273
    queryset = users.User.objects.all()
    serializer_class = serializers.UserSerializer
Maël Kervella's avatar
Maël Kervella committed
274 275 276


class ClubViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
277 278
    queryset = users.Club.objects.all()
    serializer_class = serializers.ClubSerializer
Maël Kervella's avatar
Maël Kervella committed
279 280 281


class AdherentViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
282 283
    queryset = users.Adherent.objects.all()
    serializer_class = serializers.AdherentSerializer
Maël Kervella's avatar
Maël Kervella committed
284 285 286


class ServiceUserViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
287 288
    queryset = users.ServiceUser.objects.all()
    serializer_class = serializers.ServiceUserSerializer
Maël Kervella's avatar
Maël Kervella committed
289 290 291


class SchoolViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
292 293
    queryset = users.School.objects.all()
    serializer_class = serializers.SchoolSerializer
Maël Kervella's avatar
Maël Kervella committed
294 295 296


class ListRightViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
297 298
    queryset = users.ListRight.objects.all()
    serializer_class = serializers.ListRightSerializer
Maël Kervella's avatar
Maël Kervella committed
299 300 301


class ShellViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
302 303
    queryset = users.ListShell.objects.all()
    serializer_class = serializers.ShellSerializer
Maël Kervella's avatar
Maël Kervella committed
304 305 306


class BanViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
307 308
    queryset = users.Ban.objects.all()
    serializer_class = serializers.BanSerializer
Maël Kervella's avatar
Maël Kervella committed
309 310 311


class WhitelistViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
312 313
    queryset = users.Whitelist.objects.all()
    serializer_class = serializers.WhitelistSerializer
Maël Kervella's avatar
Maël Kervella committed
314

315

316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
# Services views


class ServiceRegenView(generics.ListAPIView):
    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


334 335 336 337 338 339 340
# DHCP views

class HostMacIpView(generics.ListAPIView):
    queryset = all_active_interfaces()
    serializer_class = serializers.HostMacIpSerializer


341 342
# DNS views

343
class DNSZonesViewSet(viewsets.ReadOnlyModelViewSet):
344 345 346 347
    queryset = machines.Extension.objects.all()
    serializer_class = serializers.DNSZonesSerializer


348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
# 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,
369
            'expiration': token.created + token_duration
370
        })