From 32c882b23e67345e63cbcca7573088f7d22b81c4 Mon Sep 17 00:00:00 2001 From: Dalahro Date: Mon, 18 Jul 2016 23:22:59 +0200 Subject: [PATCH] Rest pour le parefeu --- README.md | 1 + machines/models.py | 12 ++++++------ machines/serializers.py | 9 +++++++++ machines/urls.py | 1 + machines/views.py | 28 ++++++++++++++++++++++++++++ re2o/settings.py | 1 + 6 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 machines/serializers.py diff --git a/README.md b/README.md index 0f873031..0f155de2 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ Dépendances : * python3-dateutil (jessie-backports) * texlive-latex-base * texlive-fonts-recommended + * python3-djangorestframework Moteur de db conseillé (mysql), postgresql fonctionne également. Pour mysql, il faut installer : diff --git a/machines/models.py b/machines/models.py index bf52c9e1..b8b62d4b 100644 --- a/machines/models.py +++ b/machines/models.py @@ -7,12 +7,6 @@ class Machine(models.Model): name = models.CharField(max_length=255, help_text="Optionnel", blank=True, null=True) active = models.BooleanField(default=True) - def is_active(self): - """ Renvoie si une interface doit avoir accès ou non """ - machine = self.machine - user = machine.user - return machine.active and user.has_access() - def __str__(self): return str(self.user) + ' - ' + str(self.id) + ' - ' + str(self.name) @@ -38,6 +32,12 @@ class Interface(models.Model): details = models.CharField(max_length=255, blank=True) dns = models.CharField(help_text="Obligatoire et unique, doit se terminer en %s et ne pas comporter d'autres points" % ", ".join(Extension.objects.values_list('name', flat=True)), max_length=255, unique=True) + def is_active(self): + """ Renvoie si une interface doit avoir accès ou non """ + machine = self.machine + user = self.machine.user + return machine.active and user.has_access() + def __str__(self): return self.dns diff --git a/machines/serializers.py b/machines/serializers.py new file mode 100644 index 00000000..2aa7db08 --- /dev/null +++ b/machines/serializers.py @@ -0,0 +1,9 @@ +from rest_framework import serializers +from machines.models import Interface + +class InterfaceSerializer( serializers.ModelSerializer): + class Meta: + model = Interface + fields = ('id','ipv4', 'mac_address') + + diff --git a/machines/urls.py b/machines/urls.py index f9761b55..efa3af9d 100644 --- a/machines/urls.py +++ b/machines/urls.py @@ -16,5 +16,6 @@ urlpatterns = [ url(r'^edit_extension/(?P[0-9]+)$', views.edit_extension, name='edit-extension'), url(r'^del_extension/$', views.del_extension, name='del-extension'), url(r'^index_extension/$', views.index_extension, name='index-extension'), + url(r'^rest/mac-ip/$', views.interface_list, name='interface-list'), url(r'^$', views.index, name='index'), ] diff --git a/machines/views.py b/machines/views.py index f714320e..35481480 100644 --- a/machines/views.py +++ b/machines/views.py @@ -1,6 +1,7 @@ # App de gestion des machines pour re2o # Gabriel Détraz # Gplv2 +from django.http import HttpResponse from django.shortcuts import render, redirect from django.shortcuts import render_to_response, get_object_or_404 from django.core.context_processors import csrf @@ -10,6 +11,10 @@ from django.contrib.auth.decorators import login_required, permission_required from django.db.models import ProtectedError from django.forms import ValidationError +from rest_framework.renderers import JSONRenderer +from machines.serializers import InterfaceSerializer + + import re from .forms import NewMachineForm, EditMachineForm, EditInterfaceForm, AddInterfaceForm, MachineTypeForm, DelMachineTypeForm, ExtensionForm, DelExtensionForm, BaseEditInterfaceForm, BaseEditMachineForm from .models import Machine, Interface, IpList, MachineType, Extension @@ -280,3 +285,26 @@ def index_machinetype(request): def index_extension(request): extension_list = Extension.objects.order_by('name') return render(request, 'machines/index_extension.html', {'extension_list':extension_list}) + +""" Framework Rest """ + +class JSONResponse(HttpResponse): + def __init__(self, data, **kwargs): + datas=[] + for d in data: + interface = Interface.objects.get(pk=d["id"]) + d.pop("id") + if d["ipv4"] and interface.is_active(): + d["ipv4"]= IpList.objects.get(pk=d["ipv4"]).__str__() + datas.append(d) + content = JSONRenderer().render(datas) + kwargs['content_type'] = 'application/json' + super(JSONResponse, self).__init__(content, **kwargs) + + +def interface_list(request): + interfaces = Interface.objects.all() + seria = InterfaceSerializer(interfaces, many=True) + return JSONResponse(seria.data) + + diff --git a/re2o/settings.py b/re2o/settings.py index 5cb9903a..d6d2019a 100644 --- a/re2o/settings.py +++ b/re2o/settings.py @@ -48,6 +48,7 @@ INSTALLED_APPS = ( 'topologie', 'search', 'logs', + 'rest_framework' ) MIDDLEWARE_CLASSES = (