Replaces CategoryForm with ProductsForm (makes the form slightly dumber)

This commit is contained in:
Christopher Neugebauer 2016-03-26 13:30:46 +11:00
parent 45aa83f854
commit fc279b1922
2 changed files with 24 additions and 27 deletions

View file

@ -1,21 +1,26 @@
import models as rego import models as rego
from controllers.product import ProductController
from django import forms from django import forms
def CategoryForm(category): def ProductsForm(products):
PREFIX = "product_" PREFIX = "product_"
def field_name(product): def field_name(product):
return PREFIX + ("%d" % product.id) return PREFIX + ("%d" % product.id)
class _CategoryForm(forms.Form): class _ProductsForm(forms.Form):
@staticmethod def __init__(self, *a, **k):
def initial_data(product_quantities): 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. ''' Prepares initial data for an instance of this form.
product_quantities is a sequence of (product,quantity) tuples ''' product_quantities is a sequence of (product,quantity) tuples '''
initial = {} initial = {}
@ -32,18 +37,6 @@ def CategoryForm(category):
product_id = int(name[len(PREFIX):]) product_id = int(name[len(PREFIX):])
yield (product_id, value, name) 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: for product in products:
help_text = "$%d -- %s" % (product.price, product.description) help_text = "$%d -- %s" % (product.price, product.description)
@ -52,9 +45,9 @@ def CategoryForm(category):
label=product.name, label=product.name,
help_text=help_text, 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): class ProfileForm(forms.ModelForm):

View file

@ -2,6 +2,7 @@ from registrasion import forms
from registrasion import models as rego from registrasion import models as rego
from registrasion.controllers.cart import CartController from registrasion.controllers.cart import CartController
from registrasion.controllers.invoice import InvoiceController from registrasion.controllers.invoice import InvoiceController
from registrasion.controllers.product import ProductController
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
@ -95,19 +96,21 @@ def product_category(request, category_id):
category = rego.Category.objects.get(pk=category_id) category = rego.Category.objects.get(pk=category_id)
current_cart = CartController.for_user(request.user) current_cart = CartController.for_user(request.user)
CategoryForm = forms.CategoryForm(category)
attendee = rego.Attendee.get_instance(request.user) attendee = rego.Attendee.get_instance(request.user)
products = rego.Product.objects.filter(category=category) products = rego.Product.objects.filter(category=category)
products = products.order_by("order") products = products.order_by("order")
products = ProductController.available_products(
request.user,
products=products,
)
ProductsForm = forms.ProductsForm(products)
if request.method == "POST": if request.method == "POST":
cat_form = CategoryForm( cat_form = ProductsForm(
request.POST, request.POST,
request.FILES, request.FILES,
prefix=PRODUCTS_FORM_PREFIX) prefix=PRODUCTS_FORM_PREFIX)
cat_form.disable_products_for_user(request.user)
voucher_form = forms.VoucherForm( voucher_form = forms.VoucherForm(
request.POST, request.POST,
prefix=VOUCHERS_FORM_PREFIX) prefix=VOUCHERS_FORM_PREFIX)
@ -165,9 +168,10 @@ def product_category(request, category_id):
quantity = 0 quantity = 0
quantities.append((product, quantity)) quantities.append((product, quantity))
initial = CategoryForm.initial_data(quantities) cat_form = ProductsForm(
cat_form = CategoryForm(prefix=PRODUCTS_FORM_PREFIX, initial=initial) prefix=PRODUCTS_FORM_PREFIX,
cat_form.disable_products_for_user(request.user) product_quantities=quantities,
)
voucher_form = forms.VoucherForm(prefix=VOUCHERS_FORM_PREFIX) voucher_form = forms.VoucherForm(prefix=VOUCHERS_FORM_PREFIX)