diff --git a/booking/models/booking_models.py b/booking/models/booking_models.py index 5a0a846d..f394fac0 100644 --- a/booking/models/booking_models.py +++ b/booking/models/booking_models.py @@ -758,14 +758,28 @@ def can_cancel(self): return False return self.event.can_cancel + def get_next_active_block(self): + """ + return the active block for this booking with the soonest expiry date + """ + blocks = self.user.blocks.filter( + expiry_date__gte=timezone.now(), + block_type__event_type=self.event.event_type + ).order_by("expiry_date") + # already sorted by expiry date, so we can just get the next active one + return next( + (block for block in blocks if block.active_block()), None + ) + @property def has_available_block(self): - available_blocks = [ - block for block in - self.user.blocks.filter(block_type__event_type=self.event.event_type) - if block.active_block() - ] - return bool(available_blocks) + return any( + [ + block for block in + self.user.blocks.filter(block_type__event_type=self.event.event_type, expiry_date__gte=timezone.now()) + if block.active_block() + ] + ) @cached_property def has_unpaid_block(self): diff --git a/booking/views/booking_views.py b/booking/views/booking_views.py index f00b4b79..2220286f 100644 --- a/booking/views/booking_views.py +++ b/booking/views/booking_views.py @@ -43,7 +43,7 @@ from booking.views.shopping_basket_views import shopping_basket_bookings_total_context from booking.views.views_utils import DisclaimerRequiredMixin, \ DataPolicyAgreementRequiredMixin, \ - _get_active_user_block, _get_block_status, validate_voucher_code + _get_block_status, validate_voucher_code from booking.templatetags.bookingtags import format_paid_status, get_shopping_basket_icon @@ -282,7 +282,7 @@ def form_valid(self, form): _email_free_class_request(self.request, booking, 'update') elif 'block_book' in form.data: - active_block = _get_active_user_block(self.request.user, booking) + active_block = booking.get_next_active_block() if active_block: booking.block = active_block booking.paid = True @@ -876,7 +876,7 @@ def ajax_create_booking(request, event_id): # leave paid no_show booking with existing payment method pass - active_block = _get_active_user_block(request.user, booking) + active_block = booking.get_next_active_block() if active_block: booking.block = active_block diff --git a/booking/views/shopping_basket_views.py b/booking/views/shopping_basket_views.py index 90b4ec33..a57f560b 100644 --- a/booking/views/shopping_basket_views.py +++ b/booking/views/shopping_basket_views.py @@ -25,8 +25,7 @@ ) from booking.forms import BookingVoucherForm, BlockVoucherForm import booking.context_helpers as context_helpers -from booking.views.views_utils import _get_active_user_block, \ - _get_block_status, validate_block_voucher_code, validate_voucher_code +from booking.views.views_utils import _get_block_status, validate_block_voucher_code, validate_voucher_code from payments.helpers import ( create_booking_paypal_transaction, create_multiblock_paypal_transaction, @@ -525,7 +524,7 @@ def update_block_bookings(request): block_booked = [] for booking in unpaid_bookings: - active_block = _get_active_user_block(request.user, booking) + active_block = booking.get_next_active_block() if active_block: booking.block = active_block booking.paid = True diff --git a/booking/views/views_utils.py b/booking/views/views_utils.py index 98f3483d..ea2d0272 100644 --- a/booking/views/views_utils.py +++ b/booking/views/views_utils.py @@ -101,12 +101,3 @@ def _get_block_status(booking, request): return blocks_used, total_blocks -def _get_active_user_block(user, booking): - """ - return the active block for this booking with the soonest expiry date - """ - blocks = user.blocks.filter(expiry_date__gte=timezone.now()).order_by("expiry_date") - # already sorted by expiry date, so we can just get the next active one - next_active_block = next((block for block in blocks if block.active_block() and block.block_type.event_type == booking.event.event_type), None) - # use the block with the soonest expiry date - return next_active_block diff --git a/studioadmin/views/register.py b/studioadmin/views/register.py index b7e9c660..d855963e 100644 --- a/studioadmin/views/register.py +++ b/studioadmin/views/register.py @@ -21,7 +21,6 @@ from booking.email_helpers import send_waiting_list_email from booking.models import Event, Booking, Block, BlockType, WaitingListUser -from booking.views.views_utils import _get_active_user_block from studioadmin.forms import StatusFilter, RegisterDayForm, AddRegisterBookingForm from studioadmin.views.helpers import is_instructor_or_staff, \ InstructorOrStaffUserMixin @@ -326,8 +325,8 @@ def process_event_booking_updates(form, event, request): action = 'reopened' if not booking.block: # reopened no-show could already have block - active_block = _get_active_user_block(booking.user, booking) - if booking.has_available_block: + active_block = booking.get_next_active_block() + if active_block is not None: booking.block = active_block booking.paid = True booking.payment_confirmed = True @@ -382,7 +381,7 @@ def ajax_assign_block(request, booking_id): 'status': 'warning', 'msg': 'Block already assigned.' } else: - available_block = _get_active_user_block(booking.user, booking) + available_block = booking.get_next_active_block() if available_block: booking.block = available_block booking.paid = True