Commit d82e1af4 authored by Dalahro's avatar Dalahro

parce que chirac est compliqué

parent 1cdbc6ae
#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')
......@@ -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'),
]
......@@ -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'])
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment