diff --git a/registrasion/controllers/cart.py b/registrasion/controllers/cart.py index ff31bb95..5caa4ccb 100644 --- a/registrasion/controllers/cart.py +++ b/registrasion/controllers/cart.py @@ -76,7 +76,14 @@ class CartController(object): determine whether the cart has reserved the items and discounts it holds. ''' - reservations = [datetime.timedelta()] + time = timezone.now() + + # Calculate the residual of the _old_ reservation duration + # if it's greater than what's in the cart now, keep it. + time_elapsed_since_updated = (time - self.cart.time_last_updated) + residual = self.cart.reservation_duration - time_elapsed_since_updated + + reservations = [datetime.timedelta(0), residual] # If we have vouchers, we're entitled to an hour at minimum. if len(self.cart.vouchers.all()) >= 1: @@ -90,7 +97,7 @@ class CartController(object): if product_max is not None: reservations.append(product_max) - self.cart.time_last_updated = timezone.now() + self.cart.time_last_updated = time self.cart.reservation_duration = max(reservations) def end_batch(self): diff --git a/registrasion/tests/test_cart.py b/registrasion/tests/test_cart.py index d6a4a612..1bfdba43 100644 --- a/registrasion/tests/test_cart.py +++ b/registrasion/tests/test_cart.py @@ -459,3 +459,44 @@ class BasicCartTests(RegistrationCartTestCase): cart.add_to_cart(self.PROD_1, 1) cart.cart.refresh_from_db() self.assertEqual(cart.cart.reservation_duration, new_res) + + + def test_reservation_duration_removals(self): + ''' Reservation duration should update with removals + ''' + + new_res = self.RESERVATION * 2 + self.PROD_2.reservation_duration = new_res + self.PROD_2.save() + + self.set_time(datetime.datetime(2015, 1, 1, tzinfo=UTC)) + cart = TestingCartController.for_user(self.USER_1) + + one_third = new_res / 3 + + cart.add_to_cart(self.PROD_2, 1) + cart.cart.refresh_from_db() + self.assertEqual(cart.cart.reservation_duration, new_res) + + # Reservation duration should not decrease if time hasn't decreased + cart.set_quantity(self.PROD_2, 0) + cart.cart.refresh_from_db() + self.assertEqual(cart.cart.reservation_duration, new_res) + + # Adding a new product should not reset the reservation duration below + # the old one + cart.add_to_cart(self.PROD_1, 1) + cart.cart.refresh_from_db() + self.assertEqual(cart.cart.reservation_duration, new_res) + + self.add_timedelta(one_third) + + # The old reservation duration is still longer than PROD_1's + cart.add_to_cart(self.PROD_1, 1) + cart.cart.refresh_from_db() + self.assertEqual(cart.cart.reservation_duration, new_res - one_third) + + self.add_timedelta(one_third) + cart.add_to_cart(self.PROD_1, 1) + cart.cart.refresh_from_db() + self.assertEqual(cart.cart.reservation_duration, self.RESERVATION)