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" | ||||
|     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 = rego.Category.objects.get(pk=category_id) | ||||
|     current_cart = CartController.for_user(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 = products.order_by("order") | ||||
|     products = ProductController.available_products( | ||||
|  | @ -115,17 +114,33 @@ def product_category(request, category_id): | |||
| 
 | ||||
|     ProductsForm = forms.ProductsForm(products) | ||||
| 
 | ||||
|     if request.method == "POST": | ||||
|         cat_form = ProductsForm( | ||||
|             request.POST, | ||||
|             request.FILES, | ||||
|             prefix=PRODUCTS_FORM_PREFIX) | ||||
| 
 | ||||
|         if voucher_handled: | ||||
|             # The voucher form was handled here. | ||||
|             pass | ||||
|         elif cat_form.is_valid(): | ||||
|     # 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( | ||||
|         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) | ||||
|         except ValidationError: | ||||
|             pass | ||||
|  | @ -134,8 +149,7 @@ def product_category(request, category_id): | |||
|         # in an active+valid cart | ||||
| 
 | ||||
|         if category.required: | ||||
|                 carts = rego.Cart.reserved_carts() | ||||
|                 carts = carts.filter(user=request.user) | ||||
|             carts = rego.Cart.reserved_carts().filter(user=request.user) | ||||
|             items = rego.ProductItem.objects.filter( | ||||
|                 product__category=category, | ||||
|                 cart=carts, | ||||
|  | @ -152,27 +166,8 @@ def product_category(request, category_id): | |||
|                 attendee.save() | ||||
|             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) | ||||
| 
 | ||||
|     data = { | ||||
|         "category": category, | ||||
|         "discounts": discounts, | ||||
|  |  | |||
		Loading…
	
	Add table
		
		Reference in a new issue
	
	 Christopher Neugebauer
						Christopher Neugebauer