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
...
 
@@ -173,7 +173,11 @@ class Voucher(models.Model):
 
        return "Voucher for %s" % self.recipient
 

	
 
    @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
...
 
@@ -100,4 +100,11 @@ def product_category(request, category_id):
 
    attendee = rego.Attendee.get_instance(request.user)
 

	
 
    # 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)
 
    products = products.order_by("order")
...
 
@@ -106,4 +113,5 @@ def product_category(request, category_id):
 
        products=products,
 
    )
 

	
 
    ProductsForm = forms.ProductsForm(products)
 

	
...
 
@@ -113,18 +121,8 @@ def product_category(request, category_id):
 
            request.FILES,
 
            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():
 
            try:
...
 
@@ -175,6 +173,4 @@ def product_category(request, category_id):
 
        )
 

	
 
        voucher_form = forms.VoucherForm(prefix=VOUCHERS_FORM_PREFIX)
 

	
 
    discounts = discount.available_discounts(request.user, [], products)
 
    data = {
...
 
@@ -200,4 +196,31 @@ def handle_valid_cat_form(cat_form, current_cart):
 
    current_cart.end_batch()
 

	
 
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)
 

	
 
@login_required
0 comments (0 inline, 0 general)