Adds RadioBoxProductsForm
This commit is contained in:
parent
0ae005a5f5
commit
3562772c13
2 changed files with 58 additions and 8 deletions
|
@ -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
|
||||
|
||||
|
||||
|
|
|
@ -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(
|
||||
|
|
Loading…
Reference in a new issue