Adds functionality to increase the reservation duration

This commit is contained in:
Christopher Neugebauer 2016-10-06 12:33:53 -07:00
parent b323c0eb25
commit d31d812001
2 changed files with 76 additions and 0 deletions

View file

@ -124,6 +124,32 @@ class CartController(object):
self.cart.revision += 1 self.cart.revision += 1
self.cart.save() self.cart.save()
def extend_reservation(self, timedelta):
''' Extends the reservation on this cart by the given timedelta.
This can only be done if the current state of the cart is valid (i.e
all items and discounts in the cart are still available.)
Arguments:
timedelta (timedelta): The amount of time to extend the cart by.
The resulting reservation_duration will be now() + timedelta,
unless the requested extension is *LESS* than the current
reservation deadline.
'''
self.validate_cart()
cart = self.cart
cart.refresh_from_db()
elapsed = (timezone.now() - cart.time_last_updated)
if cart.reservation_duration - elapsed > timedelta:
return
cart.time_last_updated = timezone.now()
cart.reservation_duration = timedelta
cart.save()
@_modifies_cart @_modifies_cart
def set_quantities(self, product_quantities): def set_quantities(self, product_quantities):
''' Sets the quantities on each of the products on each of the ''' Sets the quantities on each of the products on each of the

View file

@ -500,3 +500,53 @@ class BasicCartTests(RegistrationCartTestCase):
cart.add_to_cart(self.PROD_1, 1) cart.add_to_cart(self.PROD_1, 1)
cart.cart.refresh_from_db() cart.cart.refresh_from_db()
self.assertEqual(cart.cart.reservation_duration, self.RESERVATION) self.assertEqual(cart.cart.reservation_duration, self.RESERVATION)
def test_reservation_extension_less_than_current(self):
''' Reservation extension should have no effect if it's too small
'''
self.set_time(datetime.datetime(2015, 1, 1, tzinfo=UTC))
cart = TestingCartController.for_user(self.USER_1)
cart.add_to_cart(self.PROD_1, 1)
cart.cart.refresh_from_db()
self.assertEqual(cart.cart.reservation_duration, self.RESERVATION)
cart.extend_reservation(datetime.timedelta(minutes=30))
cart.cart.refresh_from_db()
self.assertEqual(cart.cart.reservation_duration, self.RESERVATION)
def test_reservation_extension(self):
''' Test various reservation extension bits.
'''
self.set_time(datetime.datetime(2015, 1, 1, tzinfo=UTC))
cart = TestingCartController.for_user(self.USER_1)
cart.add_to_cart(self.PROD_1, 1)
cart.cart.refresh_from_db()
self.assertEqual(cart.cart.reservation_duration, self.RESERVATION)
hours = datetime.timedelta(hours=1)
cart.extend_reservation(24 * hours)
cart.cart.refresh_from_db()
self.assertEqual(cart.cart.reservation_duration, 24 * hours)
self.add_timedelta(1 * hours)
# PROD_1's reservation is less than what we've added to the cart
cart.add_to_cart(self.PROD_1, 1)
cart.cart.refresh_from_db()
self.assertEqual(cart.cart.reservation_duration, 23 * hours)
# Now the extension should only have 59 minutes remaining
# so the autoextend behaviour should kick in
self.add_timedelta(datetime.timedelta(hours=22, minutes=1))
cart.add_to_cart(self.PROD_1, 1)
cart.cart.refresh_from_db()
self.assertEqual(
cart.cart.reservation_duration,
self.PROD_1.reservation_duration,
)