uncloud-mravi/uncloud_v3/app/models.py

114 lines
3.5 KiB
Python
Raw Normal View History

2022-01-02 17:29:35 +00:00
from django.db import models
2022-01-02 18:34:55 +00:00
from django.contrib.auth import get_user_model
from django.utils import timezone
2022-01-02 17:29:35 +00:00
class Currency(models.Model):
name = models.CharField(max_length=128, unique=True)
short_name = models.CharField(max_length=3, unique=True)
def __str__(self):
2022-01-14 21:23:39 +00:00
return f"{self.name} ({self.short_name})"
2022-01-02 18:34:55 +00:00
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):
2022-01-14 21:23:39 +00:00
#return "{} ({})".format(self.name, self.secs_to_name(self.seconds))
return f"{self.name}"
2022-01-02 18:34:55 +00:00
class PricePerTime(models.Model):
timeframe = models.ForeignKey(TimeFrame, on_delete=models.CASCADE)
price = models.FloatField()
currency = models.ForeignKey(Currency, on_delete=models.CASCADE)
def __str__(self):
return f"{self.price} {self.currency.short_name}/{self.timeframe}"
2022-01-02 18:34:55 +00:00
class Resource(models.Model):
name = models.CharField(max_length=128, unique=True) # CPU, RAM
2022-01-14 21:23:39 +00:00
unit = models.CharField(max_length=128) # Count, GB
2022-01-02 18:34:55 +00:00
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
price_per_time = models.ManyToManyField(PricePerTime, blank=True)
2022-01-02 18:34:55 +00:00
def __str__(self):
2022-01-14 21:23:39 +00:00
if self.minimum_units:
minimum = self.minimum_units
else:
minimum = "No minimum"
if self.maximum_units:
maximum = self.maximum_units
else:
maximum = "No maximum"
pricing = []
for price in self.price_per_time.all():
pricing.append(f"{price.price}{price.currency.short_name}/{price.timeframe}")
pricing = ", ".join(pricing)
return f"{self.name}: {minimum}-{maximum} (+/-){self.step_size} {self.unit} ({pricing})"
class ResourceOrder(models.Model):
"""
Resources that have been ordered
"""
timeframe = models.ForeignKey(TimeFrame, on_delete=models.CASCADE)
value = models.FloatField()
resource = models.ForeignKey(Resource, on_delete=models.CASCADE)
2022-01-02 18:34:55 +00:00
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)
2022-01-14 21:23:39 +00:00
resources = models.ManyToManyField(Resource, blank=True)
2022-01-02 18:34:55 +00:00
def __str__(self):
return self.name
class ProductOrder(models.Model):
"""
Describes a product a user bought
"""
product = models.ForeignKey(Product, on_delete=models.CASCADE)
resources = models.ManyToManyField(ResourceOrder)
2022-01-02 18:34:55 +00:00
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.ManyToManyField(ProductOrder, blank=True)
2022-01-02 18:34:55 +00:00
#textconfigs = models.ManyToManyField(ResourceConfig)