Skip to content

Commit

Permalink
Turn off autocancellation
Browse files Browse the repository at this point in the history
  • Loading branch information
rebkwok committed Sep 18, 2023
1 parent 73319f3 commit c34e7b8
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 24 deletions.
33 changes: 17 additions & 16 deletions booking/context_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,21 @@ def get_event_context(context, event, user):
context['payment_text'] = payment_text

# booked flag
user_bookings = Booking.objects.filter(
event=event, user=user, status='OPEN', no_show=False
)
user_cancelled = Booking.objects.filter(
event=event, user=user, status='CANCELLED'
).exists()
auto_cancelled = Booking.objects.filter(
event=event, user=user, status='CANCELLED', auto_cancelled=True
).exists()
user_no_show = Booking.objects.filter(
event=event, user=user, status='OPEN', no_show=True
).exists()
booked = bool(user_bookings)
cancelled = user_cancelled or user_no_show
any_user_booking = user.bookings.filter(event=event).first()
if any_user_booking:
user_booking = (
any_user_booking if any_user_booking.status == "OPEN" and not any_user_booking.no_show
else None
)
user_cancelled = any_user_booking.status == "CANCELLED"
auto_cancelled = user_cancelled and any_user_booking.auto_cancelled
user_no_show = any_user_booking.status == "OPEN" and any_user_booking.no_show
cancelled = user_cancelled or user_no_show

else:
user_booking = None
cancelled = auto_cancelled = False


# waiting_list flag
context['on_waiting_list'] = WaitingListUser.objects.filter(
Expand All @@ -87,9 +88,9 @@ def get_event_context(context, event, user):
context["online_class"] = True
context["show_video_link"] = event.show_video_link

if booked:
if user_booking is not None:
context['bookable'] = False
context['booking'] = user_bookings[0]
context['booking'] = user_booking
context['booked'] = True
if event.event_type.event_type == 'OT':
if context['booking'].paid:
Expand Down
3 changes: 2 additions & 1 deletion booking/management/commands/cancel_unpaid_bookings.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ def cancel_bookings(self, now):
fail_silently=False)
booking.status = 'CANCELLED'
booking.block = None
booking.auto_cancelled = True
if settings.ENFORCE_AUTO_CANCELLATION:
booking.auto_cancelled = True
booking.save()
ActivityLog.objects.create(
log='Unpaid booking id {} for event {}, user {} '
Expand Down
22 changes: 22 additions & 0 deletions booking/tests/test_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,28 @@ def test_cancel_unpaid_bookings(self, mock_tz):
self.assertTrue(unpaid_booking.auto_cancelled)
self.assertFalse(paid_booking.auto_cancelled)

@patch('booking.management.commands.cancel_unpaid_bookings.timezone')
def test_cancel_unpaid_bookings_no_autocanceling(self, mock_tz):
"""
test unpaid bookings are cancelled
"""
mock_tz.now.return_value = datetime(
2015, 2, 10, 10, tzinfo=dt_timezone.utc
)
with override_settings(ENFORCE_AUTO_CANCELLATION=False):
management.call_command('cancel_unpaid_bookings')
# emails are sent to user per cancelled booking and studio once for all
# cancelled bookings
unpaid_booking = Booking.objects.get(id=self.unpaid.id)
paid_booking = Booking.objects.get(id=self.paid.id)
self.assertEqual(len(mail.outbox), 2)
assert unpaid_booking.status == 'CANCELLED'
assert paid_booking.status == 'OPEN'

# no auto_cancelled set as per setting
assert not unpaid_booking.auto_cancelled
assert not paid_booking.auto_cancelled

@patch('booking.management.commands.cancel_unpaid_bookings.timezone')
def test_only_cancel_unpaid_bookings_within_day_hours(self, mock_tz):
"""
Expand Down
10 changes: 4 additions & 6 deletions booking/views/event_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,11 @@ def get_context_data(self, **kwargs):
if not self.request.user.is_anonymous:
# Add in the booked_events
user_bookings = self.request.user.bookings.filter(event__id__in=all_events)
user_bookings = {booking.event.id: booking for booking in user_bookings}

booked_events = all_events.filter(bookings__user_id=self.request.user.id, bookings__status='OPEN', bookings__no_show=False).values_list('id', flat=True)
auto_cancelled_events = all_events.filter(bookings__user_id=self.request.user.id, bookings__status='CANCELLED', bookings__auto_cancelled=True).values_list('id', flat=True)

user_booking_dict = {booking.event.id: booking for booking in user_bookings}
booked_events = user_bookings.filter(status='OPEN', no_show=False).values_list('event__id', flat=True)
auto_cancelled_events = user_bookings.filter(status='CANCELLED', auto_cancelled=True).values_list('event__id', flat=True)
waiting_list_events = self.request.user.waitinglists.filter(event__in=all_events).values_list('event__id', flat=True)
context['user_bookings'] = user_bookings
context['user_bookings'] = user_booking_dict
context['booked_events'] = booked_events
context['auto_cancelled_events'] = auto_cancelled_events
context['waiting_list_events'] = waiting_list_events
Expand Down
5 changes: 4 additions & 1 deletion pipsevents/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
LOCAL=(bool, False),
SHOW_VAT=(bool, True),
TESTING=(bool, False),
PAYMENT_METHOD=(str, "paypal")
PAYMENT_METHOD=(str, "paypal") ,
ENFORCE_AUTO_CANCELLATION=(bool, False)
)

environ.Env.read_env(root('pipsevents/.env')) # reading .env file
Expand Down Expand Up @@ -450,6 +451,8 @@ def show_toolbar(request): # pragma: no cover
WATCHLIST = WATCHLIST.split(',')
REGULAR_STUDENT_WHITELIST_IDS = env('REGULAR_STUDENT_WHITELIST_IDS')

ENFORCE_AUTO_CANCELLATION = env("ENFORCE_AUTO_CANCELLATION")

# Increase this to deal with the bulk emails. Currently just under 2000
# users, posts 2 fields per user
DATA_UPLOAD_MAX_NUMBER_FIELDS = 8000
Expand Down
1 change: 1 addition & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ env =
STRIPE_SECRET_KEY=dummy
STRIPE_ENDPOINT_SECRET=dummy
INVOICE_KEY=dummy
ENFORCE_AUTO_CANCELLATION=True

0 comments on commit c34e7b8

Please sign in to comment.