8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-22 03:13:12 +00:00

Ajoute la possibilité de créer une série de ports pour un switch.

This commit is contained in:
LEVY-FALK Hugo 2017-10-26 10:41:48 +02:00 committed by root
parent 473ae3afea
commit 7bc0f2b703
5 changed files with 68 additions and 2 deletions

View file

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

View file

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

View file

@ -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 %}
</td>
</tr>

View file

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