From 7086ea87292dc5ac8c5d101b998599493163e202 Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Wed, 23 Mar 2016 19:36:22 +1100 Subject: [PATCH] Moves product disabling code into the form class --- registrasion/forms.py | 10 ++++++++++ registrasion/views.py | 8 ++------ 2 files changed, 12 insertions(+), 6 deletions(-) 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,