@ -379,6 +379,58 @@ class Switch(AclMixin, Machine):
modules . append ( ( module_of_self . slot , module_of_self . module . reference ) )
return modules
@cached_property
def get_dormitory ( self ) :
""" Returns the dormitory of that switch """
if self . switchbay :
return self . switchbay . building . dormitory
else :
return None
@classmethod
def nothing_profile ( cls ) :
""" Return default nothing port profile """
nothing_profile , _created = PortProfile . objects . get_or_create (
profil_default = ' nothing ' ,
name = ' nothing ' ,
radius_type = ' NO '
)
return nothing_profile
def profile_type_or_nothing ( self , profile_type ) :
""" Return the profile for a profile_type of this switch
If exists , returns the defined default profile for a profile type on the dormitory which
the switch belongs
Otherwise , returns the nothing profile """
profile_queryset = PortProfile . objects . filter ( profil_default = profile_type )
if self . get_dormitory :
port_profile = profile_queryset . filter ( on_dormitory = self . get_dormitory ) . first ( ) or profile_queryset . first ( )
else :
port_profile = profile_queryset . first ( )
return port_profile or Switch . nothing_profile
@cached_property
def default_uplink_profile ( self ) :
""" Default uplink profile for that switch -- in cache """
return self . profile_type_or_nothing ( ' uplink ' )
@cached_property
def default_access_point_profile ( self ) :
""" Default ap profile for that switch -- in cache """
return self . profile_type_or_nothing ( ' access_point ' )
@cached_property
def default_room_profile ( self ) :
""" Default room profile for that switch -- in cache """
return self . profile_type_or_nothing ( ' room ' )
@cached_property
def default_asso_machine_profile ( self ) :
""" Default asso machine profile for that switch -- in cache """
return self . profile_type_or_nothing ( ' asso_machine ' )
def __str__ ( self ) :
return str ( self . get_name )
@ -647,33 +699,26 @@ class Port(AclMixin, RevMixin, models.Model):
@cached_property
def get_port_profile ( self ) :
""" Return the config profil for this port
: returns : the profile of self ( port ) """
def profile_or_nothing ( profile ) :
port_profile = PortProfile . objects . filter (
profil_default = profile ) . first ( )
if port_profile :
return port_profile
else :
nothing_profile , _created = PortProfile . objects . get_or_create (
profil_default = ' nothing ' ,
name = ' nothing ' ,
radius_type = ' NO '
)
return nothing_profile
: returns : the profile of self ( port )
If is defined a custom profile , returns it
elIf a default profile is defined for its dormitory , returns it
Else , returns the global default profil
If not exists , create a nothing profile """
if self . custom_profile :
return self . custom_profile
elif self . related :
return profile_or_nothing ( ' uplink ' )
return self . switch . default_uplink_profile
elif self . machine_interface :
if hasattr ( self . machine_interface . machine , ' accesspoint ' ) :
return profile_or_nothing ( ' access_point ' )
return self . switch . default_access_point_profile
else :
return profile_or_nothing ( ' asso_machine ' )
return self . switch . default_asso_machine_profile
elif self . room :
return profile_or_nothing ( ' room ' )
return self . switch . default_room_profile
else :
return profile_or_nothing ( ' nothing ' )
return Switch . nothing_profile
@classmethod
def get_instance ( cls , portid , * _args , * * kwargs ) :
@ -791,9 +836,16 @@ class PortProfile(AclMixin, RevMixin, models.Model):
choices = PROFIL_DEFAULT ,
blank = True ,
null = True ,
unique = True ,
verbose_name = _ ( " Default profile " )
)
on_dormitory = models . ForeignKey (
' topologie.Dormitory ' ,
related_name = ' dormitory_ofprofil ' ,
on_delete = models . SET_NULL ,
blank = True ,
null = True ,
verbose_name = _ ( " Profil on dormitory " )
)
vlan_untagged = models . ForeignKey (
' machines.Vlan ' ,
related_name = ' vlan_untagged ' ,
@ -871,6 +923,7 @@ class PortProfile(AclMixin, RevMixin, models.Model):
)
verbose_name = _ ( " port profile " )
verbose_name_plural = _ ( " port profiles " )
unique_together = [ ' on_dormitory ' , ' profil_default ' ]
security_parameters_fields = [
' loop_protect ' ,
@ -893,6 +946,14 @@ class PortProfile(AclMixin, RevMixin, models.Model):
def security_parameters_as_str ( self ) :
return ' , ' . join ( self . security_parameters_enabled )
def clean ( self ) :
""" Check that there is only one generic profil default """
super ( PortProfile , self ) . clean ( )
if self . profil_default and not self . on_dormitory and PortProfile . objects . exclude ( id = self . id ) . filter ( profil_default = self . profil_default ) . exclude ( on_dormitory__isnull = False ) . exists ( ) :
raise ValidationError (
{ ' profil_default ' : _ ( " A default profile for all dormitory of that type already exists. " ) }
)
def __str__ ( self ) :
return self . name