From 98a9dca664dbc7b9ad43821ace7ff3a604edf2cd Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Sun, 30 Dec 2018 18:20:06 +0100 Subject: [PATCH] Edition fine des modules pour chaque switchs avec le slot --- topologie/forms.py | 21 +++---- ...230_1556.py => 0067_auto_20181230_1819.py} | 7 ++- topologie/models.py | 16 ++--- .../templates/topologie/aff_modules.html | 20 ++++++- topologie/templates/topologie/topo.html | 2 +- topologie/urls.py | 9 ++- topologie/views.py | 59 ++++++++++++++++++- 7 files changed, 105 insertions(+), 29 deletions(-) rename topologie/migrations/{0067_auto_20181230_1556.py => 0067_auto_20181230_1819.py} (93%) diff --git a/topologie/forms.py b/topologie/forms.py index dc4a5e9b..ed6fa5b9 100644 --- a/topologie/forms.py +++ b/topologie/forms.py @@ -55,7 +55,8 @@ from .models import ( SwitchBay, Building, PortProfile, - ModuleSwitch + ModuleSwitch, + ModuleOnSwitch, ) @@ -279,14 +280,14 @@ class EditModuleForm(FormRevMixin, ModelForm): def __init__(self, *args, **kwargs): prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(EditModuleForm, self).__init__(*args, prefix=prefix, **kwargs) - self.fields['switchs'].queryset = (Switch.objects.filter(model__is_modular=True)) - def save(self, commit=True): - # TODO : None of the parents of ServiceForm use the commit - # parameter in .save() - instance = super(EditModuleForm, self).save(commit=False) - if commit: - instance.save() - instance.process_link(self.cleaned_data.get('switchs')) - return instance +class EditSwitchModuleForm(FormRevMixin, ModelForm): + """Add/edit a switch to a module""" + class Meta: + model = ModuleOnSwitch + fields = '__all__' + + def __init__(self, *args, **kwargs): + prefix = kwargs.pop('prefix', self.Meta.model.__name__) + super(EditSwitchModuleForm, self).__init__(*args, prefix=prefix, **kwargs) diff --git a/topologie/migrations/0067_auto_20181230_1556.py b/topologie/migrations/0067_auto_20181230_1819.py similarity index 93% rename from topologie/migrations/0067_auto_20181230_1556.py rename to topologie/migrations/0067_auto_20181230_1819.py index cd0d368c..57f268ea 100644 --- a/topologie/migrations/0067_auto_20181230_1556.py +++ b/topologie/migrations/0067_auto_20181230_1819.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Generated by Django 1.10.7 on 2018-12-30 14:56 +# Generated by Django 1.10.7 on 2018-12-30 17:19 from __future__ import unicode_literals from django.db import migrations, models @@ -32,7 +32,6 @@ class Migration(migrations.Migration): ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('reference', models.CharField(help_text='Reference of a module', max_length=255, verbose_name='Module reference')), ('comment', models.CharField(blank=True, help_text='Comment', max_length=255, null=True, verbose_name='Comment')), - ('switchs', models.ManyToManyField(through='topologie.ModuleOnSwitch', to='topologie.Switch')), ], options={ 'verbose_name': 'Module of a switch', @@ -60,4 +59,8 @@ class Migration(migrations.Migration): name='switch', field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='topologie.Switch'), ), + migrations.AlterUniqueTogether( + name='moduleonswitch', + unique_together=set([('slot', 'switch')]), + ), ] diff --git a/topologie/models.py b/topologie/models.py index aef4e5b3..b9b2c6f2 100644 --- a/topologie/models.py +++ b/topologie/models.py @@ -451,7 +451,6 @@ class ModuleSwitch(AclMixin, RevMixin, models.Model): help_text=_("Comment"), verbose_name=_("Comment") ) - switchs = models.ManyToManyField('Switch', through='ModuleOnSwitch') class Meta: permissions = ( @@ -459,17 +458,6 @@ class ModuleSwitch(AclMixin, RevMixin, models.Model): ) verbose_name = _("Module of a switch") - def process_link(self, switchs): - """Django can't create itself foreignkey with explicit through""" - ModuleOnSwitch.objects.bulk_create( - [ModuleOnSwitch( - module=self, switch=sw - ) for sw in switchs.exclude( - pk__in=Switch.objects.filter(moduleswitch=self) - )] - ) - ModuleOnSwitch.objects.filter(module=self).exclude(switch__in=switchs).delete() - return def __str__(self): return str(self.reference) @@ -490,6 +478,10 @@ class ModuleOnSwitch(AclMixin, RevMixin, models.Model): ("view_moduleonswitch", _("Can view a moduleonswitch object")), ) verbose_name = _("link between switchs and modules") + unique_together = ['slot', 'switch'] + + def __str__(self): + return 'On slot ' + str(self.slot) + ' of ' + str(self.switch) class ConstructorSwitch(AclMixin, RevMixin, models.Model): diff --git a/topologie/templates/topologie/aff_modules.html b/topologie/templates/topologie/aff_modules.html index 8233260c..d73fffeb 100644 --- a/topologie/templates/topologie/aff_modules.html +++ b/topologie/templates/topologie/aff_modules.html @@ -43,9 +43,27 @@ with this program; if not, write to the Free Software Foundation, Inc., {{ module.reference }} {{ module.comment }} - {{ module.switchs.all }} + + {% for module_switch in module.moduleonswitch_set.all %} + Slot {{ module_switch.slot }} of {{ module_switch.switch }} + {% can_edit module_switch %} + + + + {% acl_end %} + {% can_delete module_switch %} + + + + {% acl_end %} +
+ {% endfor %} + {% can_edit module %} + + + diff --git a/topologie/templates/topologie/topo.html b/topologie/templates/topologie/topo.html index d1ec9bb3..a7824020 100644 --- a/topologie/templates/topologie/topo.html +++ b/topologie/templates/topologie/topo.html @@ -37,7 +37,7 @@ with this program; if not, write to the Free Software Foundation, Inc., {% endif %}
{% csrf_token %} - {% massive_bootstrap_form topoform 'room,related,machine_interface,members,vlan_tagged,switchs' %} + {% massive_bootstrap_form topoform 'room,related,machine_interface,members,vlan_tagged,switch' %} {% bootstrap_button action_name icon='ok' button_class='btn-success' %}

diff --git a/topologie/urls.py b/topologie/urls.py index 7af7df9a..70eae8e4 100644 --- a/topologie/urls.py +++ b/topologie/urls.py @@ -124,9 +124,14 @@ urlpatterns = [ views.edit_vlanoptions, name='edit-vlanoptions'), url(r'^add_module/$', views.add_module, name='add-module'), - url(r'^edit_module/(?P[0-9]+)$', + url(r'^edit_module/(?P[0-9]+)$', views.edit_module, name='edit-module'), - url(r'^del_module/(?P[0-9]+)$', views.del_module, name='del-module'), + url(r'^del_module/(?P[0-9]+)$', views.del_module, name='del-module'), url(r'^index_module/$', views.index_module, name='index-module'), + url(r'^add_module_on/$', views.add_module_on, name='add-module-on'), + url(r'^edit_module_on/(?P[0-9]+)$', + views.edit_module_on, + name='edit-module-on'), + url(r'^del_module_on/(?P[0-9]+)$', views.del_module_on, name='del-module-on'), ] diff --git a/topologie/views.py b/topologie/views.py index 200f3874..6ee4de85 100644 --- a/topologie/views.py +++ b/topologie/views.py @@ -87,6 +87,7 @@ from .models import ( Server, PortProfile, ModuleSwitch, + ModuleOnSwitch, ) from .forms import ( EditPortForm, @@ -103,7 +104,8 @@ from .forms import ( EditSwitchBayForm, EditBuildingForm, EditPortProfileForm, - EditModuleForm + EditModuleForm, + EditSwitchModuleForm, ) from subprocess import ( @@ -1070,6 +1072,7 @@ def del_port_profile(request, port_profile, **_kwargs): ) +@login_required @can_create(ModuleSwitch) def add_module(request): """ View used to add a Module object """ @@ -1123,6 +1126,60 @@ def del_module(request, module, **_kwargs): request ) +@login_required +@can_create(ModuleOnSwitch) +def add_module_on(request): + """Add a module to a switch""" + module_switch = EditSwitchModuleForm(request.POST or None) + if module_switch.is_valid(): + module_switch.save() + messages.success(request, _("The module added to that switch")) + return redirect(reverse('topologie:index-module')) + return form( + {'topoform': module_switch, 'action_name': _("Create")}, + 'topologie/topo.html', + request + ) + + +@login_required +@can_edit(ModuleOnSwitch) +def edit_module_on(request, module_instance, **_kwargs): + """ View used to edit a Module object """ + module = EditSwitchModuleForm(request.POST or None, instance=module_instance) + if module.is_valid(): + if module.changed_data: + module.save() + messages.success(request, _("The module was edited.")) + return redirect(reverse('topologie:index-module')) + return form( + {'topoform': module, 'action_name': _("Edit")}, + 'topologie/topo.html', + request + ) + + +@login_required +@can_delete(ModuleOnSwitch) +def del_module_on(request, module, **_kwargs): + """Compleete delete a module""" + if request.method == "POST": + try: + module.delete() + messages.success(request, _("The module was deleted.")) + except ProtectedError: + messages.error( + request, + (_("The module %s is used by another object, impossible to" + " deleted it.") % module) + ) + return redirect(reverse('topologie:index-module')) + return form( + {'objet': module, 'objet_name': _("Module")}, + 'topologie/delete.html', + request + ) + def make_machine_graph(): """