8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-05 01:16:27 +00:00

Pylint compliance on topologie

This commit is contained in:
Maël Kervella 2018-04-14 22:06:29 +00:00
parent 595c6b99fa
commit 4a1b97bbe3
5 changed files with 118 additions and 102 deletions

View file

@ -20,5 +20,9 @@
# You should have received a copy of the GNU General Public License along # You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc., # with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""topologie
The app in charge of handling all the informations about the network
topology like the switches, the rooms, how are the connections, ...
"""
from .acl import * from .acl import *

View file

@ -32,15 +32,17 @@ NewSwitchForm)
from __future__ import unicode_literals from __future__ import unicode_literals
from django import forms
from django.forms import ModelForm
from django.db.models import Prefetch
from machines.models import Interface from machines.models import Interface
from machines.forms import ( from machines.forms import (
EditInterfaceForm,
EditMachineForm, EditMachineForm,
NewMachineForm NewMachineForm
) )
from django import forms from re2o.mixins import FormRevMixin
from django.forms import ModelForm, Form
from django.db.models import Prefetch
from .models import ( from .models import (
Port, Port,
Switch, Switch,
@ -52,7 +54,6 @@ from .models import (
SwitchBay, SwitchBay,
Building, Building,
) )
from re2o.mixins import FormRevMixin
class PortForm(FormRevMixin, ModelForm): class PortForm(FormRevMixin, ModelForm):

View file

@ -47,7 +47,7 @@ from django.db import IntegrityError
from django.db import transaction from django.db import transaction
from reversion import revisions as reversion from reversion import revisions as reversion
from machines.models import Machine, Interface, regen from machines.models import Machine, regen
from re2o.mixins import AclMixin, RevMixin from re2o.mixins import AclMixin, RevMixin
@ -193,6 +193,7 @@ class Switch(AclMixin, Machine):
ValidationError("Création d'un port existant.") ValidationError("Création d'un port existant.")
def main_interface(self): def main_interface(self):
""" Returns the 'main' interface of the switch """
return self.interface_set.first() return self.interface_set.first()
def __str__(self): def __str__(self):
@ -332,14 +333,15 @@ class Port(AclMixin, RevMixin, models.Model):
("view_port", "Peut voir un objet port"), ("view_port", "Peut voir un objet port"),
) )
def get_instance(portid, *args, **kwargs): @classmethod
return Port.objects\ def get_instance(cls, portid, *_args, **kwargs):
.select_related('machine_interface__domain__extension')\ return (cls.objects
.select_related('machine_interface__machine__switch')\ .select_related('machine_interface__domain__extension')
.select_related('room')\ .select_related('machine_interface__machine__switch')
.select_related('related')\ .select_related('room')
.prefetch_related('switch__interface_set__domain__extension')\ .select_related('related')
.get(pk=portid) .prefetch_related('switch__interface_set__domain__extension')
.get(pk=portid))
def make_port_related(self): def make_port_related(self):
""" Synchronise le port distant sur self""" """ Synchronise le port distant sur self"""
@ -364,18 +366,24 @@ class Port(AclMixin, RevMixin, models.Model):
cohérence""" cohérence"""
if hasattr(self, 'switch'): if hasattr(self, 'switch'):
if self.port > self.switch.number: if self.port > self.switch.number:
raise ValidationError("Ce port ne peut exister,\ raise ValidationError(
numero trop élevé") "Ce port ne peut exister, numero trop élevé"
if self.room and self.machine_interface or self.room and\ )
self.related or self.machine_interface and self.related: if (self.room and self.machine_interface or
raise ValidationError("Chambre, interface et related_port sont\ self.room and self.related or
mutuellement exclusifs") self.machine_interface and self.related):
raise ValidationError(
"Chambre, interface et related_port sont mutuellement "
"exclusifs"
)
if self.related == self: if self.related == self:
raise ValidationError("On ne peut relier un port à lui même") raise ValidationError("On ne peut relier un port à lui même")
if self.related and not self.related.related: if self.related and not self.related.related:
if self.related.machine_interface or self.related.room: if self.related.machine_interface or self.related.room:
raise ValidationError("Le port relié est déjà occupé, veuillez\ raise ValidationError(
le libérer avant de créer une relation") "Le port relié est déjà occupé, veuillez le libérer "
"avant de créer une relation"
)
else: else:
self.make_port_related() self.make_port_related()
elif hasattr(self, 'related_port'): elif hasattr(self, 'related_port'):
@ -403,18 +411,18 @@ class Room(AclMixin, RevMixin, models.Model):
@receiver(post_save, sender=AccessPoint) @receiver(post_save, sender=AccessPoint)
def ap_post_save(sender, **kwargs): def ap_post_save(_sender, **_kwargs):
"""Regeneration des noms des bornes vers le controleur""" """Regeneration des noms des bornes vers le controleur"""
regen('unifi-ap-names') regen('unifi-ap-names')
@receiver(post_delete, sender=AccessPoint) @receiver(post_delete, sender=AccessPoint)
def ap_post_delete(sender, **kwargs): def ap_post_delete(_sender, **_kwargs):
"""Regeneration des noms des bornes vers le controleur""" """Regeneration des noms des bornes vers le controleur"""
regen('unifi-ap-names') regen('unifi-ap-names')
@receiver(post_delete, sender=Stack) @receiver(post_delete, sender=Stack)
def stack_post_delete(sender, **kwargs): def stack_post_delete(_sender, **_kwargs):
"""Vide les id des switches membres d'une stack supprimée""" """Vide les id des switches membres d'une stack supprimée"""
Switch.objects.filter(stack=None).update(stack_member_id=None) Switch.objects.filter(stack=None).update(stack_member_id=None)

View file

@ -19,7 +19,10 @@
# You should have received a copy of the GNU General Public License along # You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc., # with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""topologie.tests
The tests for the Topologie module.
"""
from django.test import TestCase # from django.test import TestCase
# Create your tests here. # Create your tests here.

View file

@ -38,36 +38,11 @@ from __future__ import unicode_literals
from django.urls import reverse from django.urls import reverse
from django.shortcuts import render, redirect from django.shortcuts import render, redirect
from django.contrib import messages from django.contrib import messages
from django.contrib.auth.decorators import login_required, permission_required from django.contrib.auth.decorators import login_required
from django.db import IntegrityError from django.db import IntegrityError
from django.db import transaction
from django.db.models import ProtectedError, Prefetch from django.db.models import ProtectedError, Prefetch
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from topologie.models import (
Switch,
Port,
Room,
Stack,
ModelSwitch,
ConstructorSwitch,
AccessPoint,
SwitchBay,
Building
)
from topologie.forms import EditPortForm, NewSwitchForm, EditSwitchForm
from topologie.forms import (
AddPortForm,
EditRoomForm,
StackForm,
EditModelSwitchForm,
EditConstructorSwitchForm,
CreatePortsForm,
AddAccessPointForm,
EditAccessPointForm,
EditSwitchBayForm,
EditBuildingForm
)
from users.views import form from users.views import form
from re2o.utils import re2o_paginator, SortTable from re2o.utils import re2o_paginator, SortTable
from re2o.acl import ( from re2o.acl import (
@ -79,8 +54,6 @@ from re2o.acl import (
) )
from machines.forms import ( from machines.forms import (
DomainForm, DomainForm,
NewMachineForm,
EditMachineForm,
EditInterfaceForm, EditInterfaceForm,
AddInterfaceForm AddInterfaceForm
) )
@ -88,6 +61,33 @@ from machines.views import generate_ipv4_mbf_param
from machines.models import Interface from machines.models import Interface
from preferences.models import AssoOption, GeneralOption from preferences.models import AssoOption, GeneralOption
from .models import (
Switch,
Port,
Room,
Stack,
ModelSwitch,
ConstructorSwitch,
AccessPoint,
SwitchBay,
Building
)
from .forms import (
EditPortForm,
NewSwitchForm,
EditSwitchForm,
AddPortForm,
EditRoomForm,
StackForm,
EditModelSwitchForm,
EditConstructorSwitchForm,
CreatePortsForm,
AddAccessPointForm,
EditAccessPointForm,
EditSwitchBayForm,
EditBuildingForm
)
@login_required @login_required
@can_view_all(Switch) @can_view_all(Switch)
@ -296,7 +296,7 @@ def new_port(request, switchid):
@login_required @login_required
@can_edit(Port) @can_edit(Port)
def edit_port(request, port_object, portid): def edit_port(request, port_object, _portid):
""" Edition d'un port. Permet de changer le switch parent et """ Edition d'un port. Permet de changer le switch parent et
l'affectation du port""" l'affectation du port"""
@ -322,7 +322,7 @@ def edit_port(request, port_object, portid):
@login_required @login_required
@can_delete(Port) @can_delete(Port)
def del_port(request, port, portid): def del_port(request, port, _portid):
""" Supprime le port""" """ Supprime le port"""
if request.method == "POST": if request.method == "POST":
try: try:
@ -358,7 +358,7 @@ def new_stack(request):
@login_required @login_required
@can_edit(Stack) @can_edit(Stack)
def edit_stack(request, stack, stackid): def edit_stack(request, stack, _stackid):
"""Edition d'un stack (nombre de switches, nom...)""" """Edition d'un stack (nombre de switches, nom...)"""
stack = StackForm(request.POST or None, instance=stack) stack = StackForm(request.POST or None, instance=stack)
if stack.is_valid(): if stack.is_valid():
@ -374,7 +374,7 @@ def edit_stack(request, stack, stackid):
@login_required @login_required
@can_delete(Stack) @can_delete(Stack)
def del_stack(request, stack, stackid): def del_stack(request, stack, _stackid):
"""Supprime un stack""" """Supprime un stack"""
if request.method == "POST": if request.method == "POST":
try: try:
@ -392,7 +392,7 @@ def del_stack(request, stack, stackid):
@login_required @login_required
@can_edit(Stack) @can_edit(Stack)
def edit_switchs_stack(request, stack, stackid): def edit_switchs_stack(request, stack, _stackid):
"""Permet d'éditer la liste des switches dans une stack et l'ajouter""" """Permet d'éditer la liste des switches dans une stack et l'ajouter"""
if request.method == "POST": if request.method == "POST":
@ -429,17 +429,17 @@ def new_switch(request):
"créer ou le linker dans preferences") "créer ou le linker dans preferences")
) )
return redirect(reverse('topologie:index')) return redirect(reverse('topologie:index'))
new_switch = switch.save(commit=False) new_switch_obj = switch.save(commit=False)
new_switch.user = user new_switch_obj.user = user
new_interface_instance = interface.save(commit=False) new_interface_obj = interface.save(commit=False)
domain.instance.interface_parent = new_interface_instance domain.instance.interface_parent = new_interface_obj
if domain.is_valid(): if domain.is_valid():
new_domain_instance = domain.save(commit=False) new_domain_obj = domain.save(commit=False)
new_switch.save() new_switch_obj.save()
new_interface_instance.machine = new_switch new_interface_obj.machine = new_switch_obj
new_interface_instance.save() new_interface_obj.save()
new_domain_instance.interface_parent = new_interface_instance new_domain_obj.interface_parent = new_interface_obj
new_domain_instance.save() new_domain_obj.save()
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(interface, False) i_mbf_param = generate_ipv4_mbf_param(interface, False)
@ -518,15 +518,15 @@ def edit_switch(request, switch, switchid):
instance=switch.interface_set.first().domain instance=switch.interface_set.first().domain
) )
if switch_form.is_valid() and interface_form.is_valid(): if switch_form.is_valid() and interface_form.is_valid():
new_switch = switch_form.save(commit=False) new_switch_obj = switch_form.save(commit=False)
new_interface_instance = interface_form.save(commit=False) new_interface_obj = interface_form.save(commit=False)
new_domain = domain_form.save(commit=False) new_domain_obj = domain_form.save(commit=False)
if switch_form.changed_data: if switch_form.changed_data:
new_switch.save() new_switch_obj.save()
if interface_form.changed_data: if interface_form.changed_data:
new_interface_instance.save() new_interface_obj.save()
if domain_form.changed_data: if domain_form.changed_data:
new_domain.save() new_domain_obj.save()
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(interface_form, False) i_mbf_param = generate_ipv4_mbf_param(interface_form, False)
@ -570,17 +570,17 @@ def new_ap(request):
"créer ou le linker dans preferences") "créer ou le linker dans preferences")
) )
return redirect(reverse('topologie:index')) return redirect(reverse('topologie:index'))
new_ap = ap.save(commit=False) new_ap_obj = ap.save(commit=False)
new_ap.user = user new_ap_obj.user = user
new_interface = interface.save(commit=False) new_interface_obj = interface.save(commit=False)
domain.instance.interface_parent = new_interface domain.instance.interface_parent = new_interface_obj
if domain.is_valid(): if domain.is_valid():
new_domain_instance = domain.save(commit=False) new_domain_obj = domain.save(commit=False)
new_ap.save() new_ap_obj.save()
new_interface.machine = new_ap new_interface_obj.machine = new_ap_obj
new_interface.save() new_interface_obj.save()
new_domain_instance.interface_parent = new_interface new_domain_obj.interface_parent = new_interface_obj
new_domain_instance.save() new_domain_obj.save()
messages.success(request, "La borne a été créé") messages.success(request, "La borne a été créé")
return redirect(reverse('topologie:index-ap')) return redirect(reverse('topologie:index-ap'))
i_mbf_param = generate_ipv4_mbf_param(interface, False) i_mbf_param = generate_ipv4_mbf_param(interface, False)
@ -599,7 +599,7 @@ def new_ap(request):
@login_required @login_required
@can_edit(AccessPoint) @can_edit(AccessPoint)
def edit_ap(request, ap, accesspointid): def edit_ap(request, ap, _accesspointid):
""" Edition d'un switch. Permet de chambre nombre de ports, """ Edition d'un switch. Permet de chambre nombre de ports,
place dans le stack, interface et machine associée""" place dans le stack, interface et machine associée"""
interface_form = EditInterfaceForm( interface_form = EditInterfaceForm(
@ -625,15 +625,15 @@ def edit_ap(request, ap, accesspointid):
"créer ou le linker dans preferences") "créer ou le linker dans preferences")
) )
return redirect(reverse('topologie:index-ap')) return redirect(reverse('topologie:index-ap'))
new_ap = ap_form.save(commit=False) new_ap_obj = ap_form.save(commit=False)
new_interface = interface_form.save(commit=False) new_interface_obj = interface_form.save(commit=False)
new_domain = domain_form.save(commit=False) new_domain_obj = domain_form.save(commit=False)
if ap_form.changed_data: if ap_form.changed_data:
new_ap.save() new_ap_obj.save()
if interface_form.changed_data: if interface_form.changed_data:
new_interface.save() new_interface_obj.save()
if domain_form.changed_data: if domain_form.changed_data:
new_domain.save() new_domain_obj.save()
messages.success(request, "La borne a été modifiée") messages.success(request, "La borne a été modifiée")
return redirect(reverse('topologie:index-ap')) return redirect(reverse('topologie:index-ap'))
i_mbf_param = generate_ipv4_mbf_param(interface_form, False) i_mbf_param = generate_ipv4_mbf_param(interface_form, False)
@ -668,7 +668,7 @@ def new_room(request):
@login_required @login_required
@can_edit(Room) @can_edit(Room)
def edit_room(request, room, roomid): def edit_room(request, room, _roomid):
""" Edition numero et details de la chambre""" """ Edition numero et details de la chambre"""
room = EditRoomForm(request.POST or None, instance=room) room = EditRoomForm(request.POST or None, instance=room)
if room.is_valid(): if room.is_valid():
@ -685,7 +685,7 @@ def edit_room(request, room, roomid):
@login_required @login_required
@can_delete(Room) @can_delete(Room)
def del_room(request, room, roomid): def del_room(request, room, _roomid):
""" Suppression d'un chambre""" """ Suppression d'un chambre"""
if request.method == "POST": if request.method == "POST":
try: try:
@ -723,7 +723,7 @@ def new_model_switch(request):
@login_required @login_required
@can_edit(ModelSwitch) @can_edit(ModelSwitch)
def edit_model_switch(request, model_switch, modelswitchid): def edit_model_switch(request, model_switch, _modelswitchid):
""" Edition d'un modèle de switch""" """ Edition d'un modèle de switch"""
model_switch = EditModelSwitchForm( model_switch = EditModelSwitchForm(
@ -744,7 +744,7 @@ def edit_model_switch(request, model_switch, modelswitchid):
@login_required @login_required
@can_delete(ModelSwitch) @can_delete(ModelSwitch)
def del_model_switch(request, model_switch, modelswitchid): def del_model_switch(request, model_switch, _modelswitchid):
""" Suppression d'un modèle de switch""" """ Suppression d'un modèle de switch"""
if request.method == "POST": if request.method == "POST":
try: try:
@ -782,7 +782,7 @@ def new_switch_bay(request):
@login_required @login_required
@can_edit(SwitchBay) @can_edit(SwitchBay)
def edit_switch_bay(request, switch_bay, switchbayid): def edit_switch_bay(request, switch_bay, _switchbayid):
""" Edition d'une baie de switch""" """ Edition d'une baie de switch"""
switch_bay = EditSwitchBayForm(request.POST or None, instance=switch_bay) switch_bay = EditSwitchBayForm(request.POST or None, instance=switch_bay)
if switch_bay.is_valid(): if switch_bay.is_valid():
@ -799,7 +799,7 @@ def edit_switch_bay(request, switch_bay, switchbayid):
@login_required @login_required
@can_delete(SwitchBay) @can_delete(SwitchBay)
def del_switch_bay(request, switch_bay, switchbayid): def del_switch_bay(request, switch_bay, _switchbayid):
""" Suppression d'une baie de switch""" """ Suppression d'une baie de switch"""
if request.method == "POST": if request.method == "POST":
try: try:
@ -837,7 +837,7 @@ def new_building(request):
@login_required @login_required
@can_edit(Building) @can_edit(Building)
def edit_building(request, building, buildingid): def edit_building(request, building, _buildingid):
""" Edition d'un batiment""" """ Edition d'un batiment"""
building = EditBuildingForm(request.POST or None, instance=building) building = EditBuildingForm(request.POST or None, instance=building)
if building.is_valid(): if building.is_valid():
@ -854,7 +854,7 @@ def edit_building(request, building, buildingid):
@login_required @login_required
@can_delete(Building) @can_delete(Building)
def del_building(request, building, buildingid): def del_building(request, building, _buildingid):
""" Suppression d'un batiment""" """ Suppression d'un batiment"""
if request.method == "POST": if request.method == "POST":
try: try:
@ -892,7 +892,7 @@ def new_constructor_switch(request):
@login_required @login_required
@can_edit(ConstructorSwitch) @can_edit(ConstructorSwitch)
def edit_constructor_switch(request, constructor_switch, constructorswitchid): def edit_constructor_switch(request, constructor_switch, _constructorswitchid):
""" Edition d'un constructeur de switch""" """ Edition d'un constructeur de switch"""
constructor_switch = EditConstructorSwitchForm( constructor_switch = EditConstructorSwitchForm(
@ -913,7 +913,7 @@ def edit_constructor_switch(request, constructor_switch, constructorswitchid):
@login_required @login_required
@can_delete(ConstructorSwitch) @can_delete(ConstructorSwitch)
def del_constructor_switch(request, constructor_switch, constructorswitchid): def del_constructor_switch(request, constructor_switch, _constructorswitchid):
""" Suppression d'un constructeur de switch""" """ Suppression d'un constructeur de switch"""
if request.method == "POST": if request.method == "POST":
try: try: