68 lines
1.8 KiB
Python
68 lines
1.8 KiB
Python
from enum import Enum
|
|
from typing import Any
|
|
|
|
|
|
from django.contrib.auth.models import User
|
|
from django.core.validators import MinValueValidator
|
|
from django.db import models
|
|
|
|
|
|
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 DeploymentUser(models.Model):
|
|
user = models.OneToOneField(User, on_delete=models.CASCADE)
|
|
credits = models.SmallIntegerField(
|
|
null=False,
|
|
default=1,
|
|
help_text="number of deployment allowed (-1 infinite)",
|
|
validators=[
|
|
MinValueValidator(-1, message="Value must be greater than or equal to -1"),
|
|
],
|
|
)
|
|
|
|
def __str__(self) -> str:
|
|
return f"{self.user.username} - {self.credits}"
|
|
|
|
|
|
class Deployment(models.Model):
|
|
id = models.UUIDField(primary_key=True)
|
|
name = models.TextField(null=False, blank=False)
|
|
type = models.CharField(
|
|
max_length=6, choices=Type.into_choices(), default=Type.SLIM.name
|
|
)
|
|
status = models.CharField(
|
|
max_length=7, choices=Status.into_choices(), default=Status.READY.name
|
|
)
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
updated_at = models.DateTimeField(auto_now=True)
|
|
error = models.TextField(null=True, blank=False)
|
|
task_id = models.UUIDField(null=True)
|
|
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
|
|
|
def __str__(self):
|
|
return f"{self.name} | {self.type} | {self.status}"
|
|
|
|
def __init__(self, *args: Any, **kwargs: Any):
|
|
self.progress = None
|
|
super().__init__(*args, **kwargs)
|