From fc279b1922d370ec1fd5ae148f0ba0e78c4a471e Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Sat, 26 Mar 2016 13:30:46 +1100 Subject: [PATCH] Replaces CategoryForm with ProductsForm (makes the form slightly dumber) --- registrasion/forms.py | 33 +++++++++++++-------------------- registrasion/views.py | 18 +++++++++++------- 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/registrasion/forms.py b/registrasion/forms.py index 79e6d95a..fd0359bb 100644 --- a/registrasion/forms.py +++ b/registrasion/forms.py @@ -1,21 +1,26 @@ import models as rego -from controllers.product import ProductController - from django import forms -def CategoryForm(category): +def ProductsForm(products): PREFIX = "product_" def field_name(product): return PREFIX + ("%d" % product.id) - class _CategoryForm(forms.Form): + class _ProductsForm(forms.Form): - @staticmethod - def initial_data(product_quantities): + def __init__(self, *a, **k): + if "product_quantities" in k: + initial = _ProductsForm.initial_data(k["product_quantities"]) + k["initial"] = initial + del k["product_quantities"] + super(_ProductsForm, self).__init__(*a, **k) + + @classmethod + def initial_data(cls, product_quantities): ''' Prepares initial data for an instance of this form. product_quantities is a sequence of (product,quantity) tuples ''' initial = {} @@ -32,18 +37,6 @@ def CategoryForm(category): product_id = int(name[len(PREFIX):]) yield (product_id, value, name) - def disable_product(self, product): - ''' Removes a given product from this form. ''' - del self.fields[field_name(product)] - - def disable_products_for_user(self, user): - for product in products: - # Remove fields that do not have an enabling condition. - prod = ProductController(product) - if not prod.can_add_with_enabling_conditions(user, 0): - self.disable_product(product) - - products = rego.Product.objects.filter(category=category).order_by("order") for product in products: help_text = "$%d -- %s" % (product.price, product.description) @@ -52,9 +45,9 @@ def CategoryForm(category): label=product.name, help_text=help_text, ) - _CategoryForm.base_fields[field_name(product)] = field + _ProductsForm.base_fields[field_name(product)] = field - return _CategoryForm + return _ProductsForm class ProfileForm(forms.ModelForm): diff --git a/registrasion/views.py b/registrasion/views.py index 070f645a..da0eb657 100644 --- a/registrasion/views.py +++ b/registrasion/views.py @@ -2,6 +2,7 @@ from registrasion import forms from registrasion import models as rego from registrasion.controllers.cart import CartController from registrasion.controllers.invoice import InvoiceController +from registrasion.controllers.product import ProductController from django.contrib.auth.decorators import login_required from django.core.exceptions import ObjectDoesNotExist @@ -95,19 +96,21 @@ def product_category(request, category_id): category = rego.Category.objects.get(pk=category_id) current_cart = CartController.for_user(request.user) - CategoryForm = forms.CategoryForm(category) - attendee = rego.Attendee.get_instance(request.user) products = rego.Product.objects.filter(category=category) products = products.order_by("order") + products = ProductController.available_products( + request.user, + products=products, + ) + ProductsForm = forms.ProductsForm(products) if request.method == "POST": - cat_form = CategoryForm( + cat_form = ProductsForm( request.POST, request.FILES, prefix=PRODUCTS_FORM_PREFIX) - cat_form.disable_products_for_user(request.user) voucher_form = forms.VoucherForm( request.POST, prefix=VOUCHERS_FORM_PREFIX) @@ -165,9 +168,10 @@ def product_category(request, category_id): quantity = 0 quantities.append((product, quantity)) - initial = CategoryForm.initial_data(quantities) - cat_form = CategoryForm(prefix=PRODUCTS_FORM_PREFIX, initial=initial) - cat_form.disable_products_for_user(request.user) + cat_form = ProductsForm( + prefix=PRODUCTS_FORM_PREFIX, + product_quantities=quantities, + ) voucher_form = forms.VoucherForm(prefix=VOUCHERS_FORM_PREFIX)