diff --git a/topologie/forms.py b/topologie/forms.py index 8e7355ac..1f7ed7c9 100644 --- a/topologie/forms.py +++ b/topologie/forms.py @@ -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'] + diff --git a/topologie/models.py b/topologie/models.py index 80af6534..717d15ea 100644 --- a/topologie/models.py +++ b/topologie/models.py @@ -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) diff --git a/topologie/templates/topologie/aff_port.html b/topologie/templates/topologie/aff_port.html index d94b341f..dd15f1cc 100644 --- a/topologie/templates/topologie/aff_port.html +++ b/topologie/templates/topologie/aff_port.html @@ -1,4 +1,3 @@ -

Switch {% if port_list.0 %}{{ port_list.0.switch }}{% endif %}

diff --git a/topologie/templates/topologie/aff_switch.html b/topologie/templates/topologie/aff_switch.html index 880c264a..24d04761 100644 --- a/topologie/templates/topologie/aff_switch.html +++ b/topologie/templates/topologie/aff_switch.html @@ -12,7 +12,7 @@ - + {% endfor %}
{{switch.building}} {{switch.number}} {{switch.details}} Editer Configurer
diff --git a/topologie/templates/topologie/index_p.html b/topologie/templates/topologie/index_p.html index 5309bc46..28ee8b23 100644 --- a/topologie/templates/topologie/index_p.html +++ b/topologie/templates/topologie/index_p.html @@ -4,6 +4,9 @@ {% block title %}Ports du switch{% endblock %} {% block content %} +

Switch {{ nom_switch }}

+ Editer + Ajouter un port {% include "topologie/aff_port.html" with port_list=port_list %}

diff --git a/topologie/templates/topologie/sidebar.html b/topologie/templates/topologie/sidebar.html index b74e53d3..aaffefeb 100644 --- a/topologie/templates/topologie/sidebar.html +++ b/topologie/templates/topologie/sidebar.html @@ -2,4 +2,5 @@ {% block sidebar %}

Liste des switchs

+

Ajouter un switch

{% endblock %} diff --git a/topologie/urls.py b/topologie/urls.py index bc960eb6..67f4c241 100644 --- a/topologie/urls.py +++ b/topologie/urls.py @@ -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[0-9]+)$', views.index_port, name='index-port'), url(r'^edit_port/(?P[0-9]+)$', views.edit_port, name='edit-port'), + url(r'^new_port/(?P[0-9]+)$', views.new_port, name='new-port'), + url(r'^edit_switch/(?P[0-9]+)$', views.edit_switch, name='edit-switch'), ] diff --git a/topologie/views.py b/topologie/views.py index af3befe2..aff1fb62 100644 --- a/topologie/views.py +++ b/topologie/views.py @@ -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)