diff --git a/machines/serializers.py b/machines/serializers.py index fc258a4d..709ad627 100644 --- a/machines/serializers.py +++ b/machines/serializers.py @@ -1,16 +1,35 @@ #Augustin Lemesle from rest_framework import serializers -from machines.models import Interface, IpType +from machines.models import Interface, IpType, Extension, IpList, MachineType -class InterfaceSerializer( serializers.ModelSerializer): +class IpTypeField(serializers.RelatedField): + def to_representation(self, value): + return value.type + +class IpListSerializer(serializers.ModelSerializer): + ip_type = IpTypeField(read_only=True) + + class Meta: + model = IpList + fields = ('ipv4', 'ip_type') + +class InterfaceSerializer(serializers.ModelSerializer): + ipv4 = IpListSerializer(read_only=True) + class Meta: model = Interface - fields = ('ipv4', 'mac_address', 'dns', 'type') + fields = ('ipv4', 'mac_address', 'dns') + +class ExtensionNameField(serializers.RelatedField): + def to_reprsentation(self,value): + return value.name class TypeSerializer(serializers.ModelSerializer): + extension = ExtensionNameField(read_only=True) + class Meta: model = IpType - fields = ('type', 'extension') + fields = ('type', 'extension', 'domaine_ip', 'domaine_range') diff --git a/machines/views.py b/machines/views.py index b855a4cb..6aaed798 100644 --- a/machines/views.py +++ b/machines/views.py @@ -449,26 +449,10 @@ def history(request, object, id): class JSONResponse(HttpResponse): def __init__(self, data, **kwargs): - for d in data: - if d["ipv4"]: - 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') @@ -510,7 +494,7 @@ def mac_ip_dns(request): @permission_required('serveur') def corresp(request): seria = type_list(request) - return JSONRespType(seria) + return JSONResponse(seria) @csrf_exempt def login_user(request):