Skip to content

Commit

Permalink
Refactor active block methods
Browse files Browse the repository at this point in the history
  • Loading branch information
rebkwok committed Jun 24, 2024
1 parent 7a92deb commit 32c2abf
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 25 deletions.
26 changes: 20 additions & 6 deletions booking/models/booking_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 3 additions & 3 deletions booking/views/booking_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions booking/views/shopping_basket_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
9 changes: 0 additions & 9 deletions booking/views/views_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 3 additions & 4 deletions studioadmin/views/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 32c2abf

Please sign in to comment.