from django.db import models from django.contrib.auth import get_user_model from django.utils import timezone class TimeFrame(models.Model): name = models.CharField(max_length=128, unique=True) seconds = models.IntegerField(null=True, blank=True) @staticmethod def secs_to_name(secs): name = "" days = 0 hours = 0 if secs >= 24*3600: days = secs // (24*3600) secs -= (days*24*3600) if secs >= 3600: hours = secs // 3600 secs -= hours*3600 return f"{days} days {hours} hours {secs} seconds" def __str__(self): return "{} ({})".format(self.name, self.secs_to_name(self.seconds)) class Resource(models.Model): name = models.CharField(max_length=128, unique=True) # CPU, RAM unit = models.CharField(max_length=128, unique=True) # Count, GB minimum_units = models.FloatField(null=True, blank=True) # might have min maximum_units = models.FloatField(null=True, blank=True) # might have max step_size = models.FloatField(default=1) # might/must step size timeframe = models.ForeignKey(TimeFrame, on_delete=models.CASCADE) price = models.FloatField() # Per unit and per time frame def __str__(self): return f"{self.name}: {self.minimum_units}-{self.maximum_units} (+/-){self.step_size} {self.unit} {self.price}/{self.timeframe}" class Product(models.Model): """ Describes a product a user can buy """ name = models.CharField(max_length=128, unique=True) # textconfig = models.ManyToManyField(ProductTextConfiguration) # textfieldconfig = models.ManyToManyField(ProductTextFieldConfiguration) # timeframes = models.ManyToManyField(TimeFrame) resources = models.ManyToManyField(Resource) def __str__(self): return self.name class Order(models.Model): owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, editable=False) creation_date = models.DateTimeField(auto_now_add=True) starting_date = models.DateTimeField(default=timezone.now) ending_date = models.DateTimeField(blank=True, null=True) product = models.ForeignKey(Product, on_delete=models.CASCADE) #resourceconfigs = models.ManyToManyField(ResourceConfig) #textconfigs = models.ManyToManyField(ResourceConfig)