From 6f28c20b706f27feb665bdafc0dd99bce2b639a2 Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Wed, 6 Apr 2016 12:09:16 +1000 Subject: [PATCH] Factors _test_voucher() method into CartController --- registrasion/controllers/cart.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/registrasion/controllers/cart.py b/registrasion/controllers/cart.py index 6fef142f..339d7ba1 100644 --- a/registrasion/controllers/cart.py +++ b/registrasion/controllers/cart.py @@ -175,16 +175,26 @@ class CartController(object): def apply_voucher(self, voucher_code): ''' Applies the voucher with the given code to this cart. ''' - # Is voucher exhausted? - active_carts = rego.Cart.reserved_carts() - # Try and find the voucher voucher = rego.Voucher.objects.get(code=voucher_code.upper()) + self._test_voucher(voucher) + + # If successful... + self.cart.vouchers.add(voucher) + self.end_batch() + + def _test_voucher(self, voucher): + ''' Tests whether this voucher is allowed to be applied to this cart. + Raises ValidationError if not. ''' + + # Is voucher exhausted? + active_carts = rego.Cart.reserved_carts() + # It's invalid for a user to enter a voucher that's exhausted carts_with_voucher = active_carts.filter(vouchers=voucher) if len(carts_with_voucher) >= voucher.limit: - raise ValidationError("This voucher is no longer available") + raise ValidationError("Voucher %s is no longer available" % voucher.code) # It's not valid for users to re-enter a voucher they already have user_carts_with_voucher = rego.Cart.objects.filter( @@ -195,9 +205,6 @@ class CartController(object): if len(user_carts_with_voucher) > 0: raise ValidationError("You have already entered this voucher.") - # If successful... - self.cart.vouchers.add(voucher) - self.end_batch() def validate_cart(self): ''' Determines whether the status of the current cart is valid;