From aac17cbd92c1e93886d109f1462455e70a6a49d9 Mon Sep 17 00:00:00 2001 From: Dalahro Date: Fri, 18 Nov 2016 18:15:57 +0100 Subject: [PATCH] Encore une vue pour Chirac, vue d'alis --- machines/serializers.py | 21 +++++++++++++++++++-- machines/urls.py | 1 + machines/views.py | 23 ++++++++++++----------- 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/machines/serializers.py b/machines/serializers.py index 709ad627..40c38a72 100644 --- a/machines/serializers.py +++ b/machines/serializers.py @@ -1,7 +1,7 @@ #Augustin Lemesle from rest_framework import serializers -from machines.models import Interface, IpType, Extension, IpList, MachineType +from machines.models import Interface, IpType, Extension, IpList, MachineType, Alias class IpTypeField(serializers.RelatedField): def to_representation(self, value): @@ -22,7 +22,7 @@ class InterfaceSerializer(serializers.ModelSerializer): fields = ('ipv4', 'mac_address', 'dns') class ExtensionNameField(serializers.RelatedField): - def to_reprsentation(self,value): + def to_reprsentation(self, value): return value.name class TypeSerializer(serializers.ModelSerializer): @@ -32,4 +32,21 @@ class TypeSerializer(serializers.ModelSerializer): model = IpType fields = ('type', 'extension', 'domaine_ip', 'domaine_range') +class IpList_ExtensionField(serializers.RelatedField): + def to_representation(self, value): + return value.ipv4.ip_type.extension.name +class InterfaceDNS_ExtensionSerializer(serializers.ModelSerializer): + ipv4 = IpList_ExtensionField(read_only=True) + + class Meta: + model = Interface + fields = ('ipv4', 'dns') + +class AliasSerializer(serializers.ModelSerializer): + interface_parent = InterfaceDNS_ExtensionSerializer(read_only=True) + extension = ExtensionNameField(read_only=True) + + class Meta: + model = Alias + fields = ('interface_parent', 'alias', 'extension') diff --git a/machines/urls.py b/machines/urls.py index 3080427e..78d3b671 100644 --- a/machines/urls.py +++ b/machines/urls.py @@ -29,5 +29,6 @@ urlpatterns = [ url(r'^rest/mac-ip/$', views.mac_ip, name='mac-ip'), url(r'^rest/login/$', views.login_user, name='login'), url(r'^rest/mac-ip-dns/$', views.mac_ip_dns, name='mac-ip-dns'), + url(r'^rest/alias/$', views.alias, name='alias'), url(r'^rest/corresp/$', views.corresp, name='corresp'), ] diff --git a/machines/views.py b/machines/views.py index 6aaed798..57425dbd 100644 --- a/machines/views.py +++ b/machines/views.py @@ -16,12 +16,12 @@ from django.contrib.auth import authenticate, login from django.views.decorators.csrf import csrf_exempt from rest_framework.renderers import JSONRenderer -from machines.serializers import InterfaceSerializer, TypeSerializer +from machines.serializers import InterfaceSerializer, TypeSerializer, AliasSerializer from reversion import revisions as reversion import re -from .forms import NewMachineForm, EditMachineForm, EditInterfaceForm, AddInterfaceForm, MachineTypeForm, DelMachineTypeForm, ExtensionForm, DelExtensionForm, BaseEditInterfaceForm, BaseEditMachineForm +from .forms import NewMachineForm, EditMachineForm, EditInterfaceForm, AddInterfaceForm, MachineTypeForm, DelMachineTypeForm, ExtensionForm, DelExtensionForm, BaseEditInterfaceForm, BaseEditMachineForm, Alias from .forms import IpTypeForm, DelIpTypeForm, NewAliasForm, EditAliasFullForm from .models import IpType, Machine, Interface, IpList, MachineType, Extension from users.models import User @@ -468,10 +468,18 @@ def interface_list(request): @csrf_exempt @login_required @permission_required('serveur') -def type_list(request): +def alias(request): + alias = Alias.objects.all() + seria = AliasSerializer(alias, many=True) + return JSONResponse(seria.data) + +@csrf_exempt +@login_required +@permission_required('serveur') +def corresp(request): type = IpType.objects.all() seria = TypeSerializer(type, many=True) - return seria.data + return JSONResponse(seria.data) @csrf_exempt @login_required @@ -489,13 +497,6 @@ def mac_ip_dns(request): seria = interface_list(request) return JSONResponse(seria) -@csrf_exempt -@login_required -@permission_required('serveur') -def corresp(request): - seria = type_list(request) - return JSONResponse(seria) - @csrf_exempt def login_user(request): user = authenticate(username=request.POST['username'], password=request.POST['password'])