diff --git a/topologie/forms.py b/topologie/forms.py
index a325c0f9..0119435d 100644
--- a/topologie/forms.py
+++ b/topologie/forms.py
@@ -33,6 +33,7 @@ NewSwitchForm)
from __future__ import unicode_literals
from machines.models import Interface
+from django import forms
from django.forms import ModelForm
from .models import Port, Switch, Room, Stack, ModelSwitch, ConstructorSwitch
@@ -139,6 +140,12 @@ class EditRoomForm(ModelForm):
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)
+
+
class EditModelSwitchForm(ModelForm):
"""Permet d'éediter un modèle de switch : nom et constructeur"""
class Meta:
diff --git a/topologie/templates/topologie/aff_switch.html b/topologie/templates/topologie/aff_switch.html
index 6542ce13..25f466e8 100644
--- a/topologie/templates/topologie/aff_switch.html
+++ b/topologie/templates/topologie/aff_switch.html
@@ -59,6 +59,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/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/urls.py b/topologie/urls.py
index 4175c68c..752ebcc5 100644
--- a/topologie/urls.py
+++ b/topologie/urls.py
@@ -35,6 +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'^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 d72be310..7bed18aa 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
@@ -59,7 +61,8 @@ from topologie.forms import (
EditRoomForm,
StackForm,
EditModelSwitchForm,
- EditConstructorSwitchForm
+ EditConstructorSwitchForm,
+ CreatePortsForm
)
from users.views import form
from re2o.utils import SortTable
@@ -181,7 +184,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,
@@ -334,7 +339,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)
@@ -467,7 +472,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,
@@ -477,6 +482,56 @@ def new_switch(request):
}, '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/")
+
+ s_begin = s_end = 0
+ nb_ports = switch.ports.count()
+ if nb_ports > 0:
+ 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,
+ initial={'begin': s_begin, 'end': s_end}
+ )
+ if port_form.is_valid():
+ begin = port_form.cleaned_data['begin']
+ end = port_form.cleaned_data['end']
+ if end < begin:
+ messages.error(request, "Port de fin inférieur au port de début !")
+ 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/switch/" + str(switch.id))
+
+ 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():
+ port.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/switch/" + str(switch.id))
+
+ return form({'topoform': port_form}, 'topologie/switch.html', request)
+
+
@login_required
@permission_required('infra')
def edit_switch(request, switch_id):
@@ -534,7 +589,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,