models.py 2.93 KB
Newer Older
Dalahro's avatar
Dalahro committed
1
from django.db import models
chirac's avatar
chirac committed
2
from django.forms import ValidationError
chirac's avatar
chirac committed
3
from macaddress.fields import MACAddressField
Dalahro's avatar
Dalahro committed
4

chirac's avatar
chirac committed
5 6
import re

7 8
def full_domain_validator(interface):
    """ Validation du nom de domaine, extensions dans type de machine, prefixe pas plus long que 63 caractères """
chirac's avatar
chirac committed
9
    HOSTNAME_LABEL_PATTERN = re.compile("(?!-)[A-Z\d-]+(?<!-)$", re.IGNORECASE)
10 11 12
    dns = interface.dns.lower()
    allowed_extension = interface.machine.type.extension.name
    if not dns.endswith(allowed_extension):
chirac's avatar
chirac committed
13
        raise ValidationError(
14 15
                "Le nom de domaine '%(label)s' doit comporter une extension valide en '%(extension)s'",
                params={'label': dns, 'extension': allowed_extension},
chirac's avatar
chirac committed
16
        )
17 18
    dns_short=re.sub('%s$' % allowed_extension, '', dns) 
    if len(dns_short) > 63:
chirac's avatar
chirac committed
19
        raise ValidationError(
20 21
                "Le nom de domaine '%(label)s' est trop long (maximum de 63 caractères).",
                params={'label': dns},
chirac's avatar
chirac committed
22
        )
23
    if not HOSTNAME_LABEL_PATTERN.match(dns_short):
chirac's avatar
chirac committed
24
        raise ValidationError(
25 26
                "Ce nom de domaine '%(label)s' contient des carractères interdits.",
                params={'label': dns},
chirac's avatar
chirac committed
27
        )
28
    return dns 
Dalahro's avatar
Dalahro committed
29 30 31 32

class Machine(models.Model):
    user = models.ForeignKey('users.User', on_delete=models.PROTECT)
    type = models.ForeignKey('MachineType', on_delete=models.PROTECT)
chirac's avatar
chirac committed
33
    name = models.CharField(max_length=255, help_text="Optionnel", blank=True, null=True)
34
    active = models.BooleanField(default=True)
Dalahro's avatar
Dalahro committed
35

36 37 38 39 40 41
    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()

Dalahro's avatar
Dalahro committed
42
    def __str__(self):
43
        return str(self.user) + ' - ' + str(self.id) + ' - ' +  str(self.name)
Dalahro's avatar
Dalahro committed
44 45 46

class MachineType(models.Model):
    type = models.CharField(max_length=255)
47
    extension = models.ForeignKey('Extension', on_delete=models.PROTECT)
Dalahro's avatar
Dalahro committed
48 49 50

    def __str__(self):
        return self.type
chirac's avatar
chirac committed
51

52 53 54 55 56
class Extension(models.Model):
    name = models.CharField(max_length=255)

    def __str__(self):
        return self.name
chirac's avatar
chirac committed
57

chirac's avatar
chirac committed
58 59
class Interface(models.Model):
    ipv4 = models.OneToOneField('IpList', on_delete=models.PROTECT, blank=True, null=True)
60 61
    #ipv6 = models.GenericIPAddressField(protocol='IPv6', null=True)
    mac_address = MACAddressField(integer=False, unique=True)
chirac's avatar
chirac committed
62
    machine = models.ForeignKey('Machine', on_delete=models.PROTECT)
63
    details = models.CharField(max_length=255, blank=True)
64
    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)
chirac's avatar
chirac committed
65

chirac's avatar
chirac committed
66
    def __str__(self):
67
        return self.dns
chirac's avatar
chirac committed
68

69
    def clean(self):
70
        self.dns=full_domain_validator(self)
71

chirac's avatar
chirac committed
72
class IpList(models.Model):
73
    ipv4 = models.GenericIPAddressField(protocol='IPv4', unique=True)
chirac's avatar
chirac committed
74 75

    def __str__(self):
76 77
        return self.ipv4