8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-12-27 01:13:46 +00:00
re2o/topologie/models.py

63 lines
2.3 KiB
Python
Raw Normal View History

2016-07-03 01:12:41 +00:00
from django.db import models
from django.forms import ModelForm, Form
2016-07-03 12:02:46 +00:00
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey
from django.core.exceptions import ValidationError
2016-07-21 00:30:36 +00:00
import reversion
def make_port_related(port):
related_port = port.related
related_port.related = port
related_port.save()
def clean_port_related(port):
related_port = port.related_port
related_port.related = None
related_port.save()
2016-07-03 01:12:41 +00:00
2016-07-03 12:02:46 +00:00
class Switch(models.Model):
2016-07-03 01:12:41 +00:00
building = models.CharField(max_length=10)
2016-07-03 12:02:46 +00:00
number = models.IntegerField()
2016-07-03 01:12:41 +00:00
details = models.CharField(max_length=255, blank=True)
class Meta:
2016-07-03 12:02:46 +00:00
unique_together = ('building', 'number')
2016-07-03 01:12:41 +00:00
def __str__(self):
2016-07-03 12:02:46 +00:00
return str(self.building) + str(self.number)
2016-07-03 01:12:41 +00:00
2016-07-03 12:02:46 +00:00
class Port(models.Model):
2016-07-06 21:29:31 +00:00
switch = models.ForeignKey('Switch', related_name="ports")
2016-07-03 12:02:46 +00:00
port = models.IntegerField()
room = models.ForeignKey('Room', on_delete=models.PROTECT, blank=True, null=True)
machine_interface = models.OneToOneField('machines.Interface', on_delete=models.SET_NULL, blank=True, null=True)
related = models.OneToOneField('self', null=True, blank=True, related_name='related_port')
details = models.CharField(max_length=255, blank=True)
2016-07-03 12:02:46 +00:00
class Meta:
unique_together = ('switch', 'port')
def clean(self):
if self.room and self.machine_interface or self.room and self.related or self.machine_interface and self.related:
raise ValidationError("Chambre, interface et related_port sont mutuellement exclusifs")
if self.related==self:
raise ValidationError("On ne peut relier un port à lui même")
if self.related and not self.related.related:
if self.related.machine_interface or self.related.room:
raise ValidationError("Le port relié est déjà occupé, veuillez le libérer avant de créer une relation")
else:
make_port_related(self)
elif hasattr(self, 'related_port'):
clean_port_related(self)
2016-07-03 01:12:41 +00:00
def __str__(self):
2016-07-03 12:02:46 +00:00
return str(self.switch) + " - " + str(self.port)
2016-07-03 01:12:41 +00:00
2016-07-03 12:02:46 +00:00
class Room(models.Model):
2016-07-04 21:54:21 +00:00
name = models.CharField(max_length=255, unique=True)
2016-07-19 00:30:52 +00:00
details = models.CharField(max_length=255, blank=True)
2016-07-03 01:12:41 +00:00
def __str__(self):
2016-07-04 21:54:21 +00:00
return str(self.name)
2016-07-03 12:02:46 +00:00