Makes it invalid for a user to re-enter a voucher code they already have.
This commit is contained in:
		
							parent
							
								
									940bf803b6
								
							
						
					
					
						commit
						2d5cd622c5
					
				
					 2 changed files with 30 additions and 4 deletions
				
			
		|  | @ -125,18 +125,25 @@ class CartController(object): | ||||||
|     def apply_voucher(self, voucher_code): |     def apply_voucher(self, voucher_code): | ||||||
|         ''' Applies the voucher with the given code to this cart. ''' |         ''' Applies the voucher with the given code to this cart. ''' | ||||||
| 
 | 
 | ||||||
|         # TODO: is it valid for a cart to re-add a voucher that they have? |  | ||||||
| 
 |  | ||||||
|         # Is voucher exhausted? |         # Is voucher exhausted? | ||||||
|         active_carts = rego.Cart.reserved_carts() |         active_carts = rego.Cart.reserved_carts() | ||||||
| 
 | 
 | ||||||
|         # Try and find the voucher |         # Try and find the voucher | ||||||
|         voucher = rego.Voucher.objects.get(code=voucher_code.upper()) |         voucher = rego.Voucher.objects.get(code=voucher_code.upper()) | ||||||
| 
 | 
 | ||||||
|  |         # It's invalid for a user to enter a voucher that's exhausted | ||||||
|         carts_with_voucher = active_carts.filter(vouchers=voucher) |         carts_with_voucher = active_carts.filter(vouchers=voucher) | ||||||
|         if len(carts_with_voucher) >= voucher.limit: |         if len(carts_with_voucher) >= voucher.limit: | ||||||
|             raise ValidationError("This voucher is no longer available") |             raise ValidationError("This voucher is no longer available") | ||||||
| 
 | 
 | ||||||
|  |         # It's not valid for users to re-enter a voucher they already have | ||||||
|  |         user_carts_with_voucher = rego.Cart.objects.filter( | ||||||
|  |             user=self.cart.user, | ||||||
|  |             vouchers=voucher, | ||||||
|  |         ) | ||||||
|  |         if len(user_carts_with_voucher) > 0: | ||||||
|  |             raise ValidationError("You have already entered this voucher.") | ||||||
|  | 
 | ||||||
|         # If successful... |         # If successful... | ||||||
|         self.cart.vouchers.add(voucher) |         self.cart.vouchers.add(voucher) | ||||||
|         self.end_batch() |         self.end_batch() | ||||||
|  |  | ||||||
|  | @ -7,6 +7,7 @@ from django.db import IntegrityError | ||||||
| 
 | 
 | ||||||
| from registrasion import models as rego | from registrasion import models as rego | ||||||
| from registrasion.controllers.cart import CartController | from registrasion.controllers.cart import CartController | ||||||
|  | from registrasion.controllers.invoice import InvoiceController | ||||||
| 
 | 
 | ||||||
| from test_cart import RegistrationCartTestCase | from test_cart import RegistrationCartTestCase | ||||||
| 
 | 
 | ||||||
|  | @ -16,11 +17,11 @@ UTC = pytz.timezone('UTC') | ||||||
| class VoucherTestCases(RegistrationCartTestCase): | class VoucherTestCases(RegistrationCartTestCase): | ||||||
| 
 | 
 | ||||||
|     @classmethod |     @classmethod | ||||||
|     def new_voucher(self, code="VOUCHER"): |     def new_voucher(self, code="VOUCHER", limit=1): | ||||||
|         voucher = rego.Voucher.objects.create( |         voucher = rego.Voucher.objects.create( | ||||||
|             recipient="Voucher recipient", |             recipient="Voucher recipient", | ||||||
|             code=code, |             code=code, | ||||||
|             limit=1 |             limit=limit, | ||||||
|         ) |         ) | ||||||
|         voucher.save() |         voucher.save() | ||||||
|         return voucher |         return voucher | ||||||
|  | @ -107,3 +108,21 @@ class VoucherTestCases(RegistrationCartTestCase): | ||||||
|         voucher = self.new_voucher(code="VOUCHeR") |         voucher = self.new_voucher(code="VOUCHeR") | ||||||
|         current_cart = CartController.for_user(self.USER_1) |         current_cart = CartController.for_user(self.USER_1) | ||||||
|         current_cart.apply_voucher(voucher.code.lower()) |         current_cart.apply_voucher(voucher.code.lower()) | ||||||
|  | 
 | ||||||
|  |     def test_voucher_can_only_be_applied_once(self): | ||||||
|  |         voucher = self.new_voucher(limit=2) | ||||||
|  |         current_cart = CartController.for_user(self.USER_1) | ||||||
|  |         current_cart.apply_voucher(voucher.code) | ||||||
|  |         with self.assertRaises(ValidationError): | ||||||
|  |             current_cart.apply_voucher(voucher.code) | ||||||
|  | 
 | ||||||
|  |     def test_voucher_can_only_be_applied_once_across_multiple_carts(self): | ||||||
|  |         voucher = self.new_voucher(limit=2) | ||||||
|  |         current_cart = CartController.for_user(self.USER_1) | ||||||
|  |         current_cart.apply_voucher(voucher.code) | ||||||
|  | 
 | ||||||
|  |         inv = InvoiceController.for_cart(current_cart.cart) | ||||||
|  |         inv.pay("Hello!", inv.invoice.value) | ||||||
|  | 
 | ||||||
|  |         with self.assertRaises(ValidationError): | ||||||
|  |             current_cart.apply_voucher(voucher.code) | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue
	
	 Christopher Neugebauer
						Christopher Neugebauer