diff --git a/re2o/templatetags/acl.py b/re2o/templatetags/acl.py index d749b299..0f67169f 100644 --- a/re2o/templatetags/acl.py +++ b/re2o/templatetags/acl.py @@ -127,6 +127,8 @@ MODEL_NAME = { 'ConstructorSwitch' : topologie.models.ConstructorSwitch, 'Port' : topologie.models.Port, 'Room' : topologie.models.Room, + 'Building' : topologie.models.Building, + 'SwitchBay' : topologie.models.SwitchBay, # users 'User' : users.models.User, 'Adherent' : users.models.Adherent, diff --git a/re2o/views.py b/re2o/views.py index 991b8702..bd9b18cb 100644 --- a/re2o/views.py +++ b/re2o/views.py @@ -85,6 +85,8 @@ HISTORY_BIND = { 'modelswitch' : topologie.models.ModelSwitch, 'constructorswitch' : topologie.models.ConstructorSwitch, 'accesspoint' : topologie.models.AccessPoint, + 'switchbay' : topologie.models.SwitchBay, + 'building' : topologie.models.Building, }, 'machines' : { 'machine' : machines.models.Machine, diff --git a/topologie/admin.py b/topologie/admin.py index 6c64aec7..62ffd6c4 100644 --- a/topologie/admin.py +++ b/topologie/admin.py @@ -36,7 +36,9 @@ from .models import ( Stack, ModelSwitch, ConstructorSwitch, - AccessPoint + AccessPoint, + SwitchBay, + Building ) @@ -75,6 +77,16 @@ class ConstructorSwitchAdmin(VersionAdmin): pass +class SwitchBayAdmin(VersionAdmin): + """Administration d'une baie de brassage""" + pass + + +class BuildingAdmin(VersionAdmin): + """Administration d'un batiment""" + pass + + admin.site.register(Port, PortAdmin) admin.site.register(AccessPoint, AccessPointAdmin) admin.site.register(Room, RoomAdmin) @@ -82,3 +94,5 @@ admin.site.register(Switch, SwitchAdmin) admin.site.register(Stack, StackAdmin) admin.site.register(ModelSwitch, ModelSwitchAdmin) admin.site.register(ConstructorSwitch, ConstructorSwitchAdmin) +admin.site.register(Building, BuildingAdmin) +admin.site.register(SwitchBay, SwitchBayAdmin) diff --git a/topologie/forms.py b/topologie/forms.py index b8c3d8d1..6e45a833 100644 --- a/topologie/forms.py +++ b/topologie/forms.py @@ -48,7 +48,9 @@ from .models import ( Stack, ModelSwitch, ConstructorSwitch, - AccessPoint + AccessPoint, + SwitchBay, + Building, ) from re2o.mixins import FormRevMixin @@ -186,3 +188,25 @@ class EditConstructorSwitchForm(FormRevMixin, ModelForm): def __init__(self, *args, **kwargs): prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(EditConstructorSwitchForm, self).__init__(*args, prefix=prefix, **kwargs) + + +class EditSwitchBayForm(FormRevMixin, ModelForm): + """Permet d'éditer une baie de brassage""" + class Meta: + model = SwitchBay + fields = '__all__' + + def __init__(self, *args, **kwargs): + prefix = kwargs.pop('prefix', self.Meta.model.__name__) + super(EditSwitchBayForm, self).__init__(*args, prefix=prefix, **kwargs) + + +class EditBuildingForm(FormRevMixin, ModelForm): + """Permet d'éditer le batiment""" + class Meta: + model = Building + fields = '__all__' + + def __init__(self, *args, **kwargs): + prefix = kwargs.pop('prefix', self.Meta.model.__name__) + super(EditBuildingForm, self).__init__(*args, prefix=prefix, **kwargs) diff --git a/topologie/migrations/0056_building_switchbay.py b/topologie/migrations/0056_building_switchbay.py new file mode 100644 index 00000000..3f705263 --- /dev/null +++ b/topologie/migrations/0056_building_switchbay.py @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2018-04-07 17:41 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion +import re2o.mixins + + +class Migration(migrations.Migration): + + dependencies = [ + ('topologie', '0055_auto_20180329_0431'), + ] + + operations = [ + migrations.CreateModel( + name='Building', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=255)), + ], + options={ + 'permissions': (('view_building', 'Peut voir un objet batiment'),), + }, + bases=(re2o.mixins.AclMixin, re2o.mixins.RevMixin, models.Model), + ), + migrations.CreateModel( + name='SwitchBay', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=255)), + ('info', models.CharField(blank=True, help_text='Informations particulières', max_length=255, null=True)), + ('building', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='topologie.Building')), + ('members', models.ManyToManyField(blank=True, related_name='bay_switches', to='topologie.Switch')), + ], + options={ + 'permissions': (('view_switchbay', 'Peut voir un objet baie de brassage'),), + }, + bases=(re2o.mixins.AclMixin, re2o.mixins.RevMixin, models.Model), + ), + ] diff --git a/topologie/models.py b/topologie/models.py index abbdfa7a..5fd5056a 100644 --- a/topologie/models.py +++ b/topologie/models.py @@ -186,8 +186,11 @@ class Switch(AclMixin, Machine): except IntegrityError: ValidationError("Création d'un port existant.") + def main_interface(self): + return self.interface_set.first() + def __str__(self): - return str(self.interface_set.first()) + return str(self.main_interface()) class ModelSwitch(AclMixin, RevMixin, models.Model): @@ -222,6 +225,49 @@ class ConstructorSwitch(AclMixin, RevMixin, models.Model): return self.name +class SwitchBay(AclMixin, RevMixin, models.Model): + """Une baie de brassage""" + PRETTY_NAME = "Baie de brassage" + name = models.CharField(max_length=255) + building = models.ForeignKey( + 'Building', + on_delete=models.PROTECT + ) + members = models.ManyToManyField( + blank=True, + to='Switch', + related_name='bay_switches' + ) + info = models.CharField( + max_length=255, + blank=True, + null=True, + help_text="Informations particulières" + ) + + class Meta: + permissions = ( + ("view_switchbay", "Peut voir un objet baie de brassage"), + ) + + def __str__(self): + return self.name + + +class Building(AclMixin, RevMixin, models.Model): + """Un batiment""" + PRETTY_NAME = "Batiment" + name = models.CharField(max_length=255) + + class Meta: + permissions = ( + ("view_building", "Peut voir un objet batiment"), + ) + + def __str__(self): + return self.name + + class Port(AclMixin, RevMixin, models.Model): """ Definition d'un port. Relié à un switch(foreign_key), un port peut etre relié de manière exclusive à : diff --git a/topologie/templates/topologie/aff_building.html b/topologie/templates/topologie/aff_building.html new file mode 100644 index 00000000..878c85e8 --- /dev/null +++ b/topologie/templates/topologie/aff_building.html @@ -0,0 +1,62 @@ +{% comment %} +Re2o est un logiciel d'administration développé initiallement au rezometz. Il +se veut agnostique au réseau considéré, de manière à être installable en +quelques clics. + +Copyright © 2017 Gabriel Détraz +Copyright © 2017 Goulven Kermarec +Copyright © 2017 Augustin Lemesle + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +{% endcomment %} + +{% load acl %} + +{% if building_list.paginator %} +{% include "pagination.html" with list=building_list %} +{% endif %} + + + + + + + + + {% for building in building_list %} + + + + + {% endfor %} +
{% include "buttons/sort.html" with prefix='building' col='name' text='Bâtiment' %}
{{building.name}} + + + + {% can_edit building %} + + + + {% acl_end %} + {% can_delete building %} + + + + {% acl_end %} +
+ +{% if building_list.paginator %} +{% include "pagination.html" with list=building_list %} +{% endif %} diff --git a/topologie/templates/topologie/aff_switch_bay.html b/topologie/templates/topologie/aff_switch_bay.html new file mode 100644 index 00000000..6f8ab848 --- /dev/null +++ b/topologie/templates/topologie/aff_switch_bay.html @@ -0,0 +1,68 @@ +{% comment %} +Re2o est un logiciel d'administration développé initiallement au rezometz. Il +se veut agnostique au réseau considéré, de manière à être installable en +quelques clics. + +Copyright © 2017 Gabriel Détraz +Copyright © 2017 Goulven Kermarec +Copyright © 2017 Augustin Lemesle + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +{% endcomment %} + +{% load acl %} + +{% if switch_bay_list.paginator %} +{% include "pagination.html" with list=switch_bay_list %} +{% endif %} + + + + + + + + + + + + {% for switch_bay in switch_bay_list %} + + + + + + + + {% endfor %} +
{% include "buttons/sort.html" with prefix='switch-bay' col='name' text='Baie' %}BâtimentInfo particulièresSwitchs du batiment
{{switch_bay.name}}{{switch_bay.building}}{{switch_bay.info}}{{switch_bay.members}} + + + + {% can_edit switch_bay %} + + + + {% acl_end %} + {% can_delete switch_bay %} + + + + {% acl_end %} +
+ +{% if switch_bay_list.paginator %} +{% include "pagination.html" with list=switch_bay_list %} +{% endif %} diff --git a/topologie/templates/topologie/index_model_switch.html b/topologie/templates/topologie/index_model_switch.html index e375dd36..ea346f56 100644 --- a/topologie/templates/topologie/index_model_switch.html +++ b/topologie/templates/topologie/index_model_switch.html @@ -41,6 +41,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
{% acl_end %} {% include "topologie/aff_constructor_switch.html" with constructor_switch_list=constructor_switch_list %} +

Baie de brassage

+{% can_create SwitchBay %} + Ajouter une baie de brassage +
+{% acl_end %} +{% include "topologie/aff_switch_bay.html" with switch_bay_list=switch_bay_list %}


diff --git a/topologie/urls.py b/topologie/urls.py index d4b31717..c7243200 100644 --- a/topologie/urls.py +++ b/topologie/urls.py @@ -99,4 +99,14 @@ urlpatterns = [ url(r'^del_constructor_switch/(?P[0-9]+)$', views.del_constructor_switch, name='del-constructor-switch'), + url(r'^new_switch_bay/$', + views.new_switch_bay, + name='new-switch-bay' + ), + url(r'^edit_switch_bay/(?P[0-9]+)$', + views.edit_switch_bay, + name='edit-switch-bay'), + url(r'^del_switch_bay/(?P[0-9]+)$', + views.del_switch_bay, + name='del-switch-bay'), ] diff --git a/topologie/views.py b/topologie/views.py index 0d73d3ea..57ff3cb6 100644 --- a/topologie/views.py +++ b/topologie/views.py @@ -51,7 +51,9 @@ from topologie.models import ( Stack, ModelSwitch, ConstructorSwitch, - AccessPoint + AccessPoint, + SwitchBay, + Building ) from topologie.forms import EditPortForm, NewSwitchForm, EditSwitchForm from topologie.forms import ( @@ -62,7 +64,9 @@ from topologie.forms import ( EditConstructorSwitchForm, CreatePortsForm, AddAccessPointForm, - EditAccessPointForm + EditAccessPointForm, + EditSwitchBayForm, + EditBuildingForm ) from users.views import form from re2o.utils import re2o_paginator, SortTable @@ -200,6 +204,7 @@ def index_model_switch(request): """ Affichage de l'ensemble des modèles de switches""" model_switch_list = ModelSwitch.objects.select_related('constructor') constructor_switch_list = ConstructorSwitch.objects + switch_bay_list = SwitchBay.objects.select_related('building') model_switch_list = SortTable.sort( model_switch_list, request.GET.get('col'), @@ -215,6 +220,7 @@ def index_model_switch(request): return render(request, 'topologie/index_model_switch.html', { 'model_switch_list': model_switch_list, 'constructor_switch_list': constructor_switch_list, + 'switch_bay_list': switch_bay_list, }) @@ -635,6 +641,49 @@ def del_model_switch(request, model_switch, modelswitchid): }, 'topologie/delete.html', request) +@login_required +@can_create(SwitchBay) +def new_switch_bay(request): + """Nouvelle baie de switch""" + switch_bay = EditSwitchBayForm(request.POST or None) + if switch_bay.is_valid(): + switch_bay.save() + messages.success(request, "La baie a été créé") + return redirect(reverse('topologie:index-model-switch')) + return form({'topoform': switch_bay, 'action_name' : 'Ajouter'}, 'topologie/topo.html', request) + + +@login_required +@can_edit(SwitchBay) +def edit_switch_bay(request, switch_bay, switchbayid): + """ Edition d'une baie de switch""" + switch_bay = EditSwitchBayForm(request.POST or None, instance=switch_bay) + if switch_bay.is_valid(): + if switch_bay.changed_data: + switch_bay.save() + messages.success(request, "Le switch a bien été modifié") + return redirect(reverse('topologie:index-model-switch')) + return form({'topoform': switch_bay, 'action_name' : 'Editer'}, 'topologie/topo.html', request) + + +@login_required +@can_delete(SwitchBay) +def del_switch_bay(request, switch_bay, switchbayid): + """ Suppression d'une baie de switch""" + if request.method == "POST": + try: + switch_bay.delete() + messages.success(request, "La baie a été détruite") + except ProtectedError: + messages.error(request, "La baie %s est affecté à un autre objet,\ + impossible de la supprimer (switch ou user)" % switch_bay) + return redirect(reverse('topologie:index-model-switch')) + return form({ + 'objet': switch_bay, + 'objet_name': 'Baie de switch' + }, 'topologie/delete.html', request) + + @login_required @can_create(ConstructorSwitch) def new_constructor_switch(request):