Commit 917e983f authored by Dalahro's avatar Dalahro

Topologie: ajout et modif

parent b23e1eeb
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']
......@@ -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)
......
<h2>Switch {% if port_list.0 %}{{ port_list.0.switch }}{% endif %}</h2>
<table class="table table-striped">
<thead>
<tr>
......
......@@ -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>
......@@ -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 />
......
......@@ -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 %}
......@@ -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'),
]
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)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment