8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-27 07:02:26 +00:00

Topologie: ajout et modif

This commit is contained in:
Dalahro 2016-07-06 23:29:31 +02:00
parent b23e1eeb65
commit 917e983f16
8 changed files with 67 additions and 9 deletions

View file

@ -1,4 +1,4 @@
from .models import Port
from .models import Port, Switch
from django.forms import ModelForm, Form
class PortForm(ModelForm):
@ -9,3 +9,17 @@ class PortForm(ModelForm):
class EditPortForm(ModelForm):
class Meta(PortForm.Meta):
fields = ['room', 'machine_interface', 'related', 'details']
class AddPortForm(ModelForm):
class Meta(PortForm.Meta):
fields = ['port', 'room', 'machine_interface', 'related', 'details']
class SwitchForm(ModelForm):
class Meta:
model = Switch
fields = '__all__'
class EditSwitchForm(ModelForm):
class Meta(SwitchForm.Meta):
fields = ['building', 'number', 'details']

View file

@ -26,7 +26,7 @@ class Switch(models.Model):
return str(self.building) + str(self.number)
class Port(models.Model):
switch = models.ForeignKey(Switch, related_name="ports")
switch = models.ForeignKey('Switch', related_name="ports")
port = models.IntegerField()
room = models.ForeignKey('Room', on_delete=models.PROTECT, blank=True, null=True)
machine_interface = models.OneToOneField('machines.Interface', on_delete=models.PROTECT, blank=True, null=True)

View file

@ -1,4 +1,3 @@
<h2>Switch {% if port_list.0 %}{{ port_list.0.switch }}{% endif %}</h2>
<table class="table table-striped">
<thead>
<tr>

View file

@ -12,7 +12,7 @@
<td>{{switch.building}}</td>
<td>{{switch.number}}</td>
<td>{{switch.details}}</td>
<td><a class="btn btn-primary btn-sm" role="button" href="{% url 'topologie:index-port' switch.pk %}"><i class="glyphicon glyphicon-list-alt"></i> Editer</a></td>
<td><a class="btn btn-primary btn-sm" role="button" href="{% url 'topologie:index-port' switch.pk %}"><i class="glyphicon glyphicon-cog"></i> Configurer</a></td>
</tr>
{% endfor %}
</table>

View file

@ -4,6 +4,9 @@
{% block title %}Ports du switch{% endblock %}
{% block content %}
<h2>Switch {{ nom_switch }}</h2>
<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>
{% include "topologie/aff_port.html" with port_list=port_list %}
<br />
<br />

View file

@ -2,4 +2,5 @@
{% block sidebar %}
<p><a href="{% url "topologie:index" %}">Liste des switchs</a></p>
<p><a href="{% url "topologie:new-switch" %}">Ajouter un switch</a></p>
{% endblock %}

View file

@ -4,7 +4,10 @@ from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^new_switch/$', views.new_switch, name='new-switch'),
url(r'^switch/(?P<switch_id>[0-9]+)$', views.index_port, name='index-port'),
url(r'^edit_port/(?P<port_id>[0-9]+)$', views.edit_port, name='edit-port'),
url(r'^new_port/(?P<switch_id>[0-9]+)$', views.new_port, name='new-port'),
url(r'^edit_switch/(?P<switch_id>[0-9]+)$', views.edit_switch, name='edit-switch'),
]

View file

@ -1,9 +1,9 @@
from django.shortcuts import render, redirect
from django.contrib import messages
from django.db import IntegrityError
from topologie.models import Switch, Port
from topologie.forms import EditPortForm
from topologie.forms import EditPortForm, EditSwitchForm, AddPortForm
from users.views import form
@ -18,7 +18,25 @@ def index_port(request, switch_id):
messages.error(request, u"Switch inexistant")
return redirect("/topologie/")
port_list = Port.objects.filter(switch = switch).order_by('port')
return render(request, 'topologie/index_p.html', {'port_list':port_list})
return render(request, 'topologie/index_p.html', {'port_list':port_list, 'id_switch':switch_id, 'nom_switch':switch})
def new_port(request, switch_id):
try:
switch = Switch.objects.get(pk=switch_id)
except Switch.DoesNotExist:
messages.error(request, u"Switch inexistant")
return redirect("/topologie/")
port = AddPortForm(request.POST or None)
if port.is_valid():
port = port.save(commit=False)
port.switch = switch
try:
port.save()
messages.success(request, "Port ajouté")
except IntegrityError:
pass
return redirect("/topologie/switch/" + switch_id)
return form({'topoform':port}, 'topologie/port.html', request)
def edit_port(request, port_id):
try:
@ -30,6 +48,26 @@ def edit_port(request, port_id):
if port.is_valid():
port.save()
messages.success(request, "Le port a bien été modifié")
return redirect("/topologie")
return redirect("/topologie/")
return form({'topoform':port}, 'topologie/port.html', request)
def new_switch(request):
switch = EditSwitchForm(request.POST or None)
if switch.is_valid():
switch.save()
messages.success(request, "Le switch a été créé")
return redirect("/topologie/")
return form({'topoform':switch}, 'topologie/port.html', request)
def edit_switch(request, switch_id):
try:
switch = Switch.objects.get(pk=switch_id)
except Switch.DoesNotExist:
messages.error(request, u"Switch inexistant")
return redirect("/topologie/")
switch = EditSwitchForm(request.POST or None, instance=switch)
if switch.is_valid():
switch.save()
messages.success(request, "Le switch a bien été modifié")
return redirect("/topologie/")
return form({'topoform':switch}, 'topologie/port.html', request)