v3: seed timeframes, begin order definition

This commit is contained in:
Nico Schottelius 2022-01-02 21:01:03 +01:00
parent 42e200b564
commit 69e564cba6
6 changed files with 123 additions and 53 deletions

View file

@ -5,8 +5,11 @@ from .models import *
for m in [
Order,
PricePerTime,
Product,
ProductOrder,
Resource,
ResourceOrder,
TimeFrame,
]:
admin.site.register(m)

View file

@ -1,4 +1,4 @@
# Generated by Django 4.0 on 2022-01-02 18:06
# Generated by Django 4.0 on 2022-01-02 19:50
from django.db import migrations, models
import django.db.models.deletion
@ -14,6 +14,22 @@ class Migration(migrations.Migration):
]
operations = [
migrations.CreateModel(
name='Currency',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=128, unique=True)),
('short_name', models.CharField(max_length=3, unique=True)),
],
),
migrations.CreateModel(
name='PricePerTime',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('price', models.FloatField()),
('currency', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='app.currency')),
],
),
migrations.CreateModel(
name='Product',
fields=[
@ -21,6 +37,18 @@ class Migration(migrations.Migration):
('name', models.CharField(max_length=128, unique=True)),
],
),
migrations.CreateModel(
name='Resource',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=128, unique=True)),
('unit', models.CharField(max_length=128, unique=True)),
('minimum_units', models.FloatField(blank=True, null=True)),
('maximum_units', models.FloatField(blank=True, null=True)),
('step_size', models.FloatField(default=1)),
('price_per_time', models.ManyToManyField(blank=True, to='app.PricePerTime')),
],
),
migrations.CreateModel(
name='TimeFrame',
fields=[
@ -29,6 +57,33 @@ class Migration(migrations.Migration):
('seconds', models.IntegerField(blank=True, null=True)),
],
),
migrations.CreateModel(
name='ResourceOrder',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('value', models.FloatField()),
('resource', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='app.resource')),
('timeframe', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='app.timeframe')),
],
),
migrations.CreateModel(
name='ProductOrder',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('product', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='app.product')),
('resources', models.ManyToManyField(to='app.ResourceOrder')),
],
),
migrations.AddField(
model_name='product',
name='resources',
field=models.ManyToManyField(to='app.Resource'),
),
migrations.AddField(
model_name='pricepertime',
name='timeframe',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='app.timeframe'),
),
migrations.CreateModel(
name='Order',
fields=[
@ -37,7 +92,7 @@ class Migration(migrations.Migration):
('starting_date', models.DateTimeField(default=django.utils.timezone.now)),
('ending_date', models.DateTimeField(blank=True, null=True)),
('owner', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, to='uauth.user')),
('product', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='app.product')),
('product', models.ManyToManyField(blank=True, to='app.ProductOrder')),
],
),
]

View file

@ -0,0 +1,28 @@
# Generated by Django 4.0 on 2022-01-02 19:53
from django.db import migrations
def gen_timeframes(apps, schema_editor):
# We can't import the Person model directly as it may be a newer
# version than this migration expects. We use the historical version.
TimeFrame = apps.get_model('app', 'TimeFrame')
for timeframe in [ (3600, "1 hour"),
(86400, "1 day"),
(7*86400, "7 days"),
(30*86400, "30 days"),
(365*86400, "365 days") ]:
tf = TimeFrame(name=timeframe[1],
seconds=timeframe[0])
tf.save()
class Migration(migrations.Migration):
dependencies = [
('app', '0001_initial'),
]
operations = [
migrations.RunPython(gen_timeframes),
]

View file

@ -1,27 +0,0 @@
# Generated by Django 4.0 on 2022-01-02 18:28
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('app', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='Resource',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=128, unique=True)),
('unit', models.CharField(max_length=128, unique=True)),
('minimum_units', models.FloatField(blank=True, null=True)),
('maximum_units', models.FloatField(blank=True, null=True)),
('step_size', models.FloatField(default=1)),
('price', models.FloatField()),
('timeframe', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='app.timeframe')),
],
),
]

View file

@ -1,18 +0,0 @@
# Generated by Django 4.0 on 2022-01-02 18:33
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('app', '0002_resource'),
]
operations = [
migrations.AddField(
model_name='product',
name='resources',
field=models.ManyToManyField(to='app.Resource'),
),
]

View file

@ -2,6 +2,14 @@ from django.db import models
from django.contrib.auth import get_user_model
from django.utils import timezone
class Currency(models.Model):
name = models.CharField(max_length=128, unique=True)
short_name = models.CharField(max_length=3, unique=True)
def __str__(self):
return f"{self.name} {self.short_name}"
class TimeFrame(models.Model):
name = models.CharField(max_length=128, unique=True)
seconds = models.IntegerField(null=True, blank=True)
@ -25,6 +33,14 @@ class TimeFrame(models.Model):
def __str__(self):
return "{} ({})".format(self.name, self.secs_to_name(self.seconds))
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}"
class Resource(models.Model):
name = models.CharField(max_length=128, unique=True) # CPU, RAM
unit = models.CharField(max_length=128, unique=True) # Count, GB
@ -32,11 +48,18 @@ class Resource(models.Model):
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
price_per_time = models.ManyToManyField(PricePerTime, blank=True)
def __str__(self):
return f"{self.name}: {self.minimum_units}-{self.maximum_units} (+/-){self.step_size} {self.unit} {self.price}/{self.timeframe}"
return f"{self.name}: {self.minimum_units}-{self.maximum_units} (+/-){self.step_size} {self.unit}"
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)
class Product(models.Model):
@ -49,12 +72,18 @@ class Product(models.Model):
# textconfig = models.ManyToManyField(ProductTextConfiguration)
# textfieldconfig = models.ManyToManyField(ProductTextFieldConfiguration)
# timeframes = models.ManyToManyField(TimeFrame)
resources = models.ManyToManyField(Resource)
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)
class Order(models.Model):
owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, editable=False)
@ -63,6 +92,6 @@ class Order(models.Model):
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)
product = models.ManyToManyField(ProductOrder, blank=True)
#textconfigs = models.ManyToManyField(ResourceConfig)