diff --git a/registrasion/forms.py b/registrasion/forms.py index 54b3115a..5027e068 100644 --- a/registrasion/forms.py +++ b/registrasion/forms.py @@ -1,5 +1,7 @@ import models as rego +from controllers.product import ProductController + from django import forms @@ -34,6 +36,14 @@ def CategoryForm(category): ''' 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: diff --git a/registrasion/views.py b/registrasion/views.py index 3957da7d..f7d0f9c5 100644 --- a/registrasion/views.py +++ b/registrasion/views.py @@ -30,6 +30,7 @@ def product_category(request, category_id): if request.method == "POST": 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) if voucher_form.is_valid(): @@ -75,15 +76,10 @@ def product_category(request, category_id): initial = CategoryForm.initial_data(quantities) 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) - 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 = { "category": category,