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

Merge branch 'klafyvel' into 'master'

Fix #21

Closes #21

See merge request rezo/re2o!26
This commit is contained in:
Gabriel Detraz 2017-10-26 21:45:42 +02:00
commit 94a4267d2e
5 changed files with 72 additions and 5 deletions

View file

@ -33,6 +33,7 @@ NewSwitchForm)
from __future__ import unicode_literals from __future__ import unicode_literals
from machines.models import Interface from machines.models import Interface
from django import forms
from django.forms import ModelForm from django.forms import ModelForm
from .models import Port, Switch, Room, Stack, ModelSwitch, ConstructorSwitch from .models import Port, Switch, Room, Stack, ModelSwitch, ConstructorSwitch
@ -139,6 +140,12 @@ class EditRoomForm(ModelForm):
super(EditRoomForm, self).__init__(*args, prefix=prefix, **kwargs) 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): class EditModelSwitchForm(ModelForm):
"""Permet d'éediter un modèle de switch : nom et constructeur""" """Permet d'éediter un modèle de switch : nom et constructeur"""
class Meta: class Meta:

View file

@ -59,6 +59,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
{% if is_infra %} {% if is_infra %}
{% include 'buttons/edit.html' with href='topologie:edit-switch' id=switch.pk %} {% 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/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 %} {% endif %}
</td> </td>
</tr> </tr>

View file

@ -32,6 +32,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
{% if is_infra %} {% if is_infra %}
<a class="btn btn-primary btn-sm" role="button" href="{% url 'topologie:edit-switch' id_switch %}"><i class="glyphicon glyphicon-edit"></i> Editer</a> <a class="btn btn-primary btn-sm" role="button" href="{% url 'topologie:edit-switch' id_switch %}"><i class="glyphicon glyphicon-edit"></i> Editer</a>
<a class="btn btn-primary btn-sm" role="button" href="{% url 'topologie:new-port' id_switch %}"><i class="glyphicon glyphicon-plus"></i> Ajouter un port</a> <a class="btn btn-primary btn-sm" role="button" href="{% url 'topologie:new-port' id_switch %}"><i class="glyphicon glyphicon-plus"></i> Ajouter un port</a>
<a class="btn btn-primary btn-sm" role="button" href="{% url 'topologie:create-ports' id_switch %}"><i class="glyphicon glyphicon-plus"></i> Ajouter des ports</a>
{% endif %} {% endif %}
{% include "topologie/aff_port.html" with port_list=port_list %} {% include "topologie/aff_port.html" with port_list=port_list %}
<br /> <br />

View file

@ -35,6 +35,9 @@ from . import views
urlpatterns = [ urlpatterns = [
url(r'^$', views.index, name='index'), url(r'^$', views.index, name='index'),
url(r'^new_switch/$', views.new_switch, name='new-switch'), url(r'^new_switch/$', views.new_switch, name='new-switch'),
url(r'^create_ports/(?P<switch_id>[0-9]+)$',
views.create_ports,
name='create-ports'),
url(r'^index_room/$', views.index_room, name='index-room'), url(r'^index_room/$', views.index_room, name='index-room'),
url(r'^new_room/$', views.new_room, name='new-room'), url(r'^new_room/$', views.new_room, name='new-room'),
url(r'^edit_room/(?P<room_id>[0-9]+)$', views.edit_room, name='edit-room'), url(r'^edit_room/(?P<room_id>[0-9]+)$', views.edit_room, name='edit-room'),

View file

@ -35,6 +35,8 @@ coté models et forms de topologie
""" """
from __future__ import unicode_literals from __future__ import unicode_literals
import itertools
from django.shortcuts import render, redirect from django.shortcuts import render, redirect
from django.contrib import messages from django.contrib import messages
from django.contrib.auth.decorators import login_required, permission_required from django.contrib.auth.decorators import login_required, permission_required
@ -59,7 +61,8 @@ from topologie.forms import (
EditRoomForm, EditRoomForm,
StackForm, StackForm,
EditModelSwitchForm, EditModelSwitchForm,
EditConstructorSwitchForm EditConstructorSwitchForm,
CreatePortsForm
) )
from users.views import form from users.views import form
from re2o.utils import SortTable from re2o.utils import SortTable
@ -181,7 +184,9 @@ def index_port(request, switch_id):
.select_related('room')\ .select_related('room')\
.select_related('machine_interface__domain__extension')\ .select_related('machine_interface__domain__extension')\
.select_related('machine_interface__machine__user')\ .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') .select_related('switch')
port_list = SortTable.sort( port_list = SortTable.sort(
port_list, port_list,
@ -334,7 +339,7 @@ def del_port(request, port_id):
port.delete() port.delete()
reversion.set_user(request.user) reversion.set_user(request.user)
reversion.set_comment("Destruction") reversion.set_comment("Destruction")
messages.success(request, "Le port a eté détruit") messages.success(request, "Le port a été détruit")
except ProtectedError: except ProtectedError:
messages.error(request, "Le port %s est affecté à un autre objet,\ messages.error(request, "Le port %s est affecté à un autre objet,\
impossible de le supprimer" % port) impossible de le supprimer" % port)
@ -467,7 +472,7 @@ def new_switch(request):
reversion.set_comment("Création") reversion.set_comment("Création")
messages.success(request, "Le switch a été créé") messages.success(request, "Le switch a été créé")
return redirect("/topologie/") return redirect("/topologie/")
i_mbf_param = generate_ipv4_mbf_param( interface, False ) i_mbf_param = generate_ipv4_mbf_param(interface, False)
return form({ return form({
'topoform': switch, 'topoform': switch,
'machineform': machine, 'machineform': machine,
@ -477,6 +482,56 @@ def new_switch(request):
}, 'topologie/switch.html', 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 @login_required
@permission_required('infra') @permission_required('infra')
def edit_switch(request, switch_id): 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é") messages.success(request, "Le switch a bien été modifié")
return redirect("/topologie/") 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({ return form({
'topoform': switch_form, 'topoform': switch_form,
'machineform': machine_form, 'machineform': machine_form,