models.py 2.32 KB
Newer Older
chirac's avatar
chirac committed
1
from django.db import models
Dalahro's avatar
Dalahro committed
2
from django.forms import ModelForm, Form
chirac's avatar
chirac committed
3 4
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey
5
from django.core.exceptions import ValidationError
6
import reversion
7 8 9 10 11 12 13 14 15 16

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()
chirac's avatar
chirac committed
17

chirac's avatar
chirac committed
18
class Switch(models.Model):
chirac's avatar
chirac committed
19
    building = models.CharField(max_length=10)
chirac's avatar
chirac committed
20
    number = models.IntegerField()
chirac's avatar
chirac committed
21 22 23
    details = models.CharField(max_length=255, blank=True)

    class Meta:
chirac's avatar
chirac committed
24
        unique_together = ('building', 'number')
chirac's avatar
chirac committed
25 26

    def __str__(self):
chirac's avatar
chirac committed
27
        return str(self.building) + str(self.number)
chirac's avatar
chirac committed
28

chirac's avatar
chirac committed
29
class Port(models.Model):
Dalahro's avatar
Dalahro committed
30
    switch = models.ForeignKey('Switch', related_name="ports")
chirac's avatar
chirac committed
31 32
    port = models.IntegerField()
    room = models.ForeignKey('Room', on_delete=models.PROTECT, blank=True, null=True)
33
    machine_interface = models.OneToOneField('machines.Interface', on_delete=models.SET_NULL, blank=True, null=True)
34
    related = models.OneToOneField('self', null=True, blank=True, related_name='related_port')
Dalahro's avatar
Dalahro committed
35
    details = models.CharField(max_length=255, blank=True)
chirac's avatar
chirac committed
36 37

    class Meta:
38 39 40 41 42 43 44 45 46 47 48 49 50 51
        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)
chirac's avatar
chirac committed
52 53

    def __str__(self):
chirac's avatar
chirac committed
54
        return str(self.switch) + " - " + str(self.port)
chirac's avatar
chirac committed
55

chirac's avatar
chirac committed
56
class Room(models.Model):
lhark's avatar
lhark committed
57
    name = models.CharField(max_length=255, unique=True)
chirac's avatar
chirac committed
58
    details = models.CharField(max_length=255, blank=True)
chirac's avatar
chirac committed
59 60

    def __str__(self):
lhark's avatar
lhark committed
61
        return str(self.name)
chirac's avatar
chirac committed
62