2016-07-02 22:27:22 +00:00
from django . db import models
2016-07-06 00:03:52 +00:00
from django . forms import ModelForm , Form , ValidationError
2016-07-03 14:49:29 +00:00
from macaddress . fields import MACAddressField
2016-07-02 22:27:22 +00:00
2016-07-06 00:03:52 +00:00
from django . conf import settings
import re
def full_domain_validator ( hostname ) :
""" Validation du nom de domaine, extensions dans settings, prefixe pas plus long que 63 caractères """
HOSTNAME_LABEL_PATTERN = re . compile ( " (?!-)[A-Z \ d-]+(?<!-)$ " , re . IGNORECASE )
if not any ( ext in hostname for ext in settings . ALLOWED_EXTENSIONS ) :
raise ValidationError (
" , le nom de domaine ' %(label)s ' doit comporter une extension valide " ,
params = { ' label ' : hostname } ,
)
for extension in settings . ALLOWED_EXTENSIONS :
if hostname . endswith ( extension ) :
hostname = re . sub ( ' %s $ ' % extension , ' ' , hostname )
break
if len ( hostname ) > 63 :
raise ValidationError (
" , le nom de domaine ' %(label)s ' est trop long (maximum de 63 caractères). " ,
params = { ' label ' : hostname } ,
)
if not HOSTNAME_LABEL_PATTERN . match ( hostname ) :
raise ValidationError (
" , ce nom de domaine ' %(label)s ' contient des carractères interdits. " ,
params = { ' label ' : hostname } ,
)
2016-07-02 22:27:22 +00:00
class Machine ( models . Model ) :
user = models . ForeignKey ( ' users.User ' , on_delete = models . PROTECT )
type = models . ForeignKey ( ' MachineType ' , 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 )
def __str__ ( self ) :
return self . type
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-03 14:49:29 +00:00
machine = models . ForeignKey ( ' Machine ' , on_delete = models . PROTECT )
2016-07-04 00:48:24 +00:00
details = models . CharField ( max_length = 255 , blank = True )
2016-07-06 00:03:52 +00:00
dns = models . CharField ( help_text = " Obligatoire et unique, doit se terminer en %s et ne pas comporter d ' autres points " % " , " . join ( settings . ALLOWED_EXTENSIONS ) , max_length = 255 , unique = True , validators = [ full_domain_validator ] )
2016-07-03 01:12:41 +00:00
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
2016-07-06 10:02:49 +00:00
def clean ( self ) :
self . dns = self . dns . lower ( )
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
class EditMachineForm ( ModelForm ) :
class Meta :
model = Machine
fields = ' __all__ '
def __init__ ( self , * args , * * kwargs ) :
super ( EditMachineForm , self ) . __init__ ( * args , * * kwargs )
2016-07-04 01:08:53 +00:00
self . fields [ ' name ' ] . label = ' Nom de la machine '
2016-07-04 00:48:24 +00:00
self . fields [ ' type ' ] . label = ' Type de machine '
2016-07-05 09:33:27 +00:00
self . fields [ ' type ' ] . empty_label = " Séléctionner un type de machine "
2016-07-04 00:48:24 +00:00
class NewMachineForm ( EditMachineForm ) :
class Meta ( EditMachineForm . Meta ) :
fields = [ ' type ' , ' name ' ]
class EditInterfaceForm ( ModelForm ) :
class Meta :
model = Interface
fields = ' __all__ '
def __init__ ( self , * args , * * kwargs ) :
super ( EditInterfaceForm , self ) . __init__ ( * args , * * kwargs )
2016-07-04 01:08:53 +00:00
self . fields [ ' dns ' ] . label = ' Nom dns de la machine '
2016-07-04 00:48:24 +00:00
self . fields [ ' mac_address ' ] . label = ' Adresse mac '
class AddInterfaceForm ( EditInterfaceForm ) :
class Meta ( EditInterfaceForm . Meta ) :
fields = [ ' ipv4 ' , ' mac_address ' , ' dns ' , ' details ' ]
class NewInterfaceForm ( EditInterfaceForm ) :
class Meta ( EditInterfaceForm . Meta ) :
fields = [ ' mac_address ' , ' dns ' , ' details ' ]