Adds mechanism for entering a voucher code
This commit is contained in:
parent
cc42490832
commit
2d6b28c5a6
3 changed files with 34 additions and 4 deletions
|
@ -46,3 +46,10 @@ def CategoryForm(category):
|
||||||
_CategoryForm.base_fields[field_name(product)] = field
|
_CategoryForm.base_fields[field_name(product)] = field
|
||||||
|
|
||||||
return _CategoryForm
|
return _CategoryForm
|
||||||
|
|
||||||
|
class VoucherForm(forms.Form):
|
||||||
|
voucher = forms.CharField(
|
||||||
|
label="Voucher code",
|
||||||
|
help_text="If you have a voucher code, enter it here",
|
||||||
|
required=True,
|
||||||
|
)
|
||||||
|
|
|
@ -10,6 +10,12 @@
|
||||||
<form method="post" action="">
|
<form method="post" action="">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
|
|
||||||
|
<table>
|
||||||
|
{{ voucher_form }}
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<input type="submit">
|
||||||
|
|
||||||
<table>
|
<table>
|
||||||
{{ form }}
|
{{ form }}
|
||||||
</table>
|
</table>
|
||||||
|
|
|
@ -16,6 +16,9 @@ from django.shortcuts import render
|
||||||
def product_category(request, category_id):
|
def product_category(request, category_id):
|
||||||
''' Registration selections form for a specific category of items '''
|
''' Registration selections form for a specific category of items '''
|
||||||
|
|
||||||
|
PRODUCTS_FORM_PREFIX = "products"
|
||||||
|
VOUCHERS_FORM_PREFIX = "vouchers"
|
||||||
|
|
||||||
category_id = int(category_id) # Routing is [0-9]+
|
category_id = int(category_id) # Routing is [0-9]+
|
||||||
category = rego.Category.objects.get(pk=category_id)
|
category = rego.Category.objects.get(pk=category_id)
|
||||||
|
|
||||||
|
@ -25,9 +28,19 @@ def product_category(request, category_id):
|
||||||
products = products.order_by("order")
|
products = products.order_by("order")
|
||||||
|
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
cat_form = CategoryForm(request.POST, request.FILES)
|
cat_form = CategoryForm(request.POST, request.FILES, prefix=PRODUCTS_FORM_PREFIX)
|
||||||
if cat_form.is_valid():
|
voucher_form = forms.VoucherForm(request.POST, prefix=VOUCHERS_FORM_PREFIX)
|
||||||
current_cart = CartController.for_user(request.user)
|
current_cart = CartController.for_user(request.user)
|
||||||
|
|
||||||
|
if voucher_form.is_valid():
|
||||||
|
# Apply voucher
|
||||||
|
# leave
|
||||||
|
voucher = voucher_form.cleaned_data["voucher"]
|
||||||
|
try:
|
||||||
|
current_cart.apply_voucher(voucher)
|
||||||
|
except Exception as e:
|
||||||
|
voucher_form.add_error("voucher", e)
|
||||||
|
elif cat_form.is_valid():
|
||||||
try:
|
try:
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
for product_id, quantity, field_name \
|
for product_id, quantity, field_name \
|
||||||
|
@ -58,7 +71,9 @@ def product_category(request, category_id):
|
||||||
quantities.append((product, quantity))
|
quantities.append((product, quantity))
|
||||||
|
|
||||||
initial = CategoryForm.initial_data(quantities)
|
initial = CategoryForm.initial_data(quantities)
|
||||||
cat_form = CategoryForm(initial=initial)
|
cat_form = CategoryForm(prefix=PRODUCTS_FORM_PREFIX, initial=initial)
|
||||||
|
|
||||||
|
voucher_form = forms.VoucherForm(prefix=VOUCHERS_FORM_PREFIX)
|
||||||
|
|
||||||
for product in products:
|
for product in products:
|
||||||
# Remove fields that do not have an enabling condition.
|
# Remove fields that do not have an enabling condition.
|
||||||
|
@ -66,9 +81,11 @@ def product_category(request, category_id):
|
||||||
if not prod.can_add_with_enabling_conditions(request.user, 0):
|
if not prod.can_add_with_enabling_conditions(request.user, 0):
|
||||||
cat_form.disable_product(product)
|
cat_form.disable_product(product)
|
||||||
|
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
"category": category,
|
"category": category,
|
||||||
"form": cat_form,
|
"form": cat_form,
|
||||||
|
"voucher_form": voucher_form,
|
||||||
}
|
}
|
||||||
|
|
||||||
return render(request, "product_category.html", data)
|
return render(request, "product_category.html", data)
|
||||||
|
|
Loading…
Reference in a new issue