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.,