Changeset - b13e6f7ce2c2
[Not reviewed]
0 2 0
Christopher Neugebauer - 8 years ago 2016-03-26 09:01:46
chrisjrn@gmail.com
Factors out voucher form handling into its own function
2 files changed with 43 insertions and 16 deletions:
0 comments (0 inline, 0 general)
registrasion/models.py
Show inline comments
...
 
@@ -174,5 +174,9 @@ class Voucher(models.Model):
 

	
 
    @classmethod
 
    def normalise_code(cls, code):
 
        return code.upper()
 

	
 
    def save(self, *a, **k):
 
        ''' Normalise the voucher code to be uppercase '''
 
        self.code = self.code.upper()
 
        self.code = self.normalise_code(self.code)
 
        super(Voucher, self).save(*a, **k)
registrasion/views.py
Show inline comments
...
 
@@ -101,2 +101,9 @@ def product_category(request, category_id):
 

	
 
    # Handle the voucher form *before* listing products.
 
    v = handle_voucher(request, VOUCHERS_FORM_PREFIX)
 
    voucher_form, voucher_handled = v
 
    if voucher_handled:
 
        # Do not handle product form
 
        pass
 

	
 
    products = rego.Product.objects.filter(category=category)
...
 
@@ -107,2 +114,3 @@ def product_category(request, category_id):
 
    )
 

	
 
    ProductsForm = forms.ProductsForm(products)
...
 
@@ -114,16 +122,6 @@ def product_category(request, category_id):
 
            prefix=PRODUCTS_FORM_PREFIX)
 
        voucher_form = forms.VoucherForm(
 
            request.POST,
 
            prefix=VOUCHERS_FORM_PREFIX)
 

	
 
        if (voucher_form.is_valid() and
 
                voucher_form.cleaned_data["voucher"].strip()):
 
            # Apply voucher
 
            # leave
 
            voucher = voucher_form.cleaned_data["voucher"]
 
            try:
 
                current_cart.apply_voucher(voucher)
 
            except Exception as e:
 
                voucher_form.add_error("voucher", e)
 
            # Re-visit current page.
 
        if voucher_handled:
 
            # The voucher form was handled here.
 
            pass
 
        elif cat_form.is_valid():
...
 
@@ -176,4 +174,2 @@ def product_category(request, category_id):
 

	
 
        voucher_form = forms.VoucherForm(prefix=VOUCHERS_FORM_PREFIX)
 

	
 
    discounts = discount.available_discounts(request.user, [], products)
...
 
@@ -201,2 +197,29 @@ def handle_valid_cat_form(cat_form, current_cart):
 

	
 
def handle_voucher(request, prefix):
 
    ''' Handles a voucher form in the given request. Returns the voucher
 
    form instance, and whether the voucher code was handled. '''
 

	
 
    voucher_form = forms.VoucherForm(request.POST or None, prefix=prefix)
 
    current_cart = CartController.for_user(request.user)
 

	
 
    if (voucher_form.is_valid() and
 
            voucher_form.cleaned_data["voucher"].strip()):
 

	
 
        voucher = voucher_form.cleaned_data["voucher"]
 
        voucher = rego.Voucher.normalise_code(voucher)
 

	
 
        if len(current_cart.cart.vouchers.filter(code=voucher)) > 0:
 
            # This voucher has already been applied to this cart.
 
            # Do not apply code
 
            handled = False
 
        else:
 
            try:
 
                current_cart.apply_voucher(voucher)
 
            except Exception as e:
 
                voucher_form.add_error("voucher", e)
 
            handled = True
 
    else:
 
        handled = False
 

	
 
    return (voucher_form, handled)
 

	
0 comments (0 inline, 0 general)