mumui/deployment/models.py
2023-09-17 14:06:23 +02:00

53 lines
1.4 KiB
Python

from enum import Enum
from uuid import uuid4
from django.contrib.auth.models import User
from django.db import models
from django.db.models.signals import pre_save, pre_delete
from django.dispatch import receiver
from django_eventstream import send_event
class Type(Enum):
SLIM = "Slim"
MEDIUM = "Medium"
LARGE = "Large"
@classmethod
def into_choices(cls):
return [(s.name, s.value) for s in cls]
class Status(Enum):
READY = "Ready"
PENDING = "Pending"
RUNNING = "Running"
SUCCESS = "Success"
FAILED = "Failed"
@classmethod
def into_choices(cls):
return [(s.name, s.value) for s in cls]
class Deployment(models.Model):
id = models.UUIDField(primary_key=True, default=uuid4())
name = models.TextField(null=False, blank=False)
type = models.CharField(
max_length=6, choices=Type.into_choices(), default=Type.SLIM.value
)
status = models.CharField(
max_length=7, choices=Status.into_choices(), default=Status.READY.value
)
user = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return f"{self.name} | {self.type} | {self.status}"
@receiver(pre_save, sender=Deployment)
@receiver(pre_delete, sender=Deployment)
def deployment_update_handler(sender, instance, **kwargs):
send_event("deployment", "update", {"id": instance.id, "status": instance.status})