Moves product disabling code into the form class

This commit is contained in:
Christopher Neugebauer 2016-03-23 19:36:22 +11:00
parent 2f4ebc22af
commit 7086ea8729
2 changed files with 12 additions and 6 deletions

View file

@ -1,5 +1,7 @@
import models as rego import models as rego
from controllers.product import ProductController
from django import forms from django import forms
@ -34,6 +36,14 @@ def CategoryForm(category):
''' Removes a given product from this form. ''' ''' Removes a given product from this form. '''
del self.fields[field_name(product)] 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") products = rego.Product.objects.filter(category=category).order_by("order")
for product in products: for product in products:

View file

@ -30,6 +30,7 @@ def product_category(request, category_id):
if request.method == "POST": if request.method == "POST":
cat_form = CategoryForm(request.POST, request.FILES, prefix=PRODUCTS_FORM_PREFIX) cat_form = CategoryForm(request.POST, request.FILES, prefix=PRODUCTS_FORM_PREFIX)
cat_form.disable_products_for_user(request.user)
voucher_form = forms.VoucherForm(request.POST, prefix=VOUCHERS_FORM_PREFIX) voucher_form = forms.VoucherForm(request.POST, prefix=VOUCHERS_FORM_PREFIX)
if voucher_form.is_valid(): if voucher_form.is_valid():
@ -75,15 +76,10 @@ def product_category(request, category_id):
initial = CategoryForm.initial_data(quantities) initial = CategoryForm.initial_data(quantities)
cat_form = CategoryForm(prefix=PRODUCTS_FORM_PREFIX, initial=initial) cat_form = CategoryForm(prefix=PRODUCTS_FORM_PREFIX, initial=initial)
cat_form.disable_products_for_user(request.user)
voucher_form = forms.VoucherForm(prefix=VOUCHERS_FORM_PREFIX) voucher_form = forms.VoucherForm(prefix=VOUCHERS_FORM_PREFIX)
for product in products:
# Remove fields that do not have an enabling condition.
prod = ProductController(product)
if not prod.can_add_with_enabling_conditions(request.user, 0):
cat_form.disable_product(product)
data = { data = {
"category": category, "category": category,