From 69e564cba640128aea410f565c81f60f2d5a11c7 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Sun, 2 Jan 2022 21:01:03 +0100 Subject: [PATCH] v3: seed timeframes, begin order definition --- uncloud_v3/app/admin.py | 3 + uncloud_v3/app/migrations/0001_initial.py | 59 ++++++++++++++++++- .../app/migrations/0002_auto_20220102_1953.py | 28 +++++++++ uncloud_v3/app/migrations/0002_resource.py | 27 --------- .../app/migrations/0003_product_resources.py | 18 ------ uncloud_v3/app/models.py | 41 +++++++++++-- 6 files changed, 123 insertions(+), 53 deletions(-) create mode 100644 uncloud_v3/app/migrations/0002_auto_20220102_1953.py delete mode 100644 uncloud_v3/app/migrations/0002_resource.py delete mode 100644 uncloud_v3/app/migrations/0003_product_resources.py diff --git a/uncloud_v3/app/admin.py b/uncloud_v3/app/admin.py index cb95b5c..cdcebbf 100644 --- a/uncloud_v3/app/admin.py +++ b/uncloud_v3/app/admin.py @@ -5,8 +5,11 @@ from .models import * for m in [ Order, + PricePerTime, Product, + ProductOrder, Resource, + ResourceOrder, TimeFrame, ]: admin.site.register(m) diff --git a/uncloud_v3/app/migrations/0001_initial.py b/uncloud_v3/app/migrations/0001_initial.py index 1ba5751..d13191f 100644 --- a/uncloud_v3/app/migrations/0001_initial.py +++ b/uncloud_v3/app/migrations/0001_initial.py @@ -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')), ], ), ] diff --git a/uncloud_v3/app/migrations/0002_auto_20220102_1953.py b/uncloud_v3/app/migrations/0002_auto_20220102_1953.py new file mode 100644 index 0000000..4846c25 --- /dev/null +++ b/uncloud_v3/app/migrations/0002_auto_20220102_1953.py @@ -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), + ] diff --git a/uncloud_v3/app/migrations/0002_resource.py b/uncloud_v3/app/migrations/0002_resource.py deleted file mode 100644 index 95df67c..0000000 --- a/uncloud_v3/app/migrations/0002_resource.py +++ /dev/null @@ -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')), - ], - ), - ] diff --git a/uncloud_v3/app/migrations/0003_product_resources.py b/uncloud_v3/app/migrations/0003_product_resources.py deleted file mode 100644 index 4b656f6..0000000 --- a/uncloud_v3/app/migrations/0003_product_resources.py +++ /dev/null @@ -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'), - ), - ] diff --git a/uncloud_v3/app/models.py b/uncloud_v3/app/models.py index ef39a32..50a9e1d 100644 --- a/uncloud_v3/app/models.py +++ b/uncloud_v3/app/models.py @@ -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)