8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-12-24 16:03:47 +00:00

Rest pour le parefeu

This commit is contained in:
Dalahro 2016-07-18 23:22:59 +02:00
parent 60b741834f
commit ec8228d955
6 changed files with 46 additions and 6 deletions

View file

@ -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 :

View file

@ -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

9
machines/serializers.py Normal file
View file

@ -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')

View file

@ -16,5 +16,6 @@ urlpatterns = [
url(r'^edit_extension/(?P<extensionid>[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'),
]

View file

@ -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)

View file

@ -48,6 +48,7 @@ INSTALLED_APPS = (
'topologie',
'search',
'logs',
'rest_framework'
)
MIDDLEWARE_CLASSES = (