8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-22 11:23:10 +00:00

Prise en charge de la liaison port-port

This commit is contained in:
chirac 2016-07-06 15:52:33 +02:00
parent 38e23eb10a
commit e9d7071332

View file

@ -1,7 +1,17 @@
from django.db import models from django.db import models
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey from django.contrib.contenttypes.fields import GenericForeignKey
from django.core.exceptions import ValidationError
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()
class Switch(models.Model): class Switch(models.Model):
building = models.CharField(max_length=10) building = models.CharField(max_length=10)
@ -20,22 +30,23 @@ class Port(models.Model):
details = models.CharField(max_length=255, blank=True) details = models.CharField(max_length=255, blank=True)
room = models.ForeignKey('Room', on_delete=models.PROTECT, blank=True, null=True) room = models.ForeignKey('Room', on_delete=models.PROTECT, blank=True, null=True)
machine_interface = models.OneToOneField('machines.Interface', on_delete=models.PROTECT, blank=True, null=True) machine_interface = models.OneToOneField('machines.Interface', on_delete=models.PROTECT, blank=True, null=True)
related = models.OneToOneField('self', null=True, blank=True, related_name='related_port')
class Meta: class Meta:
unique_together = ('_content_type', '_object_id') unique_together = ('switch', 'port')
_content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE, blank=True, null=True) def clean(self):
_object_id = models.PositiveIntegerField(blank=True, null=True) if self.room and self.machine_interface or self.room and self.related or self.machine_interface and self.related:
goto = GenericForeignKey('_content_type', '_object_id') raise ValidationError("Chambre, interface et related_port sont mutuellement exclusifs")
if self.related==self:
@property raise ValidationError("On ne peut relier un port à lui même")
def comefrom(self): if self.related and not self.related.related:
ctype = ContentType.objects.get_for_model(self.__class__) if self.related.machine_interface or self.related.room:
try: raise ValidationError("Le port relié est déjà occupé, veuillez le libérer avant de créer une relation")
event = Port.objects.get(_content_type__pk=ctype.id, _object_id=self.id) else:
except: make_port_related(self)
return None elif hasattr(self, 'related_port'):
return event clean_port_related(self)
def __str__(self): def __str__(self):
return str(self.switch) + " - " + str(self.port) return str(self.switch) + " - " + str(self.port)