Cart reservation durations now take the residual from the last reservation duration into account.

This commit is contained in:
Christopher Neugebauer 2016-10-06 12:12:50 -07:00
parent 360175f86a
commit b323c0eb25
2 changed files with 50 additions and 2 deletions

View file

@ -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):

View file

@ -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)