2016-03-03 21:40:44 +00:00
|
|
|
import models as rego
|
|
|
|
|
2016-03-23 08:36:22 +00:00
|
|
|
from controllers.product import ProductController
|
|
|
|
|
2016-03-03 21:40:44 +00:00
|
|
|
from django import forms
|
|
|
|
|
|
|
|
|
2016-03-05 02:01:16 +00:00
|
|
|
def CategoryForm(category):
|
|
|
|
|
|
|
|
PREFIX = "product_"
|
|
|
|
|
|
|
|
def field_name(product):
|
|
|
|
return PREFIX + ("%d" % product.id)
|
|
|
|
|
|
|
|
class _CategoryForm(forms.Form):
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def initial_data(product_quantities):
|
|
|
|
''' Prepares initial data for an instance of this form.
|
|
|
|
product_quantities is a sequence of (product,quantity) tuples '''
|
|
|
|
initial = {}
|
|
|
|
for product, quantity in product_quantities:
|
|
|
|
initial[field_name(product)] = quantity
|
|
|
|
|
|
|
|
return initial
|
|
|
|
|
|
|
|
def product_quantities(self):
|
|
|
|
''' Yields a sequence of (product, quantity) tuples from the
|
|
|
|
cleaned form data. '''
|
|
|
|
for name, value in self.cleaned_data.items():
|
|
|
|
if name.startswith(PREFIX):
|
|
|
|
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)]
|
|
|
|
|
2016-03-23 08:36:22 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
2016-03-05 02:01:16 +00:00
|
|
|
products = rego.Product.objects.filter(category=category).order_by("order")
|
|
|
|
for product in products:
|
|
|
|
|
|
|
|
help_text = "$%d -- %s" % (product.price, product.description)
|
|
|
|
|
|
|
|
field = forms.IntegerField(
|
|
|
|
label=product.name,
|
|
|
|
help_text=help_text,
|
|
|
|
)
|
|
|
|
_CategoryForm.base_fields[field_name(product)] = field
|
|
|
|
|
|
|
|
return _CategoryForm
|
2016-03-23 02:33:33 +00:00
|
|
|
|
|
|
|
class VoucherForm(forms.Form):
|
|
|
|
voucher = forms.CharField(
|
|
|
|
label="Voucher code",
|
|
|
|
help_text="If you have a voucher code, enter it here",
|
2016-03-23 08:36:54 +00:00
|
|
|
required=False,
|
2016-03-23 02:33:33 +00:00
|
|
|
)
|