Add ProductOrderView

This commit is contained in:
Nico Schottelius 2022-01-14 22:23:39 +01:00
parent 69e564cba6
commit d3b6d28ae6
8 changed files with 82 additions and 6 deletions

View file

@ -12,3 +12,9 @@ machine. Use `kubectl get nodes` to verify minikube is up and running.
* `SECRET_KEY` * `SECRET_KEY`
* `DEBUG` * `DEBUG`
* `DATABASE` * `DATABASE`
## Versions
#### 3.0.0
* Introduce ProductOrderView

View file

@ -4,6 +4,7 @@ from .models import *
for m in [ for m in [
Currency,
Order, Order,
PricePerTime, PricePerTime,
Product, Product,

View file

@ -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),
),
]

View file

@ -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),
]

View file

@ -7,7 +7,7 @@ class Currency(models.Model):
short_name = models.CharField(max_length=3, unique=True) short_name = models.CharField(max_length=3, unique=True)
def __str__(self): def __str__(self):
return f"{self.name} {self.short_name}" return f"{self.name} ({self.short_name})"
class TimeFrame(models.Model): class TimeFrame(models.Model):
@ -31,7 +31,8 @@ class TimeFrame(models.Model):
return f"{days} days {hours} hours {secs} seconds" return f"{days} days {hours} hours {secs} seconds"
def __str__(self): 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): class PricePerTime(models.Model):
timeframe = models.ForeignKey(TimeFrame, on_delete=models.CASCADE) timeframe = models.ForeignKey(TimeFrame, on_delete=models.CASCADE)
@ -43,7 +44,7 @@ class PricePerTime(models.Model):
class Resource(models.Model): class Resource(models.Model):
name = models.CharField(max_length=128, unique=True) # CPU, RAM 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 minimum_units = models.FloatField(null=True, blank=True) # might have min
maximum_units = models.FloatField(null=True, blank=True) # might have max maximum_units = models.FloatField(null=True, blank=True) # might have max
step_size = models.FloatField(default=1) # might/must step size 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) price_per_time = models.ManyToManyField(PricePerTime, blank=True)
def __str__(self): 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): class ResourceOrder(models.Model):
""" """
@ -72,7 +88,7 @@ class Product(models.Model):
# textconfig = models.ManyToManyField(ProductTextConfiguration) # textconfig = models.ManyToManyField(ProductTextConfiguration)
# textfieldconfig = models.ManyToManyField(ProductTextFieldConfiguration) # textfieldconfig = models.ManyToManyField(ProductTextFieldConfiguration)
resources = models.ManyToManyField(Resource) resources = models.ManyToManyField(Resource, blank=True)
def __str__(self): def __str__(self):
return self.name return self.name

View file

@ -0,0 +1,4 @@
<form method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save">
</form>

View file

@ -1,3 +1,8 @@
from django.shortcuts import render 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' ]

View file

@ -15,7 +15,9 @@ Including another URLconf
""" """
from django.contrib import admin from django.contrib import admin
from django.urls import path from django.urls import path
from app import views as appviews
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('order', appviews.ProductOrderView.as_view())
] ]