8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-24 04:13:12 +00:00
re2o/machines/models.py

141 lines
5.1 KiB
Python
Raw Normal View History

2016-07-02 22:27:22 +00:00
from django.db import models
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
from netaddr import mac_bare, EUI
2016-11-15 14:25:27 +00:00
from django.core.validators import MinValueValidator,MaxValueValidator
2016-07-02 22:27:22 +00:00
from re2o.settings import MAIN_EXTENSION
2016-07-02 22:27:22 +00:00
class Machine(models.Model):
PRETTY_NAME = "Machine"
2016-07-02 22:27:22 +00:00
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)
active = models.BooleanField(default=True)
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):
PRETTY_NAME = "Type de machine"
2016-10-22 22:55:58 +00:00
type = models.CharField(max_length=255)
ip_type = models.ForeignKey('IpType', on_delete=models.PROTECT, blank=True, null=True)
def __str__(self):
return self.type
class IpType(models.Model):
PRETTY_NAME = "Type d'ip"
2016-07-02 22:27:22 +00:00
type = models.CharField(max_length=255)
2016-07-08 15:54:06 +00:00
extension = models.ForeignKey('Extension', on_delete=models.PROTECT)
need_infra = models.BooleanField(default=False)
domaine_ip = models.GenericIPAddressField(protocol='IPv4')
domaine_range = models.IntegerField(validators=[MinValueValidator(8), MaxValueValidator(32)])
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):
PRETTY_NAME = "Extensions dns"
name = models.CharField(max_length=255, unique=True)
need_infra = models.BooleanField(default=False)
origin = models.OneToOneField('IpList', on_delete=models.PROTECT, blank=True, null=True)
2016-07-08 15:54:06 +00:00
def __str__(self):
return self.name
2016-07-03 01:12:41 +00:00
class Mx(models.Model):
PRETTY_NAME = "Enregistrements MX"
zone = models.ForeignKey('Extension', on_delete=models.PROTECT)
priority = models.IntegerField(unique=True)
name = models.OneToOneField('Alias', on_delete=models.PROTECT)
def __str__(self):
return str(self.zone) + ' ' + str(self.priority) + ' ' + str(self.name)
class Ns(models.Model):
PRETTY_NAME = "Enregistrements NS"
zone = models.ForeignKey('Extension', on_delete=models.PROTECT)
interface = models.OneToOneField('Interface', on_delete=models.PROTECT)
def __str__(self):
return str(self.zone) + ' ' + str(self.interface)
2016-07-03 14:49:29 +00:00
class Interface(models.Model):
PRETTY_NAME = "Interface"
2016-07-03 14:49:29 +00:00
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)
machine = models.ForeignKey('Machine', on_delete=models.CASCADE)
2016-07-18 17:14:48 +00:00
type = models.ForeignKey('MachineType', on_delete=models.PROTECT)
details = models.CharField(max_length=255, blank=True)
2016-10-24 17:52:29 +00:00
dns = models.CharField(help_text="Obligatoire et unique, ne doit pas comporter de points", 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()
def mac_bare(self):
return str(EUI(self.mac_address, dialect=mac_bare)).lower()
def clean(self, *args, **kwargs):
self.mac_address = str(EUI(self.mac_address)) or None
2016-11-18 21:20:03 +00:00
if self.ipv4:
alias = Alias.objects.filter(alias=self.dns).filter(extension=self.ipv4.ip_type.extension)
else:
alias = Alias.objects.filter(alias=self.dns)
2016-11-18 16:36:30 +00:00
if alias:
raise ValidationError("Impossible, le dns est déjà utilisé par un alias (%s)" % alias[0])
2016-07-03 14:49:29 +00:00
def __str__(self):
return self.dns
2016-07-03 14:49:29 +00:00
class Alias(models.Model):
PRETTY_NAME = "Alias dns"
interface_parent = models.ForeignKey('Interface', on_delete=models.CASCADE)
2016-11-18 16:36:30 +00:00
alias = models.CharField(help_text="Obligatoire et unique, ne doit pas comporter de points", max_length=255)
extension = models.ForeignKey('Extension', on_delete=models.PROTECT)
class Meta:
unique_together = ("alias", "extension")
def clean(self, *args, **kwargs):
if hasattr(self, 'alias') and hasattr(self, 'extension'):
if Interface.objects.filter(dns=self.alias).filter(ipv4=IpList.objects.filter(ip_type=IpType.objects.filter(extension=self.extension))):
raise ValidationError("Impossible d'ajouter l'alias, déjà utilisé par une machine")
def __str__(self):
return str(self.alias) + str(self.extension)
2016-07-03 14:49:29 +00:00
class IpList(models.Model):
PRETTY_NAME = "Addresses ipv4"
ipv4 = models.GenericIPAddressField(protocol='IPv4', unique=True)
2016-10-22 22:55:58 +00:00
ip_type = models.ForeignKey('IpType', on_delete=models.PROTECT)
2016-07-03 14:49:29 +00:00
def __str__(self):
return self.ipv4
@receiver(post_save, sender=Interface)
def interface_post_save(sender, **kwargs):
user = kwargs['instance'].machine.user
2016-11-20 15:53:59 +00:00
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
2016-11-20 15:53:59 +00:00
user.ldap_sync(base=False, access_refresh=False, mac_refresh=True)