From 004f6099f5172a031b97189a6b8a51160668ea27 Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Mon, 26 Mar 2018 05:12:01 +0200 Subject: [PATCH] =?UTF-8?q?Switchs=20h=C3=A9ritent=20de=20machines?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- re2o/utils.py | 4 +- topologie/forms.py | 8 +-- topologie/migrations/0049_switchs_machine.py | 30 +++++++++ topologie/migrations/0050_port_new_switch.py | 21 +++++++ topologie/migrations/0051_transferports.py | 33 ++++++++++ topologie/migrations/0052_switchs_machine.py | 42 +++++++++++++ topologie/migrations/0053_finalsw.py | 22 +++++++ topologie/models.py | 2 +- topologie/templates/topologie/aff_switch.html | 6 +- topologie/views.py | 63 +++++++++---------- 10 files changed, 189 insertions(+), 42 deletions(-) create mode 100644 topologie/migrations/0049_switchs_machine.py create mode 100644 topologie/migrations/0050_port_new_switch.py create mode 100644 topologie/migrations/0051_transferports.py create mode 100644 topologie/migrations/0052_switchs_machine.py create mode 100644 topologie/migrations/0053_finalsw.py diff --git a/re2o/utils.py b/re2o/utils.py index fcb4f8a8..32be2432 100644 --- a/re2o/utils.py +++ b/re2o/utils.py @@ -213,8 +213,8 @@ class SortTable: 'default': ['-date'] } TOPOLOGIE_INDEX = { - 'switch_dns': ['switch_interface__domain__name'], - 'switch_ip': ['switch_interface__ipv4__ipv4'], + 'switch_dns': ['interface__domain__name'], + 'switch_ip': ['interface__ipv4__ipv4'], 'switch_loc': ['location'], 'switch_ports': ['number'], 'switch_stack': ['stack__name'], diff --git a/topologie/forms.py b/topologie/forms.py index 5032d15e..0337dc60 100644 --- a/topologie/forms.py +++ b/topologie/forms.py @@ -130,18 +130,18 @@ class EditAccessPointForm(EditMachineForm): fields = '__all__' -class EditSwitchForm(EditInterfaceForm): +class EditSwitchForm(EditMachineForm): """Permet d'éditer un switch : nom et nombre de ports""" class Meta: model = Switch - fields = ['machine', 'type', 'ipv4', 'mac_address', 'details', 'location', 'number', 'stack', 'stack_member_id'] + fields = '__all__' -class NewSwitchForm(EditInterfaceForm): +class NewSwitchForm(NewMachineForm): """Permet de créer un switch : emplacement, paramètres machine, membre d'un stack (option), nombre de ports (number)""" class Meta(EditSwitchForm.Meta): - fields = ['type', 'ipv4', 'mac_address', 'details', 'location', 'number', 'stack', 'stack_member_id'] + fields = ['name', 'location', 'number', 'stack', 'stack_member_id'] class EditRoomForm(ModelForm): diff --git a/topologie/migrations/0049_switchs_machine.py b/topologie/migrations/0049_switchs_machine.py new file mode 100644 index 00000000..040ab599 --- /dev/null +++ b/topologie/migrations/0049_switchs_machine.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2018-03-23 01:18 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('topologie', '0048_ap_machine'), + ] + + + + operations = [ + migrations.CreateModel( + name='NewSw', + fields=[ + ('machine_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='machines.Machine')), + ('location', models.CharField(max_length=255)), + ('number', models.PositiveIntegerField()), + ('stack_member_id', models.PositiveIntegerField(blank=True, null=True)), + ('model', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='topologie.ModelSwitch')), + ('stack', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='topologie.Stack')), + ], + bases=('machines.machine',), + ), + ] diff --git a/topologie/migrations/0050_port_new_switch.py b/topologie/migrations/0050_port_new_switch.py new file mode 100644 index 00000000..b03a8ee5 --- /dev/null +++ b/topologie/migrations/0050_port_new_switch.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2018-03-25 00:52 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('topologie', '0049_switchs_machine'), + ] + + operations = [ + migrations.AddField( + model_name='port', + name='new_sw', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='ports', to='topologie.NewSw'), + ), + ] diff --git a/topologie/migrations/0051_transferports.py b/topologie/migrations/0051_transferports.py new file mode 100644 index 00000000..2a67e6be --- /dev/null +++ b/topologie/migrations/0051_transferports.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2017-12-31 19:53 +from __future__ import unicode_literals + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('topologie', '0050_port_new_switch'), + ] + + def transfer_port(apps, schema_editor): + db_alias = schema_editor.connection.alias + port = apps.get_model("topologie", "Port") + switch = apps.get_model("topologie", "NewSw") + port_list = port.objects.using(db_alias).all() + for p in port_list: + p.new_sw = switch.objects.filter(machine_ptr=p.switch.machine).first() + p.save() + + def untransfer_port(apps, schema_editor): + return + + operations = [ + migrations.RunPython(transfer_port, untransfer_port), + migrations.RemoveField( + model_name='port', + name='switch', + ), + migrations.RenameField('Port', 'new_sw', 'switch') + ] diff --git a/topologie/migrations/0052_switchs_machine.py b/topologie/migrations/0052_switchs_machine.py new file mode 100644 index 00000000..b32b3c18 --- /dev/null +++ b/topologie/migrations/0052_switchs_machine.py @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2018-03-23 01:18 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('topologie', '0051_transferports'), + ] + + def transfer_sw(apps, schema_editor): + db_alias = schema_editor.connection.alias + newswitch = apps.get_model("topologie", "NewSw") + switch = apps.get_model("topologie", "Switch") + machine = apps.get_model("machines", "Machine") + sw_list = switch.objects.using(db_alias).all() + for sw in sw_list: + new_sw = newswitch() + new_sw.location = sw.location + new_sw.number = sw.number + new_sw.details = sw.details + new_sw.stack = sw.stack + new_sw.stack_member_id = sw.stack_member_id + new_sw.model = sw.model + new_sw.machine_ptr_id = sw.interface_ptr.machine.pk + new_sw.__dict__.update(sw.interface_ptr.machine.__dict__) + new_sw.save() + + def untransfer_sw(apps, schema_editor): + return + + + operations = [ + migrations.RunPython(transfer_sw, untransfer_sw), + migrations.DeleteModel( + name='Switch', + ), + ] diff --git a/topologie/migrations/0053_finalsw.py b/topologie/migrations/0053_finalsw.py new file mode 100644 index 00000000..561e0dc3 --- /dev/null +++ b/topologie/migrations/0053_finalsw.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2018-03-23 01:18 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('topologie', '0052_switchs_machine'), + ] + + + + operations = [ + migrations.RenameModel( + old_name='NewSw', + new_name='Switch', + ), + ] diff --git a/topologie/models.py b/topologie/models.py index 76b24652..fd4b1d3d 100644 --- a/topologie/models.py +++ b/topologie/models.py @@ -156,7 +156,7 @@ class AccessPoint(Machine): return True, None -class Switch(Interface): +class Switch(Machine): """ Definition d'un switch. Contient un nombre de ports (number), un emplacement (location), un stack parent (optionnel, stack) et un id de membre dans le stack (stack_member_id) diff --git a/topologie/templates/topologie/aff_switch.html b/topologie/templates/topologie/aff_switch.html index c8c9e6e0..948cd209 100644 --- a/topologie/templates/topologie/aff_switch.html +++ b/topologie/templates/topologie/aff_switch.html @@ -46,10 +46,10 @@ with this program; if not, write to the Free Software Foundation, Inc., - {{switch}} + {{switch.interface_set.first}} - {{switch.ipv4}} + {{switch.interface_set.first.ipv4}} {{switch.location}} {{switch.number}} {{switch.stack.name}} @@ -62,7 +62,7 @@ with this program; if not, write to the Free Software Foundation, Inc., {% include 'buttons/edit.html' with href='topologie:edit-switch' id=switch.pk %} {% acl_end %} {% can_delete switch %} - {% include 'buttons/suppr.html' with href='machines:del-interface' id=switch.id %} + {% include 'buttons/suppr.html' with href='machines:del-machine' id=switch.id %} {% acl_end %} {% can_create Port %} {% include 'buttons/add.html' with href='topologie:create-ports' id=switch.pk desc='Création de ports'%} diff --git a/topologie/views.py b/topologie/views.py index 3fa846df..68febece 100644 --- a/topologie/views.py +++ b/topologie/views.py @@ -92,9 +92,9 @@ from preferences.models import AssoOption, GeneralOption def index(request): """ Vue d'affichage de tous les swicthes""" switch_list = Switch.objects\ - .select_related('domain__extension')\ - .select_related('ipv4')\ - .select_related('domain')\ + .prefetch_related('interface_set__domain__extension')\ + .prefetch_related('interface_set__ipv4__ip_type')\ + .prefetch_related('interface_set__type__ip_type__extension')\ .select_related('stack') switch_list = SortTable.sort( switch_list, @@ -127,9 +127,8 @@ def index_port(request, switch, switch_id): .select_related('room')\ .select_related('machine_interface__domain__extension')\ .select_related('machine_interface__machine__user')\ - .select_related( - 'related__switch__domain__extension' - )\ + .select_related('related__switch')\ + .prefetch_related('related__switch__interface_set__domain__extension')\ .select_related('switch') port_list = SortTable.sort( port_list, @@ -385,45 +384,45 @@ def new_switch(request): request.POST or None, user=request.user ) - machine = NewMachineForm( + interface = AddInterfaceForm( request.POST or None, user=request.user ) domain = DomainForm( request.POST or None, ) - if switch.is_valid() and machine.is_valid(): + if switch.is_valid() and interface.is_valid(): user = AssoOption.get_cached_value('utilisateur_asso') if not user: messages.error(request, "L'user association n'existe pas encore,\ veuillez le créer ou le linker dans preferences") return redirect(reverse('topologie:index')) - new_machine = machine.save(commit=False) - new_machine.user = user - new_switch_instance = switch.save(commit=False) - domain.instance.interface_parent = new_switch_instance + new_switch = switch.save(commit=False) + new_switch.user = user + new_interface_instance = interface.save(commit=False) + domain.instance.interface_parent = new_interface_instance if domain.is_valid(): new_domain_instance = domain.save(commit=False) with transaction.atomic(), reversion.create_revision(): - new_machine.save() + new_switch.save() reversion.set_user(request.user) reversion.set_comment("Création") - new_switch_instance.machine = new_machine + new_interface_instance.machine = new_switch with transaction.atomic(), reversion.create_revision(): - new_switch_instance.save() + new_interface_instance.save() reversion.set_user(request.user) reversion.set_comment("Création") - new_domain_instance.interface_parent = new_switch_instance + new_domain_instance.interface_parent = new_interface_instance with transaction.atomic(), reversion.create_revision(): new_domain_instance.save() reversion.set_user(request.user) reversion.set_comment("Création") messages.success(request, "Le switch a été créé") return redirect(reverse('topologie:index')) - i_mbf_param = generate_ipv4_mbf_param(switch, False) + i_mbf_param = generate_ipv4_mbf_param(interface, False) return form({ - 'topoform': switch, - 'machineform': machine, + 'topoform': interface, + 'machineform': switch, 'domainform': domain, 'i_mbf_param': i_mbf_param, 'device' : 'switch', @@ -479,32 +478,32 @@ def edit_switch(request, switch, switch_id): instance=switch, user=request.user ) - machine_form = EditMachineForm( + interface_form = EditInterfaceForm( request.POST or None, - instance=switch.machine, + instance=switch.interface_set.first(), user=request.user ) domain_form = DomainForm( request.POST or None, - instance=switch.domain + instance=switch.interface_set.first().domain ) - if switch_form.is_valid() and machine_form.is_valid(): - new_machine = machine_form.save(commit=False) - new_switch_instance = switch_form.save(commit=False) + if switch_form.is_valid() and interface_form.is_valid(): + new_switch = switch_form.save(commit=False) + new_interface_instance = interface_form.save(commit=False) new_domain = domain_form.save(commit=False) with transaction.atomic(), reversion.create_revision(): - new_machine.save() + new_switch.save() reversion.set_user(request.user) reversion.set_comment( "Champs modifié(s) : %s" % ', '.join( - field for field in machine_form.changed_data + field for field in switch_form.changed_data ) ) with transaction.atomic(), reversion.create_revision(): - new_switch_instance.save() + new_interface_instance.save() reversion.set_user(request.user) reversion.set_comment("Champs modifié(s) : %s" % ', '.join( - field for field in switch_form.changed_data) + field for field in interface_form.changed_data) ) with transaction.atomic(), reversion.create_revision(): new_domain.save() @@ -514,11 +513,11 @@ def edit_switch(request, switch, switch_id): ) messages.success(request, "Le switch a bien été modifié") return redirect(reverse('topologie:index')) - i_mbf_param = generate_ipv4_mbf_param(switch_form, False ) + i_mbf_param = generate_ipv4_mbf_param(interface_form, False ) return form({ 'id_switch': switch_id, - 'topoform': switch_form, - 'machineform': machine_form, + 'topoform': interface_form, + 'machineform': switch_form, 'domainform': domain_form, 'i_mbf_param': i_mbf_param, 'device' : 'switch',