Refactors the product_category view to be much simpler

This commit is contained in:
Christopher Neugebauer 2016-03-26 20:21:54 +11:00
parent b13e6f7ce2
commit 464684f13e

View file

@ -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,64 +114,60 @@ 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 = []
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))
if voucher_handled: cat_form = ProductsForm(
# The voucher form was handled here. request.POST or None,
pass product_quantities=quantities,
elif cat_form.is_valid(): prefix=PRODUCTS_FORM_PREFIX,
try: )
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
# If category is required, the user must have at least one # If category is required, the user must have at least one
# 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, )
if len(items) == 0:
cat_form.add_error(
None,
"You must have at least one item from this category",
) )
if len(items) == 0:
cat_form.add_error(
None,
"You must have at least one item from this category",
)
if not cat_form.errors: if not cat_form.errors:
if category_id > attendee.highest_complete_category: if category_id > attendee.highest_complete_category:
attendee.highest_complete_category = category_id attendee.highest_complete_category = 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,