devicehub-django/action/models.py

36 lines
1.3 KiB
Python
Raw Normal View History

2024-11-30 07:38:19 +00:00
from django.db import models, connection
from user.models import User, Institution
2024-12-01 20:47:10 +00:00
from django.core.exceptions import ValidationError
2024-11-30 07:38:19 +00:00
class State(models.Model):
date = models.DateTimeField(auto_now_add=True)
institution = models.ForeignKey(Institution, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
2024-12-01 20:47:10 +00:00
state = models.CharField(max_length=50)
2024-11-30 07:38:19 +00:00
snapshot_uuid = models.UUIDField()
2024-12-01 20:54:22 +00:00
def clean(self):
if not StateDefinition.objects.filter(institution=self.institution, state=self.state).exists():
raise ValidationError(f"The state '{self.state}' is not valid for the institution '{self.institution.name}'.")
def save(self, *args, **kwargs):
self.clean()
super().save(*args, **kwargs)
2024-12-01 20:47:10 +00:00
2024-11-30 07:38:19 +00:00
def __str__(self):
return f"{self.institution.name} - {self.state} - {self.snapshot_uuid}"
2024-12-01 20:47:10 +00:00
class StateDefinition(models.Model):
institution = models.ForeignKey(Institution, on_delete=models.CASCADE)
order = models.AutoField(primary_key=True)
state = models.CharField(max_length=50)
2024-12-01 20:54:22 +00:00
class Meta:
2024-12-01 22:46:04 +00:00
constraints = [
models.UniqueConstraint(fields=['institution', 'state'], name='unique_institution_state')
]
2024-12-01 20:47:10 +00:00
def __str__(self):
return f"{self.institution.name} - {self.state}"