Refactors the product_category view to be much simpler
This commit is contained in:
parent
b13e6f7ce2
commit
464684f13e
1 changed files with 52 additions and 57 deletions
|
@ -93,19 +93,18 @@ def product_category(request, category_id):
|
||||||
PRODUCTS_FORM_PREFIX = "products"
|
PRODUCTS_FORM_PREFIX = "products"
|
||||||
VOUCHERS_FORM_PREFIX = "vouchers"
|
VOUCHERS_FORM_PREFIX = "vouchers"
|
||||||
|
|
||||||
|
# Handle the voucher form *before* listing products.
|
||||||
|
# Products can change as vouchers are entered.
|
||||||
|
v = handle_voucher(request, VOUCHERS_FORM_PREFIX)
|
||||||
|
voucher_form, voucher_handled = v
|
||||||
|
|
||||||
|
# Handle the products form
|
||||||
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)
|
||||||
current_cart = CartController.for_user(request.user)
|
current_cart = CartController.for_user(request.user)
|
||||||
|
|
||||||
attendee = rego.Attendee.get_instance(request.user)
|
attendee = rego.Attendee.get_instance(request.user)
|
||||||
|
|
||||||
# Handle the voucher form *before* listing products.
|
|
||||||
v = handle_voucher(request, VOUCHERS_FORM_PREFIX)
|
|
||||||
voucher_form, voucher_handled = v
|
|
||||||
if voucher_handled:
|
|
||||||
# Do not handle product form
|
|
||||||
pass
|
|
||||||
|
|
||||||
products = rego.Product.objects.filter(category=category)
|
products = rego.Product.objects.filter(category=category)
|
||||||
products = products.order_by("order")
|
products = products.order_by("order")
|
||||||
products = ProductController.available_products(
|
products = ProductController.available_products(
|
||||||
|
@ -115,17 +114,33 @@ def product_category(request, category_id):
|
||||||
|
|
||||||
ProductsForm = forms.ProductsForm(products)
|
ProductsForm = forms.ProductsForm(products)
|
||||||
|
|
||||||
if request.method == "POST":
|
# Create initial data for each of products in category
|
||||||
cat_form = ProductsForm(
|
items = rego.ProductItem.objects.filter(
|
||||||
request.POST,
|
product__category=category,
|
||||||
request.FILES,
|
cart=current_cart.cart,
|
||||||
prefix=PRODUCTS_FORM_PREFIX)
|
)
|
||||||
|
quantities = []
|
||||||
if voucher_handled:
|
for product in products:
|
||||||
# The voucher form was handled here.
|
# Only add items that are enabled.
|
||||||
pass
|
|
||||||
elif cat_form.is_valid():
|
|
||||||
try:
|
try:
|
||||||
|
quantity = items.get(product=product).quantity
|
||||||
|
except ObjectDoesNotExist:
|
||||||
|
quantity = 0
|
||||||
|
quantities.append((product, quantity))
|
||||||
|
|
||||||
|
cat_form = ProductsForm(
|
||||||
|
request.POST or None,
|
||||||
|
product_quantities=quantities,
|
||||||
|
prefix=PRODUCTS_FORM_PREFIX,
|
||||||
|
)
|
||||||
|
|
||||||
|
if (
|
||||||
|
not voucher_handled and
|
||||||
|
request.method == "POST" and
|
||||||
|
cat_form.is_valid()):
|
||||||
|
|
||||||
|
try:
|
||||||
|
if cat_form.has_changed():
|
||||||
handle_valid_cat_form(cat_form, current_cart)
|
handle_valid_cat_form(cat_form, current_cart)
|
||||||
except ValidationError:
|
except ValidationError:
|
||||||
pass
|
pass
|
||||||
|
@ -134,8 +149,7 @@ def product_category(request, category_id):
|
||||||
# in an active+valid cart
|
# in an active+valid cart
|
||||||
|
|
||||||
if category.required:
|
if category.required:
|
||||||
carts = rego.Cart.reserved_carts()
|
carts = rego.Cart.reserved_carts().filter(user=request.user)
|
||||||
carts = carts.filter(user=request.user)
|
|
||||||
items = rego.ProductItem.objects.filter(
|
items = rego.ProductItem.objects.filter(
|
||||||
product__category=category,
|
product__category=category,
|
||||||
cart=carts,
|
cart=carts,
|
||||||
|
@ -152,27 +166,8 @@ def product_category(request, category_id):
|
||||||
attendee.save()
|
attendee.save()
|
||||||
return redirect("dashboard")
|
return redirect("dashboard")
|
||||||
|
|
||||||
else:
|
|
||||||
# Create initial data for each of products in category
|
|
||||||
items = rego.ProductItem.objects.filter(
|
|
||||||
product__category=category,
|
|
||||||
cart=current_cart.cart,
|
|
||||||
)
|
|
||||||
quantities = []
|
|
||||||
for product in products:
|
|
||||||
# Only add items that are enabled.
|
|
||||||
try:
|
|
||||||
quantity = items.get(product=product).quantity
|
|
||||||
except ObjectDoesNotExist:
|
|
||||||
quantity = 0
|
|
||||||
quantities.append((product, quantity))
|
|
||||||
|
|
||||||
cat_form = ProductsForm(
|
|
||||||
prefix=PRODUCTS_FORM_PREFIX,
|
|
||||||
product_quantities=quantities,
|
|
||||||
)
|
|
||||||
|
|
||||||
discounts = discount.available_discounts(request.user, [], products)
|
discounts = discount.available_discounts(request.user, [], products)
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
"category": category,
|
"category": category,
|
||||||
"discounts": discounts,
|
"discounts": discounts,
|
||||||
|
|
Loading…
Reference in a new issue