From ba61a74cace3d1b344e4d3ef4c836ad930acafea Mon Sep 17 00:00:00 2001 From: Hugo LEVY-FALK Date: Thu, 26 Oct 2017 10:41:48 +0200 Subject: [PATCH 1/7] =?UTF-8?q?Ajoute=20la=20possibilit=C3=A9=20de=20cr?= =?UTF-8?q?=C3=A9er=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') From 6be359350b7858c0d5c06cbd6905a8f3e59ba4a1 Mon Sep 17 00:00:00 2001 From: Hugo LEVY-FALK Date: Thu, 26 Oct 2017 11:05:27 +0200 Subject: [PATCH 2/7] =?UTF-8?q?Optimisation=20des=20requ=C3=AAtes.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- topologie/views.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/topologie/views.py b/topologie/views.py index 35e62ce3..023ed254 100644 --- a/topologie/views.py +++ b/topologie/views.py @@ -422,11 +422,12 @@ def create_ports(request, switch_id): messages.error(request, u"Switch inexistant") return redirect("/topologie/") - ports = switch.ports.order_by('port') s_begin = s_end = 0 - if len(ports) > 0: + nb_ports = switch.ports.count() + if nb_ports > 0: + ports = switch.ports.order_by('port') s_begin = ports[0].port - s_end = ports[len(ports)-1].port + s_end = ports.last().port port_form = CreatePortsForm( request.POST or None, From 65125a020dd659faefc8d52688291cdce411be2d Mon Sep 17 00:00:00 2001 From: Hugo LEVY-FALK Date: Thu, 26 Oct 2017 11:08:54 +0200 Subject: [PATCH 3/7] typo --- topologie/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/topologie/views.py b/topologie/views.py index 023ed254..11b889fa 100644 --- a/topologie/views.py +++ b/topologie/views.py @@ -270,7 +270,7 @@ def del_port(request, port_id): port.delete() reversion.set_user(request.user) reversion.set_comment("Destruction") - messages.success(request, "Le port a eté détruit") + messages.success(request, "Le port a été détruit") except ProtectedError: messages.error(request, "Le port %s est affecté à un autre objet,\ impossible de le supprimer" % port) From 3a0fcefae1a62e1b18e103413066ae5840b0327b Mon Sep 17 00:00:00 2001 From: Hugo LEVY-FALK Date: Thu, 26 Oct 2017 11:14:16 +0200 Subject: [PATCH 4/7] =?UTF-8?q?Int=C3=A9gration=20=C3=A0=20la=20page=20/to?= =?UTF-8?q?pologie/switch/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- topologie/templates/topologie/index_p.html | 1 + topologie/views.py | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/topologie/templates/topologie/index_p.html b/topologie/templates/topologie/index_p.html index 84159659..3f9356f2 100644 --- a/topologie/templates/topologie/index_p.html +++ b/topologie/templates/topologie/index_p.html @@ -32,6 +32,7 @@ with this program; if not, write to the Free Software Foundation, Inc., {% if is_infra %} Editer Ajouter un port + Ajouter des ports {% endif %} {% include "topologie/aff_port.html" with port_list=port_list %}
diff --git a/topologie/views.py b/topologie/views.py index 11b889fa..4561506a 100644 --- a/topologie/views.py +++ b/topologie/views.py @@ -440,10 +440,10 @@ def create_ports(request, switch_id): e = [] if end < begin: messages.error(request, "Port de fin inférieur au port de début !") - return redirect("/topologie/") + return redirect("/topologie/switch/" + str(switch.id)) if end - begin > switch.number: messages.error(request, "Ce switch ne peut avoir autant de ports.") - return redirect("/topologie/") + return redirect("/topologie/switch/" + str(switch.id)) if begin < s_begin: b = range(begin, s_begin) @@ -461,7 +461,7 @@ def create_ports(request, switch_id): messages.success(request, "Création du port %d" % i) except IntegrityError: messages.error(request, "Création d'un port existant.") - return redirect("/topologie/") + return redirect("/topologie/switch/" + str(switch.id)) return form({'topoform': port_form,}, 'topologie/switch.html', request) From af9e7a86509dee829a323760a92d6e9ee97e274d Mon Sep 17 00:00:00 2001 From: Hugo LEVY-FALK Date: Thu, 26 Oct 2017 11:29:44 +0200 Subject: [PATCH 5/7] =?UTF-8?q?Suppression=20de=20v=C3=A9rifications=20inu?= =?UTF-8?q?tiles.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- topologie/views.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/topologie/views.py b/topologie/views.py index 4561506a..dcfce4d9 100644 --- a/topologie/views.py +++ b/topologie/views.py @@ -445,10 +445,8 @@ def create_ports(request, switch_id): messages.error(request, "Ce switch ne peut avoir autant de ports.") return redirect("/topologie/switch/" + str(switch.id)) - if begin < s_begin: - b = range(begin, s_begin) - if end > s_end: - e = range(s_end+1, end+1) + b = range(begin, s_begin) + e = range(s_end+1, end+1) for i in itertools.chain(b,e): p = Port() p.switch = switch From 457a098406c45cc7e1d6c86ea93fc50f4052c37b Mon Sep 17 00:00:00 2001 From: Hugo LEVY-FALK Date: Thu, 26 Oct 2017 11:51:41 +0200 Subject: [PATCH 6/7] =?UTF-8?q?Moins=20de=20requ=C3=AAtes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- topologie/views.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/topologie/views.py b/topologie/views.py index dcfce4d9..bad4d246 100644 --- a/topologie/views.py +++ b/topologie/views.py @@ -425,9 +425,9 @@ def create_ports(request, switch_id): s_begin = s_end = 0 nb_ports = switch.ports.count() if nb_ports > 0: - ports = switch.ports.order_by('port') - s_begin = ports[0].port - s_end = ports.last().port + ports = switch.ports.order_by('port').values('port') + s_begin = ports.first().get('port') + s_end = ports.last().get('port') port_form = CreatePortsForm( request.POST or None, From 3965489e95488b9f969405a9da27c52989f4ef32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Kervella?= Date: Thu, 26 Oct 2017 19:29:34 +0000 Subject: [PATCH 7/7] Pep8 et pylint compliance --- topologie/forms.py | 3 --- topologie/models.py | 1 + topologie/urls.py | 4 +++- topologie/views.py | 39 +++++++++++++++++++++++---------------- 4 files changed, 27 insertions(+), 20 deletions(-) diff --git a/topologie/forms.py b/topologie/forms.py index 958c6008..a25419af 100644 --- a/topologie/forms.py +++ b/topologie/forms.py @@ -143,6 +143,3 @@ 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 924002b3..adcc7a57 100644 --- a/topologie/models.py +++ b/topologie/models.py @@ -120,6 +120,7 @@ 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/urls.py b/topologie/urls.py index 4b39d5c5..de7186a8 100644 --- a/topologie/urls.py +++ b/topologie/urls.py @@ -35,7 +35,9 @@ 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'^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 bad4d246..80b831f7 100644 --- a/topologie/views.py +++ b/topologie/views.py @@ -53,7 +53,13 @@ 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 +from machines.forms import ( + DomainForm, + NewMachineForm, + EditMachineForm, + EditInterfaceForm, + AddInterfaceForm +) from machines.views import generate_ipv4_mbf_param from preferences.models import AssoOption, GeneralOption @@ -141,7 +147,9 @@ def index_port(request, switch_id): .select_related('room')\ .select_related('machine_interface__domain__extension')\ .select_related('machine_interface__machine__user')\ - .select_related('related__switch__switch_interface__domain__extension')\ + .select_related( + 'related__switch__switch_interface__domain__extension' + )\ .select_related('switch') port_list = SortTable.sort( port_list, @@ -403,7 +411,7 @@ def new_switch(request): reversion.set_comment("Création") messages.success(request, "Le switch a été créé") return redirect("/topologie/") - i_mbf_param = generate_ipv4_mbf_param( interface, False ) + i_mbf_param = generate_ipv4_mbf_param(interface, False) return form({ 'topoform': switch, 'machineform': machine, @@ -412,6 +420,7 @@ 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): @@ -431,13 +440,11 @@ def create_ports(request, switch_id): port_form = CreatePortsForm( request.POST or None, - initial={'begin':s_begin,'end':s_end} + 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/switch/" + str(switch.id)) @@ -445,15 +452,15 @@ def create_ports(request, switch_id): messages.error(request, "Ce switch ne peut avoir autant de ports.") return redirect("/topologie/switch/" + str(switch.id)) - b = range(begin, s_begin) - e = range(s_end+1, end+1) - for i in itertools.chain(b,e): - p = Port() - p.switch = switch - p.port = i + begin_range = range(begin, s_begin) + end_range = range(s_end+1, end+1) + for i in itertools.chain(begin_range, end_range): + port = Port() + port.switch = switch + port.port = i try: with transaction.atomic(), reversion.create_revision(): - p.save() + port.save() reversion.set_user(request.user) reversion.set_comment("Création") messages.success(request, "Création du port %d" % i) @@ -461,8 +468,8 @@ def create_ports(request, switch_id): messages.error(request, "Création d'un port existant.") return redirect("/topologie/switch/" + str(switch.id)) - return form({'topoform': port_form,}, 'topologie/switch.html', request) - + return form({'topoform': port_form}, 'topologie/switch.html', request) + @login_required @permission_required('infra') @@ -521,7 +528,7 @@ def edit_switch(request, switch_id): ) messages.success(request, "Le switch a bien été modifié") return redirect("/topologie/") - i_mbf_param = generate_ipv4_mbf_param( interface_form, False ) + i_mbf_param = generate_ipv4_mbf_param(interface_form, False) return form({ 'topoform': switch_form, 'machineform': machine_form,