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 13.1 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, views
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
from re2o.utils import all_active_interfaces, all_has_access
43

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 APP
50 51 52


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


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


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


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


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


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


82 83 84 85
# MACHINES APP


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


180
# PREFERENCES APP
181 182
# Those views differ a bit because there is only one object
# to display, so we don't bother with the listing part
183

184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
class OptionalUserView(generics.RetrieveAPIView):
    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):
    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):
    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):
    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 ServiceViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = preferences.Service.objects.all()
    serializer_class = serializers.ServiceSerializer


class AssoOptionView(generics.RetrieveAPIView):
    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):
    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):
    permission_classes = (ACLPermission, )
    perms_map = {'GET' : [preferences.MailMessageOption.can_view_all]}
    serializer_class = serializers.MailMessageOptionSerializer

    def get_object(self):
        return preferences.MailMessageOption.objects.first()
250 251


252 253 254 255
# TOPOLOGIE APP


class StackViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
256 257
    queryset = topologie.Stack.objects.all()
    serializer_class = serializers.StackSerializer
258 259 260


class AccessPointViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
261 262
    queryset = topologie.AccessPoint.objects.all()
    serializer_class = serializers.AccessPointSerializer
263 264 265


class SwitchViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
266 267
    queryset = topologie.Switch.objects.all()
    serializer_class = serializers.SwitchSerializer
268 269 270


class ModelSwitchViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
271 272
    queryset = topologie.ModelSwitch.objects.all()
    serializer_class = serializers.ModelSwitchSerializer
273 274 275


class ConstructorSwitchViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
276 277
    queryset = topologie.ConstructorSwitch.objects.all()
    serializer_class = serializers.ConstructorSwitchSerializer
278 279 280


class SwitchBayViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
281 282
    queryset = topologie.SwitchBay.objects.all()
    serializer_class = serializers.SwitchBaySerializer
283 284 285


class BuildingViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
286 287
    queryset = topologie.Building.objects.all()
    serializer_class = serializers.BuildingSerializer
288 289 290


class SwitchPortViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
291 292
    queryset = topologie.Port.objects.all()
    serializer_class = serializers.SwitchPortSerializer
293 294 295


class RoomViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
296 297
    queryset = topologie.Room.objects.all()
    serializer_class = serializers.RoomSerializer
298 299


300 301 302
# USER APP


Maël Kervella's avatar
Maël Kervella committed
303
class UserViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
304 305
    queryset = users.User.objects.all()
    serializer_class = serializers.UserSerializer
Maël Kervella's avatar
Maël Kervella committed
306 307 308


class ClubViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
309 310
    queryset = users.Club.objects.all()
    serializer_class = serializers.ClubSerializer
Maël Kervella's avatar
Maël Kervella committed
311 312 313


class AdherentViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
314 315
    queryset = users.Adherent.objects.all()
    serializer_class = serializers.AdherentSerializer
Maël Kervella's avatar
Maël Kervella committed
316 317 318


class ServiceUserViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
319 320
    queryset = users.ServiceUser.objects.all()
    serializer_class = serializers.ServiceUserSerializer
Maël Kervella's avatar
Maël Kervella committed
321 322 323


class SchoolViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
324 325
    queryset = users.School.objects.all()
    serializer_class = serializers.SchoolSerializer
Maël Kervella's avatar
Maël Kervella committed
326 327 328


class ListRightViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
329 330
    queryset = users.ListRight.objects.all()
    serializer_class = serializers.ListRightSerializer
Maël Kervella's avatar
Maël Kervella committed
331 332 333


class ShellViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
334 335
    queryset = users.ListShell.objects.all()
    serializer_class = serializers.ShellSerializer
Maël Kervella's avatar
Maël Kervella committed
336 337 338


class BanViewSet(viewsets.ReadOnlyModelViewSet):
Maël Kervella's avatar
Maël Kervella committed
339 340
    queryset = users.Ban.objects.all()
    serializer_class = serializers.BanSerializer
Maël Kervella's avatar
Maël Kervella committed
341 342 343


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

347

348 349 350
# Services views


351
class ServiceRegenViewSet(viewsets.ModelViewSet):
352 353 354 355 356 357 358 359 360 361 362 363 364 365
    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


366 367 368 369 370 371 372
# DHCP views

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


373 374
# DNS views

375
class DNSZonesView(generics.ListAPIView):
376 377 378 379
    queryset = machines.Extension.objects.all()
    serializer_class = serializers.DNSZonesSerializer


380 381 382 383 384
# Mailing views


class StandardMailingView(views.APIView):
    pagination_class = PageSizedPagination
385 386
    permission_classes = (ACLPermission, )
    perms_map = {'GET' : [users.User.can_view_all]}
387 388

    def get(self, request, format=None):
389
        adherents_data = serializers.MailingMemberSerializer(all_has_access(), many=True).data
390 391 392 393 394 395 396 397 398 399 400
        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):
    queryset = users.Club.objects.all()
    serializer_class = serializers.MailingSerializer


401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
# 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,
422
            'expiration': token.created + token_duration
423
        })