From d3b6d28ae65f77a36dfa97ea0755f929d8c3139f Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Fri, 14 Jan 2022 22:23:39 +0100 Subject: [PATCH] Add ProductOrderView --- uncloud_v3/README.md | 6 +++++ uncloud_v3/app/admin.py | 1 + ...r_product_resources_alter_resource_unit.py | 23 ++++++++++++++++ .../app/migrations/0004_auto_20220102_2020.py | 19 ++++++++++++++ uncloud_v3/app/models.py | 26 +++++++++++++++---- .../app/templates/app/productorder_form.html | 4 +++ uncloud_v3/app/views.py | 7 ++++- uncloud_v3/uncloud/urls.py | 2 ++ 8 files changed, 82 insertions(+), 6 deletions(-) create mode 100644 uncloud_v3/app/migrations/0003_alter_product_resources_alter_resource_unit.py create mode 100644 uncloud_v3/app/migrations/0004_auto_20220102_2020.py create mode 100644 uncloud_v3/app/templates/app/productorder_form.html diff --git a/uncloud_v3/README.md b/uncloud_v3/README.md index 2c66a09..d6ecad0 100644 --- a/uncloud_v3/README.md +++ b/uncloud_v3/README.md @@ -12,3 +12,9 @@ machine. Use `kubectl get nodes` to verify minikube is up and running. * `SECRET_KEY` * `DEBUG` * `DATABASE` + +## Versions + +#### 3.0.0 + +* Introduce ProductOrderView diff --git a/uncloud_v3/app/admin.py b/uncloud_v3/app/admin.py index cdcebbf..b14e989 100644 --- a/uncloud_v3/app/admin.py +++ b/uncloud_v3/app/admin.py @@ -4,6 +4,7 @@ from .models import * for m in [ + Currency, Order, PricePerTime, Product, diff --git a/uncloud_v3/app/migrations/0003_alter_product_resources_alter_resource_unit.py b/uncloud_v3/app/migrations/0003_alter_product_resources_alter_resource_unit.py new file mode 100644 index 0000000..c784159 --- /dev/null +++ b/uncloud_v3/app/migrations/0003_alter_product_resources_alter_resource_unit.py @@ -0,0 +1,23 @@ +# Generated by Django 4.0 on 2022-01-02 20:04 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('app', '0002_auto_20220102_1953'), + ] + + operations = [ + migrations.AlterField( + model_name='product', + name='resources', + field=models.ManyToManyField(blank=True, to='app.Resource'), + ), + migrations.AlterField( + model_name='resource', + name='unit', + field=models.CharField(max_length=128), + ), + ] diff --git a/uncloud_v3/app/migrations/0004_auto_20220102_2020.py b/uncloud_v3/app/migrations/0004_auto_20220102_2020.py new file mode 100644 index 0000000..775ca2d --- /dev/null +++ b/uncloud_v3/app/migrations/0004_auto_20220102_2020.py @@ -0,0 +1,19 @@ +# Generated by Django 4.0 on 2022-01-02 20:20 + +from django.db import migrations + +def gen_currencies(apps, schema_editor): + Currency = apps.get_model('app', 'Currency') + + Currency.objects.get_or_create(name="Swiss Franc", + short_name="CHF") + +class Migration(migrations.Migration): + + dependencies = [ + ('app', '0003_alter_product_resources_alter_resource_unit'), + ] + + operations = [ + migrations.RunPython(gen_currencies), + ] diff --git a/uncloud_v3/app/models.py b/uncloud_v3/app/models.py index 50a9e1d..95204a9 100644 --- a/uncloud_v3/app/models.py +++ b/uncloud_v3/app/models.py @@ -7,7 +7,7 @@ class Currency(models.Model): short_name = models.CharField(max_length=3, unique=True) def __str__(self): - return f"{self.name} {self.short_name}" + return f"{self.name} ({self.short_name})" class TimeFrame(models.Model): @@ -31,7 +31,8 @@ class TimeFrame(models.Model): return f"{days} days {hours} hours {secs} seconds" def __str__(self): - return "{} ({})".format(self.name, self.secs_to_name(self.seconds)) + #return "{} ({})".format(self.name, self.secs_to_name(self.seconds)) + return f"{self.name}" class PricePerTime(models.Model): timeframe = models.ForeignKey(TimeFrame, on_delete=models.CASCADE) @@ -43,7 +44,7 @@ class PricePerTime(models.Model): class Resource(models.Model): name = models.CharField(max_length=128, unique=True) # CPU, RAM - unit = models.CharField(max_length=128, unique=True) # Count, GB + unit = models.CharField(max_length=128) # 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 @@ -51,7 +52,22 @@ class Resource(models.Model): 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}" + 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): """ @@ -72,7 +88,7 @@ class Product(models.Model): # textconfig = models.ManyToManyField(ProductTextConfiguration) # textfieldconfig = models.ManyToManyField(ProductTextFieldConfiguration) - resources = models.ManyToManyField(Resource) + resources = models.ManyToManyField(Resource, blank=True) def __str__(self): return self.name diff --git a/uncloud_v3/app/templates/app/productorder_form.html b/uncloud_v3/app/templates/app/productorder_form.html new file mode 100644 index 0000000..256405a --- /dev/null +++ b/uncloud_v3/app/templates/app/productorder_form.html @@ -0,0 +1,4 @@ +
{% csrf_token %} + {{ form.as_p }} + +
diff --git a/uncloud_v3/app/views.py b/uncloud_v3/app/views.py index 91ea44a..2012f8c 100644 --- a/uncloud_v3/app/views.py +++ b/uncloud_v3/app/views.py @@ -1,3 +1,8 @@ from django.shortcuts import render -# Create your views here. +from django.views.generic.edit import CreateView +from .models import ProductOrder + +class ProductOrderView(CreateView): + model = ProductOrder + fields = ['product', 'resources' ] diff --git a/uncloud_v3/uncloud/urls.py b/uncloud_v3/uncloud/urls.py index 5df08ea..1a0df4c 100644 --- a/uncloud_v3/uncloud/urls.py +++ b/uncloud_v3/uncloud/urls.py @@ -15,7 +15,9 @@ Including another URLconf """ from django.contrib import admin from django.urls import path +from app import views as appviews urlpatterns = [ path('admin/', admin.site.urls), + path('order', appviews.ProductOrderView.as_view()) ]