Switch to FormView for displaying the order

This commit is contained in:
Nico Schottelius 2022-01-16 17:13:59 +01:00
parent e910556952
commit 686d2c2b1e
5 changed files with 99 additions and 30 deletions

View File

@ -36,6 +36,11 @@ machine. Use `kubectl get nodes` to verify minikube is up and running.
* resources should have a slug * resources should have a slug
* can be used as an identifier and non unique names * can be used as an identifier and non unique names
#### 3.1 (validation release, planned)
* Ensure that one resource cannot have multiple price_per_timeframe of
the same timeframe
#### 3.0.2 (planned) #### 3.0.2 (planned)
@ -50,7 +55,7 @@ machine. Use `kubectl get nodes` to verify minikube is up and running.
* Need to list resources [done] * Need to list resources [done]
* Need to create manytomany relations for each resource resoluting * Need to create manytomany relations for each resource resoluting
in ResourceOrders1 in ResourceOrders1
* Need to pass in the price for the selected timeframe * Need to pass in the price for the selected timeframe [done]
* On submit * On submit
* List of * List of
* resources + values * resources + values

13
uncloud_v3/app/forms.py Normal file
View File

@ -0,0 +1,13 @@
from django import forms
class ProductOrderForm(forms.Form):
product = forms.SlugField(required=True)
timeframe = forms.SlugField(required=True)
def __init__(self, resources, *args, **kwargs):
super().__init__(*args, **kwargs)
for res in resources:
print(res)
field_name = f"{res.name}"
#field_name = f"resource_{res.slug}"
self.fields[field_name] = forms.FloatField(required=True)

View File

@ -74,14 +74,6 @@ class Resource(models.Model):
return f"{self.slug}: {minimum}-{maximum} (+/-){self.step_size} {self.unit} ({pricing})" return f"{self.slug}: {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)
class Product(models.Model): class Product(models.Model):
""" """
@ -118,6 +110,21 @@ class Product(models.Model):
def __str__(self): def __str__(self):
return self.name return self.name
class ResourceOrder(models.Model):
"""
Resources that have been ordered
The timeframe should be in the ProductOrder, as it needs to be consistent
for all ordered resources
We need to record the selected value *and* potentially the
calculated price
"""
timeframe = models.ForeignKey(TimeFrame, on_delete=models.CASCADE)
value = models.FloatField()
resource = models.ForeignKey(Resource, on_delete=models.CASCADE)
class ProductOrder(models.Model): class ProductOrder(models.Model):
""" """
Describes a product a user bought Describes a product a user bought

View File

@ -1,9 +1,15 @@
<h2>Ordering a {{ product }} instance for {{ timeframe }}</h2> <h2>Ordering a {{ product }} instance for {{ timeframe }}</h2>
TODO: catch invalid timeframe
<form method="post" > <form method="post" >
{% csrf_token %} {% csrf_token %}
<table>
{{ form }}
</table>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<!--
<div class="form-group"> <div class="form-group">
<label for="product">Product</label> <label for="product">Product</label>
<input type="text" class="form-control" <input type="text" class="form-control"
@ -18,7 +24,7 @@ TODO: catch invalid timeframe
{% for res,price,currency in res_price %} {% for res,price,currency in res_price %}
<div class="form-group"> <div class="form-group">
<label for="{{res.slug}}">{{res.name}} {{res.unit}} <label for="{{res.slug}}">{{res.name}} {{res.unit}}
{{ price }}{{ currency.short_name }}/unit/{{ timeframe}} {{ price }}{{ currency.short_name }} per {{ timeframe}} per unit
{% if res.minimum_units %} (Min: {{res.minimum_units}}) {%endif%} {% if res.minimum_units %} (Min: {{res.minimum_units}}) {%endif%}
{% if res.maximum_units %} (Max: {{res.maximum_units}}) {%endif%} {% if res.maximum_units %} (Max: {{res.maximum_units}}) {%endif%}
</label> </label>
@ -27,5 +33,4 @@ TODO: catch invalid timeframe
</div> </div>
{% endfor %} {% endfor %}
<button type="submit" class="btn btn-primary">Submit</button> -->
</form>

View File

@ -1,35 +1,74 @@
from django.shortcuts import render, get_object_or_404 from django.shortcuts import render, get_object_or_404
from django.urls import reverse from django.urls import reverse
from django.views.generic.edit import CreateView from django.views.generic.edit import CreateView, FormView
from django.views.generic.base import TemplateView from django.views.generic.base import TemplateView
from django.views.generic.list import ListView from django.views.generic.list import ListView
from django.views.generic.detail import DetailView from django.views.generic.detail import DetailView
from .models import * from django.http import HttpResponse
class ProductOrderView(CreateView): from .models import *
model = ProductOrder from .forms import *
fields = ['resources']
#class ProductOrderView(CreateView):
class ProductOrderView(FormView):
form_class = ProductOrderForm
template_name = 'app/productorder_form.html'
success_url = '/'
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
# print(self)
# print(self.request)
print(self.kwargs)
#context = self.get_context_data(**kwargs)
#kwargs['resources'] = self.request['object'].resources.all()
product = get_object_or_404(Product, slug=self.kwargs['product'])
kwargs['resources'] = product.resources.all()
return kwargs
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
context['product'] = get_object_or_404(Product, slug=self.kwargs['product']) context['product'] = get_object_or_404(Product, slug=self.kwargs['product'])
context['timeframe'] = get_object_or_404(TimeFrame, slug=self.kwargs['timeframe']) context['timeframe'] = get_object_or_404(TimeFrame, slug=self.kwargs['timeframe'])
# context['form'] = ProductOrderForm(
res_price = [] # context['product'].resources.all(),
for res in context['product'].resources.all(): # initial={'product': context['product'].slug,
price = res.price_per_time.filter(timeframe=context['timeframe'])[0].price # 'timeframe': context['timeframe'].slug}
currency = res.price_per_time.filter(timeframe=context['timeframe'])[0].currency # )
res_price.append((res, price, currency))
context['res_price'] = res_price
print(context)
print(self.kwargs)
return context return context
# v2
# def post(self, request, *args, **kwargs):
# context = self.get_context_data()
# if context["form"].is_valid():
# print("form good")
# else:
# print("form not good")
# print(context["form"].errors)
# return HttpResponse("All good")
# v1
# def get_context_data(self, **kwargs):
# context = super().get_context_data(**kwargs)
# context['product'] = get_object_or_404(Product, slug=self.kwargs['product'])
# context['timeframe'] = get_object_or_404(TimeFrame, slug=self.kwargs['timeframe'])
# res_price = []
# for res in context['product'].resources.all():
# price = res.price_per_time.filter(timeframe=context['timeframe'])[0].price
# currency = res.price_per_time.filter(timeframe=context['timeframe'])[0].currency
# res_price.append((res, price, currency))
# context['res_price'] = res_price
# print(context)
# print(self.kwargs)
# return context
class ProductDetailView(DetailView): class ProductDetailView(DetailView):
model = Product model = Product