From d82e1af4fd610439b82dd322e87c8e5303fb2b46 Mon Sep 17 00:00:00 2001 From: Dalahro Date: Sat, 12 Nov 2016 16:12:24 +0100 Subject: [PATCH] =?UTF-8?q?parce=20que=20chirac=20est=20compliqu=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- machines/serializers.py | 8 +++++++- machines/urls.py | 1 + machines/views.py | 28 +++++++++++++++++++++++++++- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/machines/serializers.py b/machines/serializers.py index 7b058d95..fc258a4d 100644 --- a/machines/serializers.py +++ b/machines/serializers.py @@ -1,10 +1,16 @@ +#Augustin Lemesle + from rest_framework import serializers -from machines.models import Interface +from machines.models import Interface, IpType class InterfaceSerializer( serializers.ModelSerializer): class Meta: model = Interface fields = ('ipv4', 'mac_address', 'dns', 'type') +class TypeSerializer(serializers.ModelSerializer): + class Meta: + model = IpType + fields = ('type', 'extension') diff --git a/machines/urls.py b/machines/urls.py index 1eec0fff..3080427e 100644 --- a/machines/urls.py +++ b/machines/urls.py @@ -29,4 +29,5 @@ 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/corresp/$', views.corresp, name='corresp'), ] diff --git a/machines/views.py b/machines/views.py index d33c30ba..b855a4cb 100644 --- a/machines/views.py +++ b/machines/views.py @@ -16,7 +16,7 @@ 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 +from machines.serializers import InterfaceSerializer, TypeSerializer from reversion import revisions as reversion @@ -454,10 +454,21 @@ class JSONResponse(HttpResponse): ip = IpList.objects.get(pk=d["ipv4"]) d["ipv4"]= ip.__str__() d["type"]= ip.ip_type.__str__() + d["extension"] = ip.ip_type.extension.__str__() content = JSONRenderer().render(data) kwargs['content_type'] = 'application/json' super(JSONResponse, self).__init__(content, **kwargs) +class JSONRespType(HttpResponse): + def __init__(self, data, **kwargs): + for d in data: + if d["extension"]: + extension = Extension.objects.get(pk=d["extension"]) + d["extension"]=extension.__str__() + content = JSONRenderer().render(data) + kwargs['content_type'] = 'application/json' + super(JSONRespType, self).__init__(content, **kwargs) + @csrf_exempt @login_required @permission_required('serveur') @@ -470,6 +481,14 @@ def interface_list(request): seria = InterfaceSerializer(interface, many=True) return seria.data +@csrf_exempt +@login_required +@permission_required('serveur') +def type_list(request): + type = IpType.objects.all() + seria = TypeSerializer(type, many=True) + return seria.data + @csrf_exempt @login_required @permission_required('serveur') @@ -486,6 +505,13 @@ 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 JSONRespType(seria) + @csrf_exempt def login_user(request): user = authenticate(username=request.POST['username'], password=request.POST['password'])