diff --git a/machines/serializers.py b/machines/serializers.py index e9fdde29..bf6130f2 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, Alias +from machines.models import Interface, IpType, Extension, IpList, MachineType, Alias, Mx, Ns class IpTypeField(serializers.RelatedField): def to_representation(self, value): @@ -23,24 +23,46 @@ class InterfaceSerializer(serializers.ModelSerializer): class ExtensionNameField(serializers.RelatedField): def to_representation(self, value): - return value.name + return value.alias -class TypeSerializer(serializers.ModelSerializer): - extension = ExtensionNameField(read_only=True) +class MxSerializer(serializers.ModelSerializer): + name = serializers.SerializerMethodField('get_alias_name') + zone = serializers.SerializerMethodField('get_zone_name') class Meta: - model = IpType - fields = ('type', 'extension', 'domaine_ip', 'domaine_range') + model = Mx + fields = ('zone', 'priority', 'name') + + def get_alias_name(self, obj): + return obj.name.alias + obj.name.extension.name + + def get_zone_name(self, obj): + return obj.zone.name + +class NsSerializer(serializers.ModelSerializer): + zone = serializers.SerializerMethodField('get_zone_name') + interface = serializers.SerializerMethodField('get_interface_name') -class InterfaceDNS_ExtensionSerializer(serializers.ModelSerializer): class Meta: - model = Interface - fields = ('ipv4', 'dns') + model = Ns + fields = ('zone', 'interface') + + def get_zone_name(self, obj): + return obj.zone.name + + def get_interface_name(self, obj): + return obj.interface.dns + obj.interface.ipv4.ip_type.extension.name class AliasSerializer(serializers.ModelSerializer): - interface_parent = InterfaceDNS_ExtensionSerializer(read_only=True) - extension = ExtensionNameField(read_only=True) + interface_parent = serializers.SerializerMethodField('get_interface_name') + extension = serializers.SerializerMethodField('get_zone_name') class Meta: model = Alias fields = ('interface_parent', 'alias', 'extension') + + def get_zone_name(self, obj): + return obj.extension.name + + def get_interface_name(self, obj): + return obj.interface_parent.dns + obj.interface_parent.ipv4.ip_type.extension.name diff --git a/machines/urls.py b/machines/urls.py index b52f83ff..212c84c0 100644 --- a/machines/urls.py +++ b/machines/urls.py @@ -44,4 +44,6 @@ urlpatterns = [ 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'), -] + url(r'^rest/mx/$', views.mx, name='mx'), + url(r'^rest/ns/$', views.ns, name='ns'), + ] diff --git a/machines/views.py b/machines/views.py index c6829b1f..9bf4255d 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, TypeSerializer, AliasSerializer +from machines.serializers import InterfaceSerializer, TypeSerializer, AliasSerializer, MxSerializer, NsSerializer from reversion import revisions as reversion @@ -667,14 +667,8 @@ def interface_list(request): @login_required @permission_required('serveur') def alias(request): - alias = Alias.objects.all() + alias = Alias.objects.filter(interface_parent=Interface.objects.exclude(ipv4=None)) seria = AliasSerializer(alias, many=True) - for d in seria.data: - if d["interface_parent"]["ipv4"]: - id = d["interface_parent"]["ipv4"] - ip_list = IpList.objects.get(pk=id) - d["interface_parent"]["extension"] = ip_list.ip_type.extension.name - d["interface_parent"].pop("ipv4") return JSONResponse(seria.data) @csrf_exempt @@ -685,6 +679,22 @@ def corresp(request): seria = TypeSerializer(type, many=True) return JSONResponse(seria.data) +@csrf_exempt +@login_required +@permission_required('serveur') +def mx(request): + mx = Mx.objects.all() + seria = MxSerializer(mx, many=True) + return JSONResponse(seria.data) + +@csrf_exempt +@login_required +@permission_required('serveur') +def ns(request): + ns = Ns.objects.filter(interface=Interface.objects.exclude(ipv4=None)) + seria = NsSerializer(ns, many=True) + return JSONResponse(seria.data) + @csrf_exempt @login_required @permission_required('serveur')