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

Switch hérite de interface

This commit is contained in:
Gabriel Detraz 2018-03-25 04:05:26 +02:00 committed by chirac
parent cb87b3f6c0
commit 3a2e007372
13 changed files with 253 additions and 126 deletions

View file

@ -78,7 +78,7 @@ class EditPortForm(ModelForm):
self.fields['machine_interface'].queryset = Interface.objects.all()\
.select_related('domain__extension')
self.fields['related'].queryset = Port.objects.all()\
.select_related('switch__switch_interface__domain__extension')\
.select_related('switch__domain__extension')\
.order_by('switch', 'port')
@ -95,7 +95,7 @@ class AddPortForm(ModelForm):
self.fields['machine_interface'].queryset = Interface.objects.all()\
.select_related('domain__extension')
self.fields['related'].queryset = Port.objects.all()\
.select_related('switch__switch_interface__domain__extension')\
.select_related('switch__domain__extension')\
.order_by('switch', 'port')
@ -126,32 +126,30 @@ class EditBorneForm(EditInterfaceForm):
fields = ['machine', 'type', 'ipv4', 'mac_address', 'details', 'location']
class EditSwitchForm(ModelForm):
class EditSwitchForm(EditInterfaceForm):
"""Permet d'éditer un switch : nom et nombre de ports"""
class Meta:
model = Switch
fields = '__all__'
fields = ['machine', 'type', 'ipv4', 'mac_address', 'details', 'location', 'number', 'stack', 'stack_member_id']
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', self.Meta.model.__name__)
super(EditSwitchForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['switch_interface'].queryset = Interface.objects.all()\
.select_related('domain__extension')
self.fields['location'].label = 'Localisation'
self.fields['number'].label = 'Nombre de ports'
# def __init__(self, *args, **kwargs):
# prefix = kwargs.pop('prefix', self.Meta.model.__name__)
# super(EditInterfaceForm, self).__init__(*args, prefix=prefix, **kwargs)
# self.fields['location'].label = 'Localisation'
# self.fields['number'].label = 'Nombre de ports'
class NewSwitchForm(ModelForm):
class NewSwitchForm(EditInterfaceForm):
"""Permet de créer un switch : emplacement, paramètres machine,
membre d'un stack (option), nombre de ports (number)"""
class Meta(EditSwitchForm.Meta):
fields = ['location', 'number', 'details', 'stack', 'stack_member_id']
fields = ['type', 'ipv4', 'mac_address', 'details', 'location', 'number', 'stack', 'stack_member_id']
def __init__(self, *args, **kwargs):
prefix = kwargs.pop('prefix', self.Meta.model.__name__)
super(NewSwitchForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['location'].label = 'Localisation'
self.fields['number'].label = 'Nombre de ports'
#def __init__(self, *args, **kwargs):
# prefix = kwargs.pop('prefix', self.Meta.model.__name__)
# super(NewSwitchForm, self).__init__(*args, prefix=prefix, **kwargs)
# self.fields['location'].label = 'Localisation'
# self.fields['number'].label = 'Nombre de ports'
class EditRoomForm(ModelForm):
"""Permet d'éediter le nom et commentaire d'une prise murale"""

View file

@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.7 on 2018-03-25 00:27
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('machines', '0076_auto_20180130_1623'),
('topologie', '0036_transferborne'),
]
operations = [
migrations.CreateModel(
name='NewSwitch',
fields=[
('interface_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='machines.Interface')),
('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.interface',),
),
migrations.AlterUniqueTogether(
name='newswitch',
unique_together=set([('stack', 'stack_member_id')]),
),
]

View file

@ -0,0 +1,37 @@
# -*- 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', '0037_auto_20180325_0127'),
]
def transfer_sw(apps, schema_editor):
db_alias = schema_editor.connection.alias
newswitch = apps.get_model("topologie", "NewSwitch")
switch = apps.get_model("topologie", "Switch")
interface = apps.get_model("machines", "Interface")
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.interface_ptr_id = sw.switch_interface.pk
new_sw.__dict__.update(sw.switch_interface.__dict__)
new_sw.save()
def untransfer_sw(apps, schema_editor):
return
operations = [
migrations.RunPython(transfer_sw, untransfer_sw),
]

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', '0038_transfersw'),
]
operations = [
migrations.AddField(
model_name='port',
name='new_switch',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='ports', to='topologie.NewSwitch'),
),
]

View file

@ -0,0 +1,28 @@
# -*- 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', '0039_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", "NewSwitch")
port_list = port.objects.using(db_alias).all()
for p in port_list:
p.new_switch = switch.objects.filter(interface_ptr=p.switch.switch_interface).first()
p.save()
def untransfer_port(apps, schema_editor):
return
operations = [
migrations.RunPython(transfer_port, untransfer_port),
]

View file

@ -0,0 +1,20 @@
# -*- 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', '0040_transferports'),
]
operations = [
migrations.RemoveField(
model_name='port',
name='switch',
),
migrations.RenameField('Port', 'new_switch', 'switch')
]

View file

@ -0,0 +1,18 @@
# -*- 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', '0041_transferportsw'),
]
operations = [
migrations.DeleteModel(
name='Switch',
),
]

View file

@ -0,0 +1,16 @@
# -*- 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', '0042_transferswitch'),
]
operations = [
migrations.RenameModel(old_name='NewSwitch', new_name='Switch'),
]

View file

@ -156,7 +156,7 @@ class Borne(Interface):
return True, None
class Switch(models.Model):
class Switch(Interface):
""" 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)
@ -170,13 +170,9 @@ class Switch(models.Model):
id_max de la stack parente"""
PRETTY_NAME = "Switch / Commutateur"
switch_interface = models.OneToOneField(
'machines.Interface',
on_delete=models.CASCADE
)
location = models.CharField(max_length=255)
number = models.PositiveIntegerField()
details = models.CharField(max_length=255, blank=True)
stack = models.ForeignKey(
'topologie.Stack',
blank=True,
@ -194,7 +190,7 @@ class Switch(models.Model):
class Meta:
unique_together = ('stack', 'stack_member_id')
permissions = (
("view_switch", "Peut voir un objet switch"),
("view_newswitch", "Peut voir un objet switch"),
)
def get_instance(switch_id, *args, **kwargs):
@ -224,8 +220,8 @@ class Switch(models.Model):
return False, u"Vous n'avez pas le droit de voir les switch"
return True, None
def __str__(self):
return self.location + ' ' + str(self.switch_interface)
# def __str__(self):
# return self.location + ' ' + str(self.switch_interface)
def clean(self):
""" Verifie que l'id stack est dans le bon range"""
@ -421,7 +417,7 @@ class Port(models.Model):
def get_instance(port_id, *args, **kwargs):
return Port.objects\
.select_related('switch__switch_interface__domain__extension')\
.select_related('switch__domain__extension')\
.select_related('machine_interface__domain__extension')\
.select_related('machine_interface__switch')\
.select_related('room')\

View file

@ -46,10 +46,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
<tr>
<td>
<a title="Configuer" href="{% url 'topologie:index-port' switch.pk %}">
{{switch.switch_interface}}
{{switch}}
</a>
</td>
<td>{{switch.switch_interface.ipv4}}</td>
<td>{{switch.ipv4}}</td>
<td>{{switch.location}}</td>
<td>{{switch.number}}</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 %}
{% acl_end %}
{% can_delete switch %}
{% include 'buttons/suppr.html' with href='machines:del-interface' id=switch.switch_interface.id %}
{% include 'buttons/suppr.html' with href='machines:del-interface' 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'%}

View file

@ -32,44 +32,17 @@ with this program; if not, write to the Free Software Foundation, Inc.,
{% if topoform %}
{% bootstrap_form_errors topoform %}
{% endif %}
{% if machineform %}
{% bootstrap_form_errors machineform %}
{% endif %}
{% if interfaceform %}
{% bootstrap_form_errors interfaceform %}
{% endif %}
{% if domainform %}
{% bootstrap_form_errors domainform %}
{% endif %}
{% if id_switch %}
<a class="btn btn-primary" href="{% url "topologie:index-port" id_switch %}" role="button">{% bootstrap_icon "list" %} Aller à la liste des ports</a>
{% endif %}
<form class="form" method="post">
{% csrf_token %}
{% if topoform %}
<h3>Réglage spécifiques du switch</h3>
{% massive_bootstrap_form topoform 'switch_interface' %}
{% endif %}
{% if machineform %}
<h3>Réglages généraux de la machine associée au switch</h3>
{% massive_bootstrap_form machineform 'user' %}
{% endif %}
{% if interfaceform %}
<h3>Réglages généraux de l'interface associée au switch</h3>
{% if i_mbf_param %}
{% massive_bootstrap_form interfaceform 'ipv4,machine' mbf_param=i_mbf_param %}
{% else %}
{% massive_bootstrap_form interfaceform 'ipv4,machine' %}
{% endif %}
{% endif %}
{% if domainform %}
<h3>Nom de la machine</h3>
{% bootstrap_form domainform %}
{% endif %}
{% bootstrap_button "Créer ou modifier" button_type="submit" icon="ok" %}
{% bootstrap_button "Créer" button_type="submit" icon="ok" %}
</form>
<br />
<br />

View file

@ -26,7 +26,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
{% load bootstrap3 %}
{% load massive_bootstrap_form %}
{% block title %}Création et modification d'une borne{% endblock %}
{% block title %}Création et modification d'un objet topologie{% endblock %}
{% block content %}
{% if topoform %}
@ -44,11 +44,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
<form class="form" method="post">
{% csrf_token %}
{% if topoform %}
<h3>Réglage spécifiques de la borne</h3>
<h3>Réglage spécifiques du {{ device }}</h3>
{% massive_bootstrap_form topoform 'ipv4,machine' mbf_param=i_mbf_param%}
{% endif %}
{% if machineform %}
<h3>Réglages généraux de la machine associée à la borne</h3>
<h3>Réglages généraux de la machine associée au {{ device }}</h3>
{% massive_bootstrap_form machineform 'user' %}
{% endif %}
{% if domainform %}

View file

@ -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('switch_interface__domain__extension')\
.select_related('switch_interface__ipv4')\
.select_related('switch_interface__domain')\
.select_related('domain__extension')\
.select_related('ipv4')\
.select_related('domain')\
.select_related('stack')
switch_list = SortTable.sort(
switch_list,
@ -128,7 +128,7 @@ def index_port(request, switch, switch_id):
.select_related('machine_interface__domain__extension')\
.select_related('machine_interface__machine__user')\
.select_related(
'related__switch__switch_interface__domain__extension'
'related__switch__domain__extension'
)\
.select_related('switch')
port_list = SortTable.sort(
@ -203,7 +203,7 @@ def index_borne(request):
def index_stack(request):
"""Affichage de la liste des stacks (affiche l'ensemble des switches)"""
stack_list = Stack.objects\
.prefetch_related('switch_set__switch_interface__domain__extension')
.prefetch_related('switch_set__domain__extension')
stack_list = SortTable.sort(
stack_list,
request.GET.get('col'),
@ -381,19 +381,18 @@ def new_switch(request):
""" Creation d'un switch. Cree en meme temps l'interface et la machine
associée. Vue complexe. Appelle successivement les 4 models forms
adaptés : machine, interface, domain et switch"""
switch = NewSwitchForm(request.POST or None)
switch = NewSwitchForm(
request.POST or None,
user=request.user
)
machine = NewMachineForm(
request.POST or None,
user=request.user
)
interface = AddInterfaceForm(
request.POST or None,
user=request.user
)
domain = DomainForm(
request.POST or None,
)
if switch.is_valid() and machine.is_valid() and interface.is_valid():
if switch.is_valid() and machine.is_valid():
user = AssoOption.get_cached_value('utilisateur_asso')
if not user:
messages.error(request, "L'user association n'existe pas encore,\
@ -401,38 +400,34 @@ def new_switch(request):
return redirect(reverse('topologie:index'))
new_machine = machine.save(commit=False)
new_machine.user = user
new_interface = interface.save(commit=False)
new_switch_instance = switch.save(commit=False)
new_domain_instance = domain.save(commit=False)
with transaction.atomic(), reversion.create_revision():
new_machine.save()
reversion.set_user(request.user)
reversion.set_comment("Création")
new_interface.machine = new_machine
with transaction.atomic(), reversion.create_revision():
new_interface.save()
reversion.set_user(request.user)
reversion.set_comment("Création")
new_domain_instance.interface_parent = new_interface
with transaction.atomic(), reversion.create_revision():
new_domain_instance.save()
reversion.set_user(request.user)
reversion.set_comment("Création")
new_switch_instance.switch_interface = new_interface
with transaction.atomic(), reversion.create_revision():
new_switch_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( interface, False)
domain.instance.interface_parent = new_switch_instance
if domain.is_valid():
new_domain_instance = domain.save(commit=False)
with transaction.atomic(), reversion.create_revision():
new_machine.save()
reversion.set_user(request.user)
reversion.set_comment("Création")
new_switch_instance.machine = new_machine
with transaction.atomic(), reversion.create_revision():
new_switch_instance.save()
reversion.set_user(request.user)
reversion.set_comment("Création")
new_domain_instance.interface_parent = new_switch_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)
return form({
'topoform': switch,
'machineform': machine,
'interfaceform': interface,
'domainform': domain,
'i_mbf_param': i_mbf_param
}, 'topologie/switch.html', request)
'i_mbf_param': i_mbf_param,
'device' : 'switch',
}, 'topologie/topo_more.html', request)
@login_required
@ -470,7 +465,6 @@ def create_ports(request, switch_id):
'topologie:index-port',
kwargs={'switch_id':switch_id}
))
return form({'id_switch': switch_id, 'topoform': port_form}, 'topologie/switch.html', request)
@ -480,24 +474,21 @@ def edit_switch(request, switch, switch_id):
""" Edition d'un switch. Permet de chambre nombre de ports,
place dans le stack, interface et machine associée"""
switch_form = EditSwitchForm(request.POST or None, instance=switch)
machine_form = EditMachineForm(
switch_form = EditSwitchForm(
request.POST or None,
instance=switch.switch_interface.machine,
instance=switch,
user=request.user
)
interface_form = EditInterfaceForm(
machine_form = EditMachineForm(
request.POST or None,
instance=switch.switch_interface,
instance=switch.machine,
user=request.user
)
domain_form = DomainForm(
request.POST or None,
instance=switch.switch_interface.domain
instance=switch.domain
)
if switch_form.is_valid() and machine_form.is_valid()\
and interface_form.is_valid():
new_interface = interface_form.save(commit=False)
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)
new_domain = domain_form.save(commit=False)
@ -510,10 +501,10 @@ def edit_switch(request, switch, switch_id):
)
)
with transaction.atomic(), reversion.create_revision():
new_interface.save()
new_switch_instance.save()
reversion.set_user(request.user)
reversion.set_comment("Champs modifié(s) : %s" % ', '.join(
field for field in interface_form.changed_data)
field for field in switch_form.changed_data)
)
with transaction.atomic(), reversion.create_revision():
new_domain.save()
@ -521,23 +512,17 @@ def edit_switch(request, switch, switch_id):
reversion.set_comment("Champs modifié(s) : %s" % ', '.join(
field for field in domain_form.changed_data)
)
with transaction.atomic(), reversion.create_revision():
new_switch_instance.save()
reversion.set_user(request.user)
reversion.set_comment("Champs modifié(s) : %s" % ', '.join(
field for field in switch_form.changed_data)
)
messages.success(request, "Le switch a bien été modifié")
return redirect(reverse('topologie:index'))
i_mbf_param = generate_ipv4_mbf_param( interface_form, False )
i_mbf_param = generate_ipv4_mbf_param(switch_form, False )
return form({
'id_switch': switch_id,
'topoform': switch_form,
'machineform': machine_form,
'interfaceform': interface_form,
'domainform': domain_form,
'i_mbf_param': i_mbf_param
}, 'topologie/switch.html', request)
'i_mbf_param': i_mbf_param,
'device' : 'switch',
}, 'topologie/topo_more.html', request)
@login_required
@ -590,8 +575,9 @@ def new_borne(request):
'topoform': borne,
'machineform': machine,
'domainform': domain,
'i_mbf_param': i_mbf_param
}, 'topologie/borne.html', request)
'i_mbf_param': i_mbf_param,
'device' : 'wifi ap',
}, 'topologie/topo_more.html', request)
@login_required
@ -649,8 +635,9 @@ def edit_borne(request, borne, borne_id):
'topoform': borne_form,
'machineform': machine_form,
'domainform': domain_form,
'i_mbf_param': i_mbf_param
}, 'topologie/borne.html', request)
'i_mbf_param': i_mbf_param,
'device' : 'wifi ap',
}, 'topologie/topo_more.html', request)
@login_required