From 3562772c13cc0583cbdf23d4cda682e78cccb383 Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Sun, 27 Mar 2016 11:18:26 +1100 Subject: [PATCH] Adds RadioBoxProductsForm --- registrasion/forms.py | 64 ++++++++++++++++++++++++++++++++++++++----- registrasion/views.py | 2 +- 2 files changed, 58 insertions(+), 8 deletions(-) diff --git a/registrasion/forms.py b/registrasion/forms.py index 68ed9041..cc4b1b4e 100644 --- a/registrasion/forms.py +++ b/registrasion/forms.py @@ -23,7 +23,7 @@ class _ProductsForm(forms.Form): return cls.PRODUCT_PREFIX + ("%d" % product.id) @classmethod - def set_fields(cls, products): + def set_fields(cls, category, products): ''' Sets the base_fields on this _ProductsForm to allow selecting from the provided products. ''' pass @@ -45,7 +45,7 @@ class _QuantityBoxProductsForm(_ProductsForm): of desired products. ''' @classmethod - def set_fields(cls, products): + def set_fields(cls, category, products): for product in products: help_text = "$%d -- %s" % (product.price, product.description) @@ -70,15 +70,65 @@ class _QuantityBoxProductsForm(_ProductsForm): yield (product_id, value, name) -def ProductsForm(products): +class _RadioButtonProductsForm(_ProductsForm): + ''' Products entry form that allows users to enter quantities + of desired products. ''' + + FIELD = "chosen_product" + + @classmethod + def set_fields(cls, category, products): + choices = [] + for product in products: + + choice_text = "%s -- $%d" % (product.name, product.price) + choices.append((product.id, choice_text)) + + cls.base_fields[cls.FIELD] = forms.TypedChoiceField( + label=category.name, + widget=forms.RadioSelect, + choices=choices, + empty_value=0, + coerce=int, + ) + + @classmethod + def initial_data(cls, product_quantities): + initial = {} + + for product, quantity in product_quantities: + if quantity > 0: + initial[cls.FIELD] = product.id + break + + return initial + + def product_quantities(self): + ours = self.cleaned_data[self.FIELD] + choices = self.fields[self.FIELD].choices + for choice_value, choice_display in choices: + yield ( + choice_value, + 1 if ours == choice_value else 0, + self.FIELD, + ) + + +def ProductsForm(category, products): ''' Produces an appropriate _ProductsForm subclass for the given render type. ''' - if True: - class ProductsForm(_QuantityBoxProductsForm): - pass + # Each Category.RENDER_TYPE value has a subclass here. + RENDER_TYPES = { + rego.Category.RENDER_TYPE_QUANTITY : _QuantityBoxProductsForm, + rego.Category.RENDER_TYPE_RADIO : _RadioButtonProductsForm, + } - ProductsForm.set_fields(products) + # Produce a subclass of _ProductsForm which we can alter the base_fields on + class ProductsForm(RENDER_TYPES[category.render_type]): + pass + + ProductsForm.set_fields(category, products) return ProductsForm diff --git a/registrasion/views.py b/registrasion/views.py index 04c63ae2..6d42b2b3 100644 --- a/registrasion/views.py +++ b/registrasion/views.py @@ -136,7 +136,7 @@ def handle_products(request, category, products, prefix): current_cart = CartController.for_user(request.user) - ProductsForm = forms.ProductsForm(products) + ProductsForm = forms.ProductsForm(category, products) # Create initial data for each of products in category items = rego.ProductItem.objects.filter(