Commit eefa0b4a authored by Levy--Falk Hugo's avatar Levy--Falk Hugo

PEP8 mon amour + typos

parent a07e0d92
...@@ -355,7 +355,7 @@ def decide_vlan_and_register_switch(nas_machine, nas_type, port_number, ...@@ -355,7 +355,7 @@ def decide_vlan_and_register_switch(nas_machine, nas_type, port_number,
port=port_number port=port_number
) )
.first()) .first())
# Si le port est inconnu, on place sur le vlan defaut # Si le port est inconnu, on place sur le vlan defaut
# Aucune information particulière ne permet de déterminer quelle # Aucune information particulière ne permet de déterminer quelle
# politique à appliquer sur ce port # politique à appliquer sur ce port
...@@ -363,12 +363,12 @@ def decide_vlan_and_register_switch(nas_machine, nas_type, port_number, ...@@ -363,12 +363,12 @@ def decide_vlan_and_register_switch(nas_machine, nas_type, port_number,
return (sw_name, "Chambre inconnue", u'Port inconnu', VLAN_OK) return (sw_name, "Chambre inconnue", u'Port inconnu', VLAN_OK)
# On récupère le profil du port # On récupère le profil du port
port_profil = port.get_port_profil port_profile = port.get_port_profile
# Si un vlan a été précisé dans la config du port, # Si un vlan a été précisé dans la config du port,
# on l'utilise pour VLAN_OK # on l'utilise pour VLAN_OK
if port_profil.vlan_untagged: if port_profile.vlan_untagged:
DECISION_VLAN = int(port_profil.vlan_untagged.vlan_id) DECISION_VLAN = int(port_profile.vlan_untagged.vlan_id)
extra_log = u"Force sur vlan " + str(DECISION_VLAN) extra_log = u"Force sur vlan " + str(DECISION_VLAN)
else: else:
DECISION_VLAN = VLAN_OK DECISION_VLAN = VLAN_OK
...@@ -378,7 +378,7 @@ def decide_vlan_and_register_switch(nas_machine, nas_type, port_number, ...@@ -378,7 +378,7 @@ def decide_vlan_and_register_switch(nas_machine, nas_type, port_number,
return (sw_name, port.room, u'Port desactivé', VLAN_NOK) return (sw_name, port.room, u'Port desactivé', VLAN_NOK)
# Si radius est désactivé, on laisse passer # Si radius est désactivé, on laisse passer
if port_profil.radius_type == 'NO': if port_profile.radius_type == 'NO':
return (sw_name, return (sw_name,
"", "",
u"Pas d'authentification sur ce port" + extra_log, u"Pas d'authentification sur ce port" + extra_log,
...@@ -386,7 +386,7 @@ def decide_vlan_and_register_switch(nas_machine, nas_type, port_number, ...@@ -386,7 +386,7 @@ def decide_vlan_and_register_switch(nas_machine, nas_type, port_number,
# Si le 802.1X est activé sur ce port, cela veut dire que la personne a été accept précédemment # Si le 802.1X est activé sur ce port, cela veut dire que la personne a été accept précédemment
# Par conséquent, on laisse passer sur le bon vlan # Par conséquent, on laisse passer sur le bon vlan
if nas_type.port_access_mode == '802.1X' and port_profil.radius_type == '802.1X': if nas_type.port_access_mode == '802.1X' and port_profile.radius_type == '802.1X':
room = port.room or "Chambre/local inconnu" room = port.room or "Chambre/local inconnu"
return (sw_name, room, u'Acceptation authentification 802.1X', DECISION_VLAN) return (sw_name, room, u'Acceptation authentification 802.1X', DECISION_VLAN)
...@@ -395,7 +395,7 @@ def decide_vlan_and_register_switch(nas_machine, nas_type, port_number, ...@@ -395,7 +395,7 @@ def decide_vlan_and_register_switch(nas_machine, nas_type, port_number,
# rattachés à ce port sont bien à jour de cotisation. Sinon on rejette (anti squattage) # rattachés à ce port sont bien à jour de cotisation. Sinon on rejette (anti squattage)
# Il n'est pas possible de se connecter sur une prise strict sans adhérent à jour de cotis # Il n'est pas possible de se connecter sur une prise strict sans adhérent à jour de cotis
# dedans # dedans
if port_profil.radius_mode == 'STRICT': if port_profile.radius_mode == 'STRICT':
room = port.room room = port.room
if not room: if not room:
return (sw_name, "Inconnue", u'Chambre inconnue', VLAN_NOK) return (sw_name, "Inconnue", u'Chambre inconnue', VLAN_NOK)
...@@ -411,7 +411,7 @@ def decide_vlan_and_register_switch(nas_machine, nas_type, port_number, ...@@ -411,7 +411,7 @@ def decide_vlan_and_register_switch(nas_machine, nas_type, port_number,
# else: user OK, on passe à la verif MAC # else: user OK, on passe à la verif MAC
# Si on fait de l'auth par mac, on cherche l'interface via sa mac dans la bdd # Si on fait de l'auth par mac, on cherche l'interface via sa mac dans la bdd
if port_profil.radius_mode == 'COMMON' or port_profil.radius_mode == 'STRICT': if port_profile.radius_mode == 'COMMON' or port_profile.radius_mode == 'STRICT':
# Authentification par mac # Authentification par mac
interface = (Interface.objects interface = (Interface.objects
.filter(mac_address=mac_address) .filter(mac_address=mac_address)
......
...@@ -40,7 +40,7 @@ from __future__ import unicode_literals ...@@ -40,7 +40,7 @@ from __future__ import unicode_literals
import itertools import itertools
from django.db import models from django.db import models
from django.db.models.signals import pre_save, post_save, post_delete from django.db.models.signals import post_save, post_delete
from django.utils.functional import cached_property from django.utils.functional import cached_property
from django.dispatch import receiver from django.dispatch import receiver
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
...@@ -52,11 +52,6 @@ from reversion import revisions as reversion ...@@ -52,11 +52,6 @@ from reversion import revisions as reversion
from machines.models import Machine, regen from machines.models import Machine, regen
from re2o.mixins import AclMixin, RevMixin from re2o.mixins import AclMixin, RevMixin
from os.path import isfile
from os import remove
class Stack(AclMixin, RevMixin, models.Model): class Stack(AclMixin, RevMixin, models.Model):
"""Un objet stack. Regrouppe des switchs en foreign key """Un objet stack. Regrouppe des switchs en foreign key
...@@ -123,7 +118,10 @@ class AccessPoint(AclMixin, Machine): ...@@ -123,7 +118,10 @@ class AccessPoint(AclMixin, Machine):
) )
def building(self): def building(self):
"""Return the building of the AP/Server (building of the switchs connected to...)""" """
Return the building of the AP/Server (building of the switchs
connected to...)
"""
return Building.objects.filter( return Building.objects.filter(
switchbay__switch=self.switch() switchbay__switch=self.switch()
) )
...@@ -135,14 +133,18 @@ class AccessPoint(AclMixin, Machine): ...@@ -135,14 +133,18 @@ class AccessPoint(AclMixin, Machine):
@classmethod @classmethod
def all_ap_in(cls, building_instance): def all_ap_in(cls, building_instance):
"""Get a building as argument, returns all ap of a building""" """Get a building as argument, returns all ap of a building"""
return cls.objects.filter(interface__port__switch__switchbay__building=building_instance) return cls.objects.filter(
interface__port__switch__switchbay__building=building_instance
)
def __str__(self): def __str__(self):
return str(self.interface_set.first()) return str(self.interface_set.first())
class Server(Machine): class Server(Machine):
"""Dummy class, to retrieve servers of a building, or get switch of a server""" """
Dummy class, to retrieve servers of a building, or get switch of a server
"""
class Meta: class Meta:
proxy = True proxy = True
...@@ -160,7 +162,10 @@ class Server(Machine): ...@@ -160,7 +162,10 @@ class Server(Machine):
) )
def building(self): def building(self):
"""Return the building of the AP/Server (building of the switchs connected to...)""" """
Return the building of the AP/Server
(building of the switchs connected to...)
"""
return Building.objects.filter( return Building.objects.filter(
switchbay__switch=self.switch() switchbay__switch=self.switch()
) )
...@@ -172,7 +177,9 @@ class Server(Machine): ...@@ -172,7 +177,9 @@ class Server(Machine):
@classmethod @classmethod
def all_server_in(cls, building_instance): def all_server_in(cls, building_instance):
"""Get a building as argument, returns all server of a building""" """Get a building as argument, returns all server of a building"""
return cls.objects.filter(interface__port__switch__switchbay__building=building_instance).exclude(accesspoint__isnull=False) return cls.objects.filter(
interface__port__switch__switchbay__building=building_instance
).exclude(accesspoint__isnull=False)
def __str__(self): def __str__(self):
return str(self.interface_set.first()) return str(self.interface_set.first())
...@@ -200,7 +207,7 @@ class Switch(AclMixin, Machine): ...@@ -200,7 +207,7 @@ class Switch(AclMixin, Machine):
blank=True, blank=True,
null=True, null=True,
on_delete=models.SET_NULL on_delete=models.SET_NULL
) )
stack_member_id = models.PositiveIntegerField( stack_member_id = models.PositiveIntegerField(
blank=True, blank=True,
null=True, null=True,
...@@ -238,7 +245,7 @@ class Switch(AclMixin, Machine): ...@@ -238,7 +245,7 @@ class Switch(AclMixin, Machine):
raise ValidationError( raise ValidationError(
{'stack_member_id': "L'id de ce switch est en\ {'stack_member_id': "L'id de ce switch est en\
dehors des bornes permises pas la stack"} dehors des bornes permises pas la stack"}
) )
else: else:
raise ValidationError({'stack_member_id': "L'id dans la stack\ raise ValidationError({'stack_member_id': "L'id dans la stack\
ne peut être nul"}) ne peut être nul"})
...@@ -378,25 +385,25 @@ class Port(AclMixin, RevMixin, models.Model): ...@@ -378,25 +385,25 @@ class Port(AclMixin, RevMixin, models.Model):
on_delete=models.PROTECT, on_delete=models.PROTECT,
blank=True, blank=True,
null=True null=True
) )
machine_interface = models.ForeignKey( machine_interface = models.ForeignKey(
'machines.Interface', 'machines.Interface',
on_delete=models.SET_NULL, on_delete=models.SET_NULL,
blank=True, blank=True,
null=True null=True
) )
related = models.OneToOneField( related = models.OneToOneField(
'self', 'self',
null=True, null=True,
blank=True, blank=True,
related_name='related_port' related_name='related_port'
) )
custom_profile = models.ForeignKey( custom_profile = models.ForeignKey(
'PortProfile', 'PortProfile',
on_delete=models.PROTECT, on_delete=models.PROTECT,
blank=True, blank=True,
null=True null=True
) )
state = models.BooleanField( state = models.BooleanField(
default=True, default=True,
help_text='Port state Active', help_text='Port state Active',
...@@ -411,32 +418,35 @@ class Port(AclMixin, RevMixin, models.Model): ...@@ -411,32 +418,35 @@ class Port(AclMixin, RevMixin, models.Model):
) )
@cached_property @cached_property
def get_port_profil(self): def get_port_profile(self):
"""Return the config profil for this port """Return the config profile for this port
:returns: the profile of self (port)""" :returns: the profile of self (port)"""
def profil_or_nothing(profil): def profile_or_nothing(profile):
port_profil = PortProfile.objects.filter(profil_default=profil).first() port_profile = PortProfile.objects.filter(
if port_profil: profile_default=profile).first()
return port_profil if port_profile:
return port_profile
else: else:
nothing = PortProfile.objects.filter(profil_default='nothing').first() nothing_profile, _created = PortProfile.objects.get_or_create(
if not nothing: profile_default='nothing',
nothing = PortProfile.objects.create(profil_default='nothing', name='nothing', radius_type='NO') name='nothing',
return nothing radius_type='NO'
)
return nothing_profile
if self.custom_profile: if self.custom_profile:
return self.custom_profile return self.custom_profile
elif self.related: elif self.related:
return profil_or_nothing('uplink') return profile_or_nothing('uplink')
elif self.machine_interface: elif self.machine_interface:
if hasattr(self.machine_interface.machine, 'accesspoint'): if hasattr(self.machine_interface.machine, 'accesspoint'):
return profil_or_nothing('access_point') return profile_or_nothing('access_point')
else: else:
return profil_or_nothing('asso_machine') return profile_or_nothing('asso_machine')
elif self.room: elif self.room:
return profil_or_nothing('room') return profile_or_nothing('room')
else: else:
return profil_or_nothing('nothing') return profile_or_nothing('nothing')
@classmethod @classmethod
def get_instance(cls, portid, *_args, **kwargs): def get_instance(cls, portid, *_args, **kwargs):
...@@ -521,11 +531,11 @@ class PortProfile(AclMixin, RevMixin, models.Model): ...@@ -521,11 +531,11 @@ class PortProfile(AclMixin, RevMixin, models.Model):
('NO', 'NO'), ('NO', 'NO'),
('802.1X', '802.1X'), ('802.1X', '802.1X'),
('MAC-radius', 'MAC-radius'), ('MAC-radius', 'MAC-radius'),
) )
MODES = ( MODES = (
('STRICT', 'STRICT'), ('STRICT', 'STRICT'),
('COMMON', 'COMMON'), ('COMMON', 'COMMON'),
) )
SPEED = ( SPEED = (
('10-half', '10-half'), ('10-half', '10-half'),
('100-half', '100-half'), ('100-half', '100-half'),
...@@ -535,14 +545,14 @@ class PortProfile(AclMixin, RevMixin, models.Model): ...@@ -535,14 +545,14 @@ class PortProfile(AclMixin, RevMixin, models.Model):
('auto', 'auto'), ('auto', 'auto'),
('auto-10', 'auto-10'), ('auto-10', 'auto-10'),
('auto-100', 'auto-100'), ('auto-100', 'auto-100'),
) )
PROFIL_DEFAULT= ( PROFIL_DEFAULT = (
('room', 'room'), ('room', 'room'),
('accespoint', 'accesspoint'), ('accespoint', 'accesspoint'),
('uplink', 'uplink'), ('uplink', 'uplink'),
('asso_machine', 'asso_machine'), ('asso_machine', 'asso_machine'),
('nothing', 'nothing'), ('nothing', 'nothing'),
) )
name = models.CharField(max_length=255, verbose_name=_("Name")) name = models.CharField(max_length=255, verbose_name=_("Name"))
profil_default = models.CharField( profil_default = models.CharField(
max_length=32, max_length=32,
...@@ -616,25 +626,36 @@ class PortProfile(AclMixin, RevMixin, models.Model): ...@@ -616,25 +626,36 @@ class PortProfile(AclMixin, RevMixin, models.Model):
default=False, default=False,
help_text='Protect against rogue ra', help_text='Protect against rogue ra',
verbose_name=_("Ra guard") verbose_name=_("Ra guard")
) )
loop_protect = models.BooleanField( loop_protect = models.BooleanField(
default=False, default=False,
help_text='Protect again loop', help_text='Protect again loop',
verbose_name=_("Loop Protect") verbose_name=_("Loop Protect")
) )
class Meta: class Meta:
permissions = ( permissions = (
("view_port_profile", _("Can view a port profile object")), ("view_port_profile", _("Can view a port profile object")),
) )
verbose_name = _("Port profile") verbose_name = _("Port profile")
verbose_name_plural = _("Port profiles") verbose_name_plural = _("Port profiles")
security_parameters_fields = ['loop_protect', 'ra_guard', 'arp_protect', 'dhcpv6_snooping', 'dhcp_snooping', 'flow_control'] security_parameters_fields = [
'loop_protect',
'ra_guard',
'arp_protect',
'dhcpv6_snooping',
'dhcp_snooping',
'flow_control'
]
@cached_property @cached_property
def security_parameters_enabled(self): def security_parameters_enabled(self):
return [parameter for parameter in self.security_parameters_fields if getattr(self, parameter)] return [
parameter
for parameter in self.security_parameters_fields
if getattr(self, parameter)
]
@cached_property @cached_property
def security_parameters_as_str(self): def security_parameters_as_str(self):
...@@ -650,45 +671,55 @@ def ap_post_save(**_kwargs): ...@@ -650,45 +671,55 @@ def ap_post_save(**_kwargs):
regen('unifi-ap-names') regen('unifi-ap-names')
regen("graph_topo") regen("graph_topo")
@receiver(post_delete, sender=AccessPoint) @receiver(post_delete, sender=AccessPoint)
def ap_post_delete(**_kwargs): def ap_post_delete(**_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')
regen("graph_topo") regen("graph_topo")
@receiver(post_delete, sender=Stack) @receiver(post_delete, sender=Stack)
def stack_post_delete(**_kwargs): def stack_post_delete(**_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)
@receiver(post_save, sender=Port) @receiver(post_save, sender=Port)
def port_post_save(**_kwargs): def port_post_save(**_kwargs):
regen("graph_topo") regen("graph_topo")
@receiver(post_delete, sender=Port) @receiver(post_delete, sender=Port)
def port_post_delete(**_kwargs): def port_post_delete(**_kwargs):
regen("graph_topo") regen("graph_topo")
@receiver(post_save, sender=ModelSwitch) @receiver(post_save, sender=ModelSwitch)
def modelswitch_post_save(**_kwargs): def modelswitch_post_save(**_kwargs):
regen("graph_topo") regen("graph_topo")
@receiver(post_delete, sender=ModelSwitch) @receiver(post_delete, sender=ModelSwitch)
def modelswitch_post_delete(**_kwargs): def modelswitch_post_delete(**_kwargs):
regen("graph_topo") regen("graph_topo")
@receiver(post_save, sender=Building) @receiver(post_save, sender=Building)
def building_post_save(**_kwargs): def building_post_save(**_kwargs):
regen("graph_topo") regen("graph_topo")
@receiver(post_delete, sender=Building) @receiver(post_delete, sender=Building)
def building_post_delete(**_kwargs): def building_post_delete(**_kwargs):
regen("graph_topo") regen("graph_topo")
@receiver(post_save, sender=Switch) @receiver(post_save, sender=Switch)
def switch_post_save(**_kwargs): def switch_post_save(**_kwargs):
regen("graph_topo") regen("graph_topo")
@receiver(post_delete, sender=Switch) @receiver(post_delete, sender=Switch)
def switch_post_delete(**_kwargs): def switch_post_delete(**_kwargs):
regen("graph_topo") regen("graph_topo")
...@@ -29,9 +29,9 @@ with this program; if not, write to the Free Software Foundation, Inc., ...@@ -29,9 +29,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
{% include "pagination.html" with list=port_profile_list %} {% include "pagination.html" with list=port_profile_list %}
{% endif %} {% endif %}
<thead>
<table class="table table-striped"> <table class="table table-striped">
<thead>
<tr> <tr>
<th>{% trans "Name" %}</th> <th>{% trans "Name" %}</th>
<th>{% trans "Default for" %}</th> <th>{% trans "Default for" %}</th>
...@@ -49,7 +49,7 @@ with this program; if not, write to the Free Software Foundation, Inc., ...@@ -49,7 +49,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
<td>{{port_profile.profil_default}}</td> <td>{{port_profile.profil_default}}</td>
<td> <td>
{% if port_profile.vlan_untagged %} {% if port_profile.vlan_untagged %}
<b>Untagged : </b>{{port_profile.vlan_untagged}} <b>Untagged : </b>{{port_profile.vlan_untagged}}
<br> <br>
{% endif %} {% endif %}
{% if port_profile.vlan_tagged.all %} {% if port_profile.vlan_tagged.all %}
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment