From 7171891c02af12addf73a95da22eea7ea9fd42e7 Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Tue, 3 Oct 2017 04:42:49 +0200 Subject: [PATCH] Export des ipv6 en rest --- machines/serializers.py | 19 +++++++++++++++++++ machines/views.py | 12 ++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/machines/serializers.py b/machines/serializers.py index bfe5f295..51daa4b5 100644 --- a/machines/serializers.py +++ b/machines/serializers.py @@ -55,6 +55,25 @@ class InterfaceSerializer(serializers.ModelSerializer): def get_macaddress(self, obj): return str(obj.mac_address) +class FullInterfaceSerializer(serializers.ModelSerializer): + ipv4 = IpListSerializer(read_only=True) + mac_address = serializers.SerializerMethodField('get_macaddress') + domain = serializers.SerializerMethodField('get_dns') + extension = serializers.SerializerMethodField('get_interface_extension') + + class Meta: + model = Interface + fields = ('ipv4', 'ipv6', 'mac_address', 'domain', 'extension') + + def get_dns(self, obj): + return obj.domain.name + + def get_interface_extension(self, obj): + return obj.domain.extension.name + + def get_macaddress(self, obj): + return str(obj.mac_address) + class ExtensionNameField(serializers.RelatedField): def to_representation(self, value): return value.name diff --git a/machines/views.py b/machines/views.py index 7dc4a3ca..694bc2aa 100644 --- a/machines/views.py +++ b/machines/views.py @@ -41,7 +41,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, TypeSerializer, DomainSerializer, TextSerializer, MxSerializer, ExtensionSerializer, ServiceServersSerializer, NsSerializer +from machines.serializers import FullInterfaceSerializer, InterfaceSerializer, TypeSerializer, DomainSerializer, TextSerializer, MxSerializer, ExtensionSerializer, ServiceServersSerializer, NsSerializer from reversion import revisions as reversion from reversion.models import Version @@ -1027,6 +1027,14 @@ def mac_ip_list(request): seria = InterfaceSerializer(interfaces, many=True) return seria.data +@csrf_exempt +@login_required +@permission_required('serveur') +def full_mac_ip_list(request): + interfaces = all_active_assigned_interfaces() + seria = FullInterfaceSerializer(interfaces, many=True) + return seria.data + @csrf_exempt @login_required @permission_required('serveur') @@ -1086,7 +1094,7 @@ def mac_ip(request): @login_required @permission_required('serveur') def mac_ip_dns(request): - seria = mac_ip_list(request) + seria = full_mac_ip_list(request) return JSONResponse(seria) @csrf_exempt