From 95acdb2ecd987d01e022c589b533b86c3d6057c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Kervella?= Date: Tue, 24 Apr 2018 13:59:18 +0000 Subject: [PATCH] API support for app topologie --- api/serializers.py | 111 +++++++++++++++++++++++++++++++++++++++++++++ api/urls.py | 10 ++++ api/views.py | 69 ++++++++++++++++++++++++++++ 3 files changed, 190 insertions(+) diff --git a/api/serializers.py b/api/serializers.py index 888891f0..192125a7 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -64,6 +64,17 @@ from preferences.models import ( ) # Avoid duplicate names from preferences.models import Service as ServiceOption +from topologie.models import ( + Stack, + AccessPoint, + Switch, + ModelSwitch, + ConstructorSwitch, + SwitchBay, + Building, + Room +) +from topologie.models import Port as SwitchPort from users.models import ( User, Club, @@ -456,6 +467,106 @@ class OuverturePortSerializer(serializers.HyperlinkedModelSerializer): # } + +# TOPOLOGIE APP + + +class StackSerializer(serializers.HyperlinkedModelSerializer): + class Meta: + model = Stack + fields = ('name', 'stack_id', 'details', 'member_id_min', + 'member_id_max', 'api_url') + extra_kwargs = { + 'api_url': {'view_name': 'api:stack-detail'} + } + + +class AccessPointSerializer(serializers.HyperlinkedModelSerializer): + class Meta: + model = AccessPoint + fields = ('user', 'name', 'active', 'location', 'api_url') + extra_kwargs = { + 'user': {'view_name': 'api:user-detail'}, + 'api_url': {'view_name': 'api:accesspoint-detail'} + } + + +class SwitchSerializer(serializers.HyperlinkedModelSerializer): + port_amount = serializers.IntegerField(source='number') + class Meta: + model = Switch + fields = ('port_amount', 'stack', 'stack_member_id', 'model', + 'switchbay', 'api_url') + extra_kwargs = { + 'stack': {'view_name': 'api:stack-detail'}, + 'model': {'view_name': 'api:modelswitch-detail'}, + 'switchbay': {'view_name': 'api:switchbay-detail'}, + 'api_url': {'view_name': 'api:switch-detail'} + } + + +class ModelSwitchSerializer(serializers.HyperlinkedModelSerializer): + class Meta: + model = ModelSwitch + fields = ('reference', 'constructor', 'api_url') + extra_kwargs = { + 'constructor': {'view_name': 'api:constructorswitch-detail'}, + 'api_url': {'view_name': 'api:modelswitch-detail'} + } + + +class ConstructorSwitchSerializer(serializers.HyperlinkedModelSerializer): + class Meta: + model = ConstructorSwitch + fields = ('name', 'api_url') + extra_kwargs = { + 'api_url': {'view_name': 'api:constructorswitch-detail'} + } + + +class SwitchBaySerializer(serializers.HyperlinkedModelSerializer): + class Meta: + model = SwitchBay + fields = ('name', 'building', 'info', 'api_url') + extra_kwargs = { + 'building': {'view_name': 'api:building-detail'}, + 'api_url': {'view_name': 'api:switchbay-detail'} + } + + +class BuildingSerializer(serializers.HyperlinkedModelSerializer): + class Meta: + model = Building + fields = ('name', 'api_url') + extra_kwargs = { + 'api_url': {'view_name': 'api:building-detail'} + } + + +class SwitchPortSerializer(serializers.HyperlinkedModelSerializer): + class Meta: + model = SwitchPort + fields = ('switch', 'port', 'room', 'machine_interface', 'related', + 'radius', 'vlan_force', 'details', 'api_url') + extra_kwargs = { + 'switch': {'view_name': 'api:switch-detail'}, + 'room': {'view_name': 'api:room-detail'}, + 'machine_interface': {'view_name': 'api:interface-detail'}, + 'related': {'view_name': 'api:switchport-detail'}, + 'vlan_force': {'view_name': 'api:vlan-detail'}, + 'api_url': {'view_name': 'api:switchport-detail'} + } + + +class RoomSerializer(serializers.HyperlinkedModelSerializer): + class Meta: + model = Room + fields = ('name', 'details', 'api_url') + extra_kwargs = { + 'api_url': {'view_name': 'api:room-detail'} + } + + # USERS APP diff --git a/api/urls.py b/api/urls.py index c2ba0c2a..f01e6a3e 100644 --- a/api/urls.py +++ b/api/urls.py @@ -66,6 +66,16 @@ router.register(r'ouvertureports', views.OuverturePortViewSet) #router.register(r'assooption', views.AssoOptionSerializer) #router.register(r'homeoption', views.HomeOptionSerializer) #router.register(r'mailmessageoption', views.MailMessageOptionSerializer) +# TOPOLOGIE APP +router.register(r'stack', views.StackViewSet) +router.register(r'acesspoint', views.AccessPointViewSet) +router.register(r'switch', views.SwitchViewSet) +router.register(r'modelswitch', views.ModelSwitchViewSet) +router.register(r'constructorswitch', views.ConstructorSwitchViewSet) +router.register(r'switchbay', views.SwitchBayViewSet) +router.register(r'building', views.BuildingViewSet) +router.register(r'switchport', views.SwitchPortViewSet, 'switchport') +router.register(r'room', views.RoomViewSet) # USERS APP router.register(r'users', views.UserViewSet) router.register(r'clubs', views.ClubViewSet) diff --git a/api/views.py b/api/views.py index f970745c..34f8a947 100644 --- a/api/views.py +++ b/api/views.py @@ -74,6 +74,17 @@ from machines.models import ( # ) # # Avoid duplicate names # from preferences.models import Service as ServiceOption +from topologie.models import ( + Stack, + AccessPoint, + Switch, + ModelSwitch, + ConstructorSwitch, + SwitchBay, + Building, + Room +) +from topologie.models import Port as SwitchPort from users.models import ( User, Club, @@ -123,6 +134,16 @@ from .serializers import ( # AssoOptionSerializer, # HomeOptionSerializer, # MailMessageOptionSerializer, + # TOPOLOGIE APP + StackSerializer, + AccessPointSerializer, + SwitchSerializer, + ModelSwitchSerializer, + ConstructorSwitchSerializer, + SwitchBaySerializer, + BuildingSerializer, + SwitchPortSerializer, + RoomSerializer, # USERS APP UserSerializer, ClubSerializer, @@ -309,6 +330,54 @@ class OuverturePortViewSet(viewsets.ReadOnlyModelViewSet): # serializer_class = MailMessageOptionSerializer +# TOPOLOGIE APP + + +class StackViewSet(viewsets.ReadOnlyModelViewSet): + queryset = Stack.objects.all() + serializer_class = StackSerializer + + +class AccessPointViewSet(viewsets.ReadOnlyModelViewSet): + queryset = AccessPoint.objects.all() + serializer_class = AccessPointSerializer + + +class SwitchViewSet(viewsets.ReadOnlyModelViewSet): + queryset = Switch.objects.all() + serializer_class = SwitchSerializer + + +class ModelSwitchViewSet(viewsets.ReadOnlyModelViewSet): + queryset = ModelSwitch.objects.all() + serializer_class = ModelSwitchSerializer + + +class ConstructorSwitchViewSet(viewsets.ReadOnlyModelViewSet): + queryset = ConstructorSwitch.objects.all() + serializer_class = ConstructorSwitchSerializer + + +class SwitchBayViewSet(viewsets.ReadOnlyModelViewSet): + queryset = SwitchBay.objects.all() + serializer_class = SwitchBaySerializer + + +class BuildingViewSet(viewsets.ReadOnlyModelViewSet): + queryset = Building.objects.all() + serializer_class = BuildingSerializer + + +class SwitchPortViewSet(viewsets.ReadOnlyModelViewSet): + queryset = SwitchPort.objects.all() + serializer_class = SwitchPortSerializer + + +class RoomViewSet(viewsets.ReadOnlyModelViewSet): + queryset = Room.objects.all() + serializer_class = RoomSerializer + + # USER APP