From 7bc0f2b7035cfa3dda6158aaf8fba907c441f050 Mon Sep 17 00:00:00 2001 From: LEVY-FALK Hugo Date: Thu, 26 Oct 2017 10:41:48 +0200 Subject: [PATCH] =?UTF-8?q?Ajoute=20la=20possibilit=C3=A9=20de=20cr=C3=A9e?= =?UTF-8?q?r=20une=20s=C3=A9rie=20de=20ports=20pour=20un=20switch.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- topologie/forms.py | 12 +++- topologie/models.py | 1 - topologie/templates/topologie/aff_switch.html | 1 + topologie/urls.py | 1 + topologie/views.py | 55 +++++++++++++++++++ 5 files changed, 68 insertions(+), 2 deletions(-) diff --git a/topologie/forms.py b/topologie/forms.py index c8e39d6a..958c6008 100644 --- a/topologie/forms.py +++ b/topologie/forms.py @@ -33,7 +33,8 @@ NewSwitchForm) from __future__ import unicode_literals from machines.models import Interface -from django.forms import ModelForm +from django import forms +from django.forms import ModelForm, Form from .models import Port, Switch, Room, Stack @@ -136,3 +137,12 @@ class EditRoomForm(ModelForm): def __init__(self, *args, **kwargs): prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(EditRoomForm, self).__init__(*args, prefix=prefix, **kwargs) + + +class CreatePortsForm(Form): + """Permet de créer une liste de ports pour un switch.""" + begin = forms.IntegerField(label="Début :", min_value=0) + end = forms.IntegerField(label="Fin :", min_value=0) + + + diff --git a/topologie/models.py b/topologie/models.py index adcc7a57..924002b3 100644 --- a/topologie/models.py +++ b/topologie/models.py @@ -120,7 +120,6 @@ class Switch(models.Model): raise ValidationError({'stack_member_id': "L'id dans la stack\ ne peut être nul"}) - class Port(models.Model): """ Definition d'un port. Relié à un switch(foreign_key), un port peut etre relié de manière exclusive à : diff --git a/topologie/templates/topologie/aff_switch.html b/topologie/templates/topologie/aff_switch.html index c0d6c55f..b533edec 100644 --- a/topologie/templates/topologie/aff_switch.html +++ b/topologie/templates/topologie/aff_switch.html @@ -53,6 +53,7 @@ with this program; if not, write to the Free Software Foundation, Inc., {% if is_infra %} {% include 'buttons/edit.html' with href='topologie:edit-switch' id=switch.pk %} {% include 'buttons/suppr.html' with href='machines:del-interface' id=switch.switch_interface.id %} + {% include 'buttons/add.html' with href='topologie:create-ports' id=switch.pk desc='Création de ports'%} {% endif %} diff --git a/topologie/urls.py b/topologie/urls.py index 77a78b97..4b39d5c5 100644 --- a/topologie/urls.py +++ b/topologie/urls.py @@ -35,6 +35,7 @@ from . import views urlpatterns = [ url(r'^$', views.index, name='index'), url(r'^new_switch/$', views.new_switch, name='new-switch'), + url(r'^create_ports/(?P[0-9]+)$', views.create_ports, name='create-ports'), url(r'^index_room/$', views.index_room, name='index-room'), url(r'^new_room/$', views.new_room, name='new-room'), url(r'^edit_room/(?P[0-9]+)$', views.edit_room, name='edit-room'), diff --git a/topologie/views.py b/topologie/views.py index dec681b6..35e62ce3 100644 --- a/topologie/views.py +++ b/topologie/views.py @@ -35,6 +35,8 @@ coté models et forms de topologie """ from __future__ import unicode_literals +import itertools + from django.shortcuts import render, redirect from django.contrib import messages from django.contrib.auth.decorators import login_required, permission_required @@ -48,6 +50,7 @@ from reversion.models import Version from topologie.models import Switch, Port, Room, Stack from topologie.forms import EditPortForm, NewSwitchForm, EditSwitchForm from topologie.forms import AddPortForm, EditRoomForm, StackForm +from topologie.forms import CreatePortsForm from users.views import form from re2o.utils import SortTable from machines.forms import DomainForm, NewMachineForm, EditMachineForm, EditInterfaceForm, AddInterfaceForm @@ -409,6 +412,58 @@ def new_switch(request): 'i_mbf_param': i_mbf_param }, 'topologie/switch.html', request) +@login_required +@permission_required('infra') +def create_ports(request, switch_id): + """ Création d'une liste de ports pour un switch.""" + try: + switch = Switch.objects.get(pk=switch_id) + except Switch.DoesNotExist: + messages.error(request, u"Switch inexistant") + return redirect("/topologie/") + + ports = switch.ports.order_by('port') + s_begin = s_end = 0 + if len(ports) > 0: + s_begin = ports[0].port + s_end = ports[len(ports)-1].port + + port_form = CreatePortsForm( + request.POST or None, + initial={'begin':s_begin,'end':s_end} + ) + if port_form.is_valid(): + begin = port_form.cleaned_data['begin'] + end = port_form.cleaned_data['end'] + b = [] + e = [] + if end < begin: + messages.error(request, "Port de fin inférieur au port de début !") + return redirect("/topologie/") + if end - begin > switch.number: + messages.error(request, "Ce switch ne peut avoir autant de ports.") + return redirect("/topologie/") + + if begin < s_begin: + b = range(begin, s_begin) + if end > s_end: + e = range(s_end+1, end+1) + for i in itertools.chain(b,e): + p = Port() + p.switch = switch + p.port = i + try: + with transaction.atomic(), reversion.create_revision(): + p.save() + reversion.set_user(request.user) + reversion.set_comment("Création") + messages.success(request, "Création du port %d" % i) + except IntegrityError: + messages.error(request, "Création d'un port existant.") + return redirect("/topologie/") + + return form({'topoform': port_form,}, 'topologie/switch.html', request) + @login_required @permission_required('infra')