2016-07-02 22:27:22 +00:00
from django . db import models
2016-07-25 21:54:40 +00:00
from django . db . models . signals import post_save , post_delete
from django . dispatch import receiver
2016-07-06 20:49:16 +00:00
from django . forms import ValidationError
2016-07-03 14:49:29 +00:00
from macaddress . fields import MACAddressField
2016-07-02 22:27:22 +00:00
2016-07-25 21:54:40 +00:00
2016-07-02 22:27:22 +00:00
class Machine ( models . Model ) :
user = models . ForeignKey ( ' users.User ' , on_delete = models . PROTECT )
2016-07-04 01:08:53 +00:00
name = models . CharField ( max_length = 255 , help_text = " Optionnel " , blank = True , null = True )
2016-07-05 10:21:43 +00:00
active = models . BooleanField ( default = True )
2016-07-02 22:27:22 +00:00
def __str__ ( self ) :
2016-07-04 00:48:24 +00:00
return str ( self . user ) + ' - ' + str ( self . id ) + ' - ' + str ( self . name )
2016-07-02 22:27:22 +00:00
class MachineType ( models . Model ) :
type = models . CharField ( max_length = 255 )
2016-07-08 15:54:06 +00:00
extension = models . ForeignKey ( ' Extension ' , on_delete = models . PROTECT )
2016-07-02 22:27:22 +00:00
def __str__ ( self ) :
return self . type
2016-07-03 01:12:41 +00:00
2016-07-08 15:54:06 +00:00
class Extension ( models . Model ) :
name = models . CharField ( max_length = 255 )
def __str__ ( self ) :
return self . name
2016-07-03 01:12:41 +00:00
2016-07-03 14:49:29 +00:00
class Interface ( models . Model ) :
ipv4 = models . OneToOneField ( ' IpList ' , on_delete = models . PROTECT , blank = True , null = True )
2016-07-04 00:48:24 +00:00
#ipv6 = models.GenericIPAddressField(protocol='IPv6', null=True)
mac_address = MACAddressField ( integer = False , unique = True )
2016-07-18 03:30:35 +00:00
machine = models . ForeignKey ( ' Machine ' , on_delete = models . CASCADE )
2016-07-18 17:14:48 +00:00
type = models . ForeignKey ( ' MachineType ' , on_delete = models . PROTECT )
2016-07-04 00:48:24 +00:00
details = models . CharField ( max_length = 255 , blank = True )
2016-07-08 15:54:06 +00:00
dns = models . CharField ( help_text = " Obligatoire et unique, doit se terminer en %s et ne pas comporter d ' autres points " % " , " . join ( Extension . objects . values_list ( ' name ' , flat = True ) ) , max_length = 255 , unique = True )
2016-07-03 01:12:41 +00:00
2016-07-18 21:22:59 +00:00
def is_active ( self ) :
""" Renvoie si une interface doit avoir accès ou non """
machine = self . machine
user = self . machine . user
return machine . active and user . has_access ( )
2016-07-25 21:54:40 +00:00
#def save(self, *args, **kwargs):
# user = self.machine.user
# user.ldap_sync(base=False, access_refresh=False, mac_refresh=True)
# super(Interface, self).save(*args, **kwargs)
2016-07-03 14:49:29 +00:00
def __str__ ( self ) :
2016-07-04 00:48:24 +00:00
return self . dns
2016-07-03 14:49:29 +00:00
class IpList ( models . Model ) :
2016-07-04 00:48:24 +00:00
ipv4 = models . GenericIPAddressField ( protocol = ' IPv4 ' , unique = True )
2016-07-03 14:49:29 +00:00
def __str__ ( self ) :
2016-07-04 00:48:24 +00:00
return self . ipv4
2016-07-25 21:54:40 +00:00
@receiver ( post_save , sender = Interface )
def interface_post_save ( sender , * * kwargs ) :
user = kwargs [ ' instance ' ] . machine . user
user . ldap_sync ( base = False , access_refresh = False , mac_refresh = True )
@receiver ( post_delete , sender = Interface )
def interface_post_delete ( sender , * * kwargs ) :
user = kwargs [ ' instance ' ] . machine . user
user . ldap_sync ( base = False , access_refresh = False , mac_refresh = True )