@@ -53,12 +53,13 @@ def ProductsForm(category, products):
type. '''
# Each Category.RENDER_TYPE value has a subclass here.
RENDER_TYPES = {
inventory.Category.RENDER_TYPE_QUANTITY: _QuantityBoxProductsForm,
inventory.Category.RENDER_TYPE_RADIO: _RadioButtonProductsForm,
inventory.Category.RENDER_TYPE_ITEM_QUANTITY: _ItemQuantityProductsForm,
}
# Produce a subclass of _ProductsForm which we can alter the base_fields on
class ProductsForm(RENDER_TYPES[category.render_type]):
pass
@@ -149,13 +150,12 @@ class _RadioButtonProductsForm(_ProductsForm):
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))
if not category.required:
choices.append((0, "No selection"))
@@ -187,12 +187,69 @@ class _RadioButtonProductsForm(_ProductsForm):
yield (
choice_value,
1 if ours == choice_value else 0,
self.FIELD,
)
class _ItemQuantityProductsForm(_ProductsForm):
''' Products entry form that allows users to select a product type, and
enter a quantity of that product. This version _only_ allows a specific
product type to be purchased.'''
CHOICE_FIELD = "choice"
QUANTITY_FIELD = "quantity"
choice_text = "%s -- $%d each" % (product.name, product.price)
cls.base_fields[cls.CHOICE_FIELD] = forms.TypedChoiceField(
label=category.name,
widget=forms.Select,
choices=choices,
empty_value=0,
coerce=int,
cls.base_fields[cls.QUANTITY_FIELD] = forms.IntegerField(
label="Quantity", # TODO: internationalise
min_value=0,
max_value=500, # Issue #19. We should figure out real limit.
def initial_data(cls, product_quantities):
initial = {}
for product, quantity in product_quantities:
if quantity > 0:
initial[cls.CHOICE_FIELD] = product.id
initial[cls.QUANTITY_FIELD] = quantity
break
return initial
def product_quantities(self):
our_choice = self.cleaned_data[self.CHOICE_FIELD]
our_quantity = self.cleaned_data[self.QUANTITY_FIELD]
choices = self.fields[self.CHOICE_FIELD].choices
for choice_value, choice_display in choices:
if choice_value == 0:
continue
our_quantity if our_choice == choice_value else 0,
self.CHOICE_FIELD,
class VoucherForm(forms.Form):
voucher = forms.CharField(
label="Voucher code",
help_text="If you have a voucher code, enter it here",
required=False,