8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-22 11:23:10 +00:00

Switchs héritent de machines

This commit is contained in:
Gabriel Detraz 2018-03-26 05:12:01 +02:00 committed by chirac
parent 119416cc4f
commit 004f6099f5
10 changed files with 189 additions and 42 deletions

View file

@ -213,8 +213,8 @@ class SortTable:
'default': ['-date'] 'default': ['-date']
} }
TOPOLOGIE_INDEX = { TOPOLOGIE_INDEX = {
'switch_dns': ['switch_interface__domain__name'], 'switch_dns': ['interface__domain__name'],
'switch_ip': ['switch_interface__ipv4__ipv4'], 'switch_ip': ['interface__ipv4__ipv4'],
'switch_loc': ['location'], 'switch_loc': ['location'],
'switch_ports': ['number'], 'switch_ports': ['number'],
'switch_stack': ['stack__name'], 'switch_stack': ['stack__name'],

View file

@ -130,18 +130,18 @@ class EditAccessPointForm(EditMachineForm):
fields = '__all__' fields = '__all__'
class EditSwitchForm(EditInterfaceForm): class EditSwitchForm(EditMachineForm):
"""Permet d'éditer un switch : nom et nombre de ports""" """Permet d'éditer un switch : nom et nombre de ports"""
class Meta: class Meta:
model = Switch 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, """Permet de créer un switch : emplacement, paramètres machine,
membre d'un stack (option), nombre de ports (number)""" membre d'un stack (option), nombre de ports (number)"""
class Meta(EditSwitchForm.Meta): 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): class EditRoomForm(ModelForm):

View file

@ -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',),
),
]

View file

@ -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'),
),
]

View file

@ -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')
]

View file

@ -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',
),
]

View file

@ -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',
),
]

View file

@ -156,7 +156,7 @@ class AccessPoint(Machine):
return True, None return True, None
class Switch(Interface): class Switch(Machine):
""" Definition d'un switch. Contient un nombre de ports (number), """ Definition d'un switch. Contient un nombre de ports (number),
un emplacement (location), un stack parent (optionnel, stack) un emplacement (location), un stack parent (optionnel, stack)
et un id de membre dans le stack (stack_member_id) et un id de membre dans le stack (stack_member_id)

View file

@ -46,10 +46,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
<tr> <tr>
<td> <td>
<a title="Configuer" href="{% url 'topologie:index-port' switch.pk %}"> <a title="Configuer" href="{% url 'topologie:index-port' switch.pk %}">
{{switch}} {{switch.interface_set.first}}
</a> </a>
</td> </td>
<td>{{switch.ipv4}}</td> <td>{{switch.interface_set.first.ipv4}}</td>
<td>{{switch.location}}</td> <td>{{switch.location}}</td>
<td>{{switch.number}}</td> <td>{{switch.number}}</td>
<td>{{switch.stack.name}}</td> <td>{{switch.stack.name}}</td>
@ -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 %} {% include 'buttons/edit.html' with href='topologie:edit-switch' id=switch.pk %}
{% acl_end %} {% acl_end %}
{% can_delete switch %} {% 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 %} {% acl_end %}
{% can_create Port %} {% can_create Port %}
{% include 'buttons/add.html' with href='topologie:create-ports' id=switch.pk desc='Création de ports'%} {% include 'buttons/add.html' with href='topologie:create-ports' id=switch.pk desc='Création de ports'%}

View file

@ -92,9 +92,9 @@ from preferences.models import AssoOption, GeneralOption
def index(request): def index(request):
""" Vue d'affichage de tous les swicthes""" """ Vue d'affichage de tous les swicthes"""
switch_list = Switch.objects\ switch_list = Switch.objects\
.select_related('domain__extension')\ .prefetch_related('interface_set__domain__extension')\
.select_related('ipv4')\ .prefetch_related('interface_set__ipv4__ip_type')\
.select_related('domain')\ .prefetch_related('interface_set__type__ip_type__extension')\
.select_related('stack') .select_related('stack')
switch_list = SortTable.sort( switch_list = SortTable.sort(
switch_list, switch_list,
@ -127,9 +127,8 @@ def index_port(request, switch, switch_id):
.select_related('room')\ .select_related('room')\
.select_related('machine_interface__domain__extension')\ .select_related('machine_interface__domain__extension')\
.select_related('machine_interface__machine__user')\ .select_related('machine_interface__machine__user')\
.select_related( .select_related('related__switch')\
'related__switch__domain__extension' .prefetch_related('related__switch__interface_set__domain__extension')\
)\
.select_related('switch') .select_related('switch')
port_list = SortTable.sort( port_list = SortTable.sort(
port_list, port_list,
@ -385,45 +384,45 @@ def new_switch(request):
request.POST or None, request.POST or None,
user=request.user user=request.user
) )
machine = NewMachineForm( interface = AddInterfaceForm(
request.POST or None, request.POST or None,
user=request.user user=request.user
) )
domain = DomainForm( domain = DomainForm(
request.POST or None, 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') user = AssoOption.get_cached_value('utilisateur_asso')
if not user: if not user:
messages.error(request, "L'user association n'existe pas encore,\ messages.error(request, "L'user association n'existe pas encore,\
veuillez le créer ou le linker dans preferences") veuillez le créer ou le linker dans preferences")
return redirect(reverse('topologie:index')) return redirect(reverse('topologie:index'))
new_machine = machine.save(commit=False) new_switch = switch.save(commit=False)
new_machine.user = user new_switch.user = user
new_switch_instance = switch.save(commit=False) new_interface_instance = interface.save(commit=False)
domain.instance.interface_parent = new_switch_instance domain.instance.interface_parent = new_interface_instance
if domain.is_valid(): if domain.is_valid():
new_domain_instance = domain.save(commit=False) new_domain_instance = domain.save(commit=False)
with transaction.atomic(), reversion.create_revision(): with transaction.atomic(), reversion.create_revision():
new_machine.save() new_switch.save()
reversion.set_user(request.user) reversion.set_user(request.user)
reversion.set_comment("Création") reversion.set_comment("Création")
new_switch_instance.machine = new_machine new_interface_instance.machine = new_switch
with transaction.atomic(), reversion.create_revision(): with transaction.atomic(), reversion.create_revision():
new_switch_instance.save() new_interface_instance.save()
reversion.set_user(request.user) reversion.set_user(request.user)
reversion.set_comment("Création") 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(): with transaction.atomic(), reversion.create_revision():
new_domain_instance.save() new_domain_instance.save()
reversion.set_user(request.user) reversion.set_user(request.user)
reversion.set_comment("Création") reversion.set_comment("Création")
messages.success(request, "Le switch a été créé") messages.success(request, "Le switch a été créé")
return redirect(reverse('topologie:index')) 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({ return form({
'topoform': switch, 'topoform': interface,
'machineform': machine, 'machineform': switch,
'domainform': domain, 'domainform': domain,
'i_mbf_param': i_mbf_param, 'i_mbf_param': i_mbf_param,
'device' : 'switch', 'device' : 'switch',
@ -479,32 +478,32 @@ def edit_switch(request, switch, switch_id):
instance=switch, instance=switch,
user=request.user user=request.user
) )
machine_form = EditMachineForm( interface_form = EditInterfaceForm(
request.POST or None, request.POST or None,
instance=switch.machine, instance=switch.interface_set.first(),
user=request.user user=request.user
) )
domain_form = DomainForm( domain_form = DomainForm(
request.POST or None, request.POST or None,
instance=switch.domain instance=switch.interface_set.first().domain
) )
if switch_form.is_valid() and machine_form.is_valid(): if switch_form.is_valid() and interface_form.is_valid():
new_machine = machine_form.save(commit=False) new_switch = switch_form.save(commit=False)
new_switch_instance = switch_form.save(commit=False) new_interface_instance = interface_form.save(commit=False)
new_domain = domain_form.save(commit=False) new_domain = domain_form.save(commit=False)
with transaction.atomic(), reversion.create_revision(): with transaction.atomic(), reversion.create_revision():
new_machine.save() new_switch.save()
reversion.set_user(request.user) reversion.set_user(request.user)
reversion.set_comment( reversion.set_comment(
"Champs modifié(s) : %s" % ', '.join( "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(): with transaction.atomic(), reversion.create_revision():
new_switch_instance.save() new_interface_instance.save()
reversion.set_user(request.user) reversion.set_user(request.user)
reversion.set_comment("Champs modifié(s) : %s" % ', '.join( 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(): with transaction.atomic(), reversion.create_revision():
new_domain.save() new_domain.save()
@ -514,11 +513,11 @@ def edit_switch(request, switch, switch_id):
) )
messages.success(request, "Le switch a bien été modifié") messages.success(request, "Le switch a bien été modifié")
return redirect(reverse('topologie:index')) 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({ return form({
'id_switch': switch_id, 'id_switch': switch_id,
'topoform': switch_form, 'topoform': interface_form,
'machineform': machine_form, 'machineform': switch_form,
'domainform': domain_form, 'domainform': domain_form,
'i_mbf_param': i_mbf_param, 'i_mbf_param': i_mbf_param,
'device' : 'switch', 'device' : 'switch',