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 12.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 181 182
# PREFERENCES APP

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


222 223 224 225
# TOPOLOGIE APP


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


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


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


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


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


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


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


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


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


270 271 272
# USER APP


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


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


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


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


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


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


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


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


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

317

318 319 320
# Services views


321
class ServiceRegenViewSet(viewsets.ModelViewSet):
322 323 324 325 326 327 328 329 330 331 332 333 334 335
    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


336 337 338 339 340 341 342
# DHCP views

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


343 344
# DNS views

345
class DNSZonesView(generics.ListAPIView):
346 347 348 349
    queryset = machines.Extension.objects.all()
    serializer_class = serializers.DNSZonesSerializer


350 351 352 353 354
# Mailing views


class StandardMailingView(views.APIView):
    pagination_class = PageSizedPagination
355 356
    permission_classes = (ACLPermission, )
    perms_map = {'GET' : [users.User.can_view_all]}
357 358

    def get(self, request, format=None):
359
        adherents_data = serializers.MailingMemberSerializer(all_has_access(), many=True).data
360 361 362 363 364 365 366 367 368 369 370
        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


371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
# 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,
392
            'expiration': token.created + token_duration
393
        })