8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-26 06:32:26 +00:00
re2o/machines/models.py

78 lines
2.9 KiB
Python
Raw Normal View History

2016-07-02 22:27:22 +00:00
from django.db import models
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-06 00:03:52 +00:00
import re
2016-07-08 15:54:06 +00:00
def full_domain_validator(interface):
""" Validation du nom de domaine, extensions dans type de machine, prefixe pas plus long que 63 caractères """
2016-07-06 00:03:52 +00:00
HOSTNAME_LABEL_PATTERN = re.compile("(?!-)[A-Z\d-]+(?<!-)$", re.IGNORECASE)
2016-07-08 15:54:06 +00:00
dns = interface.dns.lower()
allowed_extension = interface.machine.type.extension.name
if not dns.endswith(allowed_extension):
2016-07-06 00:03:52 +00:00
raise ValidationError(
2016-07-08 15:54:06 +00:00
"Le nom de domaine '%(label)s' doit comporter une extension valide en '%(extension)s'",
params={'label': dns, 'extension': allowed_extension},
2016-07-06 00:03:52 +00:00
)
2016-07-08 15:54:06 +00:00
dns_short=re.sub('%s$' % allowed_extension, '', dns)
if len(dns_short) > 63:
2016-07-06 00:03:52 +00:00
raise ValidationError(
2016-07-08 15:54:06 +00:00
"Le nom de domaine '%(label)s' est trop long (maximum de 63 caractères).",
params={'label': dns},
2016-07-06 00:03:52 +00:00
)
2016-07-08 15:54:06 +00:00
if not HOSTNAME_LABEL_PATTERN.match(dns_short):
2016-07-06 00:03:52 +00:00
raise ValidationError(
2016-07-08 15:54:06 +00:00
"Ce nom de domaine '%(label)s' contient des carractères interdits.",
params={'label': dns},
2016-07-06 00:03:52 +00:00
)
2016-07-08 15:54:06 +00:00
return dns
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)
active = models.BooleanField(default=True)
2016-07-02 22:27:22 +00:00
def is_active(self):
""" Renvoie si une interface doit avoir accès ou non """
machine = self.machine
user = machine.user
return machine.active and user.has_access()
2016-07-02 22:27:22 +00:00
def __str__(self):
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)
#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)
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-03 14:49:29 +00:00
def __str__(self):
return self.dns
2016-07-03 14:49:29 +00:00
def clean(self):
2016-07-08 15:54:06 +00:00
self.dns=full_domain_validator(self)
2016-07-03 14:49:29 +00:00
class IpList(models.Model):
ipv4 = models.GenericIPAddressField(protocol='IPv4', unique=True)
2016-07-03 14:49:29 +00:00
def __str__(self):
return self.ipv4