Better linter score on topologie

This commit is contained in:
chapeau 2025-07-06 08:55:39 +02:00
parent 2b6131ecec
commit 8796d01608
6 changed files with 58 additions and 80 deletions

View file

@ -47,68 +47,46 @@ from .models import (
class StackAdmin(VersionAdmin):
"""Admin class of stacks (includes switches)."""
pass
class SwitchAdmin(VersionAdmin):
"""Admin class of switches."""
pass
class PortAdmin(VersionAdmin):
"""Admin class of switch ports."""
pass
class AccessPointAdmin(VersionAdmin):
"""Admin class of APs."""
pass
class RoomAdmin(VersionAdmin):
"""Admin class of rooms."""
pass
class ModelSwitchAdmin(VersionAdmin):
"""Admin class of switch models."""
pass
class ConstructorSwitchAdmin(VersionAdmin):
"""Admin class of switch constructors."""
pass
class SwitchBayAdmin(VersionAdmin):
"""Admin class of switch bays."""
pass
class BuildingAdmin(VersionAdmin):
"""Admin class of buildings."""
pass
class DormitoryAdmin(VersionAdmin):
"""Admin class of dormitories."""
pass
class PortProfileAdmin(VersionAdmin):
"""Admin class of port profiles."""
pass
admin.site.register(Port, PortAdmin)
admin.site.register(AccessPoint, AccessPointAdmin)

View file

@ -23,7 +23,7 @@ from rest_framework import serializers
import machines.models as machines
import topologie.models as topologie
from api.serializers import NamespacedHIField, NamespacedHMSerializer, NamespacedHRField
from api.serializers import NamespacedHMSerializer
from machines.api.serializers import Ipv6ListSerializer, VlanSerializer

View file

@ -77,14 +77,15 @@ class PortForm(FormRevMixin, ModelForm):
def __init__(self, *args, **kwargs):
prefix = kwargs.pop("prefix", self.Meta.model.__name__)
super(PortForm, self).__init__(*args, prefix=prefix, **kwargs)
super().__init__(*args, prefix=prefix, **kwargs)
class EditPortForm(FormRevMixin, ModelForm):
"""Form used to edit a switch's port: change in RADIUS or VLANs settings,
assignement to a room, port or machine.
A port is related to either a room, another port (uplink) or a machine (server or AP).
A port is related to either a room, another port (uplink)
or a machine (server or AP).
"""
class Meta(PortForm.Meta):
@ -99,7 +100,7 @@ class EditPortForm(FormRevMixin, ModelForm):
def __init__(self, *args, **kwargs):
prefix = kwargs.pop("prefix", self.Meta.model.__name__)
super(EditPortForm, self).__init__(*args, prefix=prefix, **kwargs)
super().__init__(*args, prefix=prefix, **kwargs)
self.fields["machine_interface"].queryset = (
Interface.objects.all().select_related("domain__extension")
)
@ -127,7 +128,7 @@ class AddPortForm(FormRevMixin, ModelForm):
def __init__(self, *args, **kwargs):
prefix = kwargs.pop("prefix", self.Meta.model.__name__)
super(AddPortForm, self).__init__(*args, prefix=prefix, **kwargs)
super().__init__(*args, prefix=prefix, **kwargs)
self.fields["machine_interface"].queryset = (
Interface.objects.all().select_related("domain__extension")
)
@ -152,7 +153,7 @@ class StackForm(FormRevMixin, ModelForm):
def __init__(self, *args, **kwargs):
prefix = kwargs.pop("prefix", self.Meta.model.__name__)
super(StackForm, self).__init__(*args, prefix=prefix, **kwargs)
super().__init__(*args, prefix=prefix, **kwargs)
class AddAccessPointForm(NewMachineForm):
@ -204,7 +205,7 @@ class EditRoomForm(FormRevMixin, ModelForm):
def __init__(self, *args, **kwargs):
prefix = kwargs.pop("prefix", self.Meta.model.__name__)
super(EditRoomForm, self).__init__(*args, prefix=prefix, **kwargs)
super().__init__(*args, prefix=prefix, **kwargs)
class CreatePortsForm(forms.Form):
@ -229,7 +230,7 @@ class EditModelSwitchForm(FormRevMixin, ModelForm):
def __init__(self, *args, **kwargs):
prefix = kwargs.pop("prefix", self.Meta.model.__name__)
super(EditModelSwitchForm, self).__init__(*args, prefix=prefix, **kwargs)
super().__init__(*args, prefix=prefix, **kwargs)
instance = kwargs.get("instance", None)
if instance:
self.initial["members"] = Switch.objects.filter(model=instance)
@ -249,7 +250,7 @@ class EditConstructorSwitchForm(FormRevMixin, ModelForm):
def __init__(self, *args, **kwargs):
prefix = kwargs.pop("prefix", self.Meta.model.__name__)
super(EditConstructorSwitchForm, self).__init__(*args, prefix=prefix, **kwargs)
super().__init__(*args, prefix=prefix, **kwargs)
class EditSwitchBayForm(FormRevMixin, ModelForm):
@ -270,7 +271,7 @@ class EditSwitchBayForm(FormRevMixin, ModelForm):
def __init__(self, *args, **kwargs):
prefix = kwargs.pop("prefix", self.Meta.model.__name__)
super(EditSwitchBayForm, self).__init__(*args, prefix=prefix, **kwargs)
super().__init__(*args, prefix=prefix, **kwargs)
instance = kwargs.get("instance", None)
if instance:
self.initial["members"] = Switch.objects.filter(switchbay=instance)
@ -290,7 +291,7 @@ class EditBuildingForm(FormRevMixin, ModelForm):
def __init__(self, *args, **kwargs):
prefix = kwargs.pop("prefix", self.Meta.model.__name__)
super(EditBuildingForm, self).__init__(*args, prefix=prefix, **kwargs)
super().__init__(*args, prefix=prefix, **kwargs)
class EditDormitoryForm(FormRevMixin, ModelForm):
@ -302,7 +303,7 @@ class EditDormitoryForm(FormRevMixin, ModelForm):
def __init__(self, *args, **kwargs):
prefix = kwargs.pop("prefix", self.Meta.model.__name__)
super(EditDormitoryForm, self).__init__(*args, prefix=prefix, **kwargs)
super().__init__(*args, prefix=prefix, **kwargs)
class EditPortProfileForm(FormRevMixin, ModelForm):
@ -320,7 +321,7 @@ class EditPortProfileForm(FormRevMixin, ModelForm):
def __init__(self, *args, **kwargs):
prefix = kwargs.pop("prefix", self.Meta.model.__name__)
super(EditPortProfileForm, self).__init__(*args, prefix=prefix, **kwargs)
super().__init__(*args, prefix=prefix, **kwargs)
class EditModuleForm(FormRevMixin, ModelForm):
@ -332,7 +333,7 @@ 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)
super().__init__(*args, prefix=prefix, **kwargs)
class EditSwitchModuleForm(FormRevMixin, ModelForm):
@ -344,4 +345,4 @@ class EditSwitchModuleForm(FormRevMixin, ModelForm):
def __init__(self, *args, **kwargs):
prefix = kwargs.pop("prefix", self.Meta.model.__name__)
super(EditSwitchModuleForm, self).__init__(*args, prefix=prefix, **kwargs)
super().__init__(*args, prefix=prefix, **kwargs)

View file

@ -34,11 +34,9 @@ The following models are defined:
from __future__ import unicode_literals
import itertools
from django.core.cache import cache
from django.core.exceptions import ValidationError
from django.db import IntegrityError, models, transaction
from django.db import models, transaction
from django.db.models.signals import post_delete, post_save
from django.dispatch import receiver
from django.utils.functional import cached_property
@ -78,7 +76,7 @@ class Stack(AclMixin, RevMixin, models.Model):
self.clean()
if not self.name:
self.name = self.stack_id
super(Stack, self).save(*args, **kwargs)
super().save(*args, **kwargs)
def clean(self):
"""Check if id_max < id_min."""
@ -143,7 +141,8 @@ class AccessPoint(Machine):
# We want to retrieve the default behaviour given by AclMixin rather
# than the one overwritten by Machine. If you are not familiar with
# the behaviour of `super`, please check https://docs.python.org/3/library/functions.html#super
# the behaviour of `super`, please check
# https://docs.python.org/3/library/functions.html#super
@classmethod
def get_instance(cls, *args, **kwargs):
@ -208,7 +207,8 @@ class Server(Machine):
# We want to retrieve the default behaviour given by AclMixin rather
# than the one overwritten by Machine. If you are not familiar with
# the behaviour of `super`, please check https://docs.python.org/3/library/functions.html#super
# the behaviour of `super`, please check
# https://docs.python.org/3/library/functions.html#super
@classmethod
def get_instance(cls, *args, **kwargs):
@ -285,7 +285,7 @@ class Switch(Machine):
"""Check if the stack member ID is in the range of the stack's IDs and
calls the clean of the parent class.
"""
super(Switch, self).clean()
super().clean()
if self.stack is not None:
if self.stack_member_id is not None:
if (self.stack_member_id > self.stack.member_id_max) or (
@ -356,7 +356,6 @@ class Switch(Machine):
"""
if self.get_radius_key:
return self.get_radius_key.radius_key
else:
return None
@cached_property
@ -409,7 +408,6 @@ class Switch(Machine):
"id": self.get_management_cred.management_id,
"pass": self.get_management_cred.management_pass,
}
else:
return None
@cached_property
@ -427,9 +425,8 @@ class Switch(Machine):
)
if sw_management_ssl:
return "ssl"
elif sw_management:
if sw_management:
return "plain"
else:
return self.automatic_provision
@cached_property
@ -487,7 +484,6 @@ class Switch(Machine):
"""Get the dormitory in which the switch is located."""
if self.switchbay:
return self.switchbay.building.dormitory
else:
return None
@classmethod
@ -540,7 +536,8 @@ class Switch(Machine):
# We want to retrieve the default behaviour given by AclMixin rather
# than the one overwritten by Machine. If you are not familiar with
# the behaviour of `super`, please check https://docs.python.org/3/library/functions.html#super
# the behaviour of `super`, please check
# https://docs.python.org/3/library/functions.html#super
@classmethod
def get_instance(cls, *args, **kwargs):
@ -592,7 +589,6 @@ class ModelSwitch(AclMixin, RevMixin, models.Model):
def __str__(self):
if self.commercial_name:
return str(self.constructor) + " " + str(self.commercial_name)
else:
return str(self.constructor) + " " + self.reference
@ -711,7 +707,6 @@ class Dormitory(AclMixin, RevMixin, models.Model):
multiple_dorms = cache.get("multiple_dorms")
if multiple_dorms:
return multiple_dorms
else:
return cache.get_or_set("multiple_dorms", cls.objects.count() > 1)
@classmethod
@ -752,7 +747,6 @@ class Building(AclMixin, RevMixin, models.Model):
def get_name(self):
if Dormitory.is_multiple_dorms():
return self.dormitory.name + " : " + self.name
else:
return self.name
@classmethod
@ -835,11 +829,10 @@ class Port(AclMixin, RevMixin, models.Model):
"""More elaborated name for label on switch configuration."""
if self.related:
return _("Uplink: ") + self.related.switch.short_name
elif self.machine_interface:
if self.machine_interface:
return _("Machine: ") + str(self.machine_interface.domain)
elif self.room:
if self.room:
return _("Room: ") + str(self.room)
else:
return _("Unknown")
@cached_property
@ -853,16 +846,14 @@ class Port(AclMixin, RevMixin, models.Model):
"""
if self.custom_profile:
return self.custom_profile
elif self.related:
if self.related:
return self.switch.default_uplink_profile
elif self.machine_interface:
if self.machine_interface:
if hasattr(self.machine_interface.machine, "accesspoint"):
return self.switch.default_access_point_profile
else:
return self.switch.default_asso_machine_profile
elif self.room:
if self.room:
return self.switch.default_room_profile
else:
return Switch.nothing_profile()
@classmethod
@ -922,7 +913,6 @@ class Port(AclMixin, RevMixin, models.Model):
" before creating the relation."
)
)
else:
self.make_port_related()
elif hasattr(self, "related_port"):
self.clean_port_related()
@ -1117,7 +1107,7 @@ class PortProfile(AclMixin, RevMixin, models.Model):
def clean(self):
"""Check that there is only one generic profile default."""
super(PortProfile, self).clean()
super().clean()
if (
self.profil_default
and not self.on_dormitory
@ -1129,7 +1119,8 @@ class PortProfile(AclMixin, RevMixin, models.Model):
raise ValidationError(
{
"profil_default": _(
"A default profile for all dormitories of that type already exists."
"A default profile for all dormitories of that type "
"already exists."
)
}
)

View file

@ -1438,8 +1438,10 @@ def recursive_switchs(switch_start, switch_before, detected):
Args:
switch_start: the switch to begin the visit on.
switch_before: the switch that you come from. None if switch_start is the first one.
detected: list of all switches already visited. None if switch_start is the first one.
switch_before: the switch that you come from.
None if switch_start is the first one.
detected: list of all switches already visited.
None if switch_start is the first one.
Returns:
A list of all the links found and a list of all the switches visited.

View file

@ -42,9 +42,11 @@ from .models import Building, Dormitory, Port, PortProfile, Room, Switch, Switch
class RoomAutocomplete(AutocompleteLoggedOutViewMixin):
obj_type = Room
# Precision on search to add annotations so search behaves more like users expect it to
# Precision on search to add annotations so search behaves more like
# users expect it to
def filter_results(self):
# Suppose we have a dorm named Dorm, a building named B, and rooms from 001 - 999
# Suppose we have a dorm named Dorm, a building named B,
# and rooms from 001 - 999
# Comments explain what we try to match
self.query_set = self.query_set.annotate(
full_name=Concat(
@ -70,6 +72,7 @@ class RoomAutocomplete(AutocompleteLoggedOutViewMixin):
), # Match "Dorm : B 001" (see Room's full_name property)
).all()
# pylint: disable=unsupported-binary-operation
if self.q:
self.query_set = self.query_set.filter(
Q(full_name__icontains=self.q)
@ -109,7 +112,8 @@ class PortAutocomplete(AutocompleteViewMixin):
def filter_results(self):
# We want to enter the switch name, not just the port number
# Because we're concatenating a CharField and an Integer, we have to specify the output_field
# Because we're concatenating a CharField and an Integer,
# Zwe have to specify the output_field
self.query_set = self.query_set.annotate(
full_name=Concat(
"switch__name", Value(" "), "port", output_field=CharField()
@ -121,6 +125,7 @@ class PortAutocomplete(AutocompleteViewMixin):
).all()
if self.q:
# pylint: disable=unsupported-binary-operation
self.query_set = self.query_set.filter(
Q(full_name__icontains=self.q)
| Q(full_name_stuck__icontains=self.q)
@ -153,6 +158,7 @@ class SwitchBayAutocomplete(AutocompleteViewMixin):
).all()
if self.q:
# pylint: disable=unsupported-binary-operation
self.query_set = self.query_set.filter(
Q(full_name__icontains=self.q)
| Q(dorm_name__icontains=self.q)