def _process_waiting_list(booking, request): waiting_list_users = WaitingListUser.objects.filter(event=booking.event) if waiting_list_users: try: send_waiting_list_email( booking.event, [wluser.user for wluser in waiting_list_users], host='http://{}'.format(request.META.get('HTTP_HOST')) ) ActivityLog.objects.create( log='Waiting list email sent to user(s) {} for event {}'. format(', '.join( [wluser.user.username for wluser in waiting_list_users] ), booking.event ) ) except Exception as e: # send mail to tech support with Exception send_support_email( e, __name__, "Automatic cancel job - waiting list email" )
def handle(self, *args, **options): bookings = [] for booking in Booking.objects.filter( event__date__gte=timezone.now(), event__advance_payment_required=True, status='OPEN', paid=False, payment_confirmed=False, warning_sent=True, date_booked__lte=timezone.now() - timedelta(hours=4)): if booking.event.date - timedelta( hours=booking.event.cancellation_period ) < timezone.now(): bookings.append(booking) elif booking.event.payment_due_date: if booking.event.payment_due_date < timezone.now(): bookings.append(booking) for booking in bookings: event_was_full = booking.event.spaces_left() == 0 ctx = { 'booking': booking, 'event': booking.event, 'date': booking.event.date.strftime('%A %d %B'), 'time': booking.event.date.strftime('%I:%M %p'), } # send mails to users try: send_mail('{} Booking cancelled: {}'.format( settings.ACCOUNT_EMAIL_SUBJECT_PREFIX, booking.event.name), get_template( 'flex_bookings/email/booking_auto_cancelled.txt' ).render(ctx), settings.DEFAULT_FROM_EMAIL, [booking.user.email], html_message=get_template( 'flex_bookings/email/booking_auto_cancelled.html' ).render(ctx), fail_silently=False) except Exception as e: # send mail to tech support with Exception send_support_email( e, __name__, "Automatic cancel job - cancelled email" ) booking.status = 'CANCELLED' booking.save() ActivityLog.objects.create( log='Unpaid booking id {} for event {}, user {} ' 'automatically cancelled'.format( booking.id, booking.event, booking.user ) ) if event_was_full: waiting_list_users = WaitingListUser.objects.filter( event=booking.event ) try: send_waiting_list_email( booking.event, [user.user for user in waiting_list_users] ) ActivityLog.objects.create( log='Waiting list email sent to user(s) {} for ' 'event {}'.format( ', '.join( [wluser.user.username for \ wluser in waiting_list_users] ), booking.event ) ) except Exception as e: # send mail to tech support with Exception send_support_email( e, __name__, "Automatic cancel job - waiting list email" ) if bookings: # send single mail to Studio send_mail('{} Booking{} been automatically cancelled'.format( settings.ACCOUNT_EMAIL_SUBJECT_PREFIX, ' has' if len(bookings) == 1 else 's have'), get_template( 'flex_bookings/email/booking_auto_cancelled_studio_email.txt' ).render({'bookings': bookings}), settings.DEFAULT_FROM_EMAIL, [settings.DEFAULT_STUDIO_EMAIL], html_message=get_template( 'flex_bookings/email/booking_auto_cancelled_studio_email.html' ).render({'bookings': bookings}), fail_silently=False) self.stdout.write( 'Cancellation emails sent for booking ids {}'.format( ', '.join([str(booking.id) for booking in bookings]) ) ) else: self.stdout.write('No bookings to cancel') ActivityLog.objects.create( log='cancel_unpaid_bookings job run; no bookings to cancel' )
def delete(self, request, *args, **kwargs): booking = self.get_object() block = booking.block if 'cancel_block' in request.POST: cancel_type = 'block' bookings = [ bk for bk in request.user.bookings.all() if bk.block == block and bk.event.date > timezone.now() and booking.status == 'OPEN' ] else: cancel_type = 'single' bookings = [booking] for booking in bookings: event_was_full = booking.event.spaces_left() == 0 booking.status = 'CANCELLED' # set block back to None. If user decides to rebook later, they # can either rebook the entire block if it hasn't started yet, or # book single class at the individual rate booking.block = None # set payment confirmed to False but leave paid unchanged; email # will be sent to studio to confirm refunded and mark as unpaid booking.payment_confirmed = False booking.save() # if applicable, email users on waiting list if event_was_full: waiting_list_users = WaitingListUser.objects.filter( event=booking.event ) if waiting_list_users: try: send_waiting_list_email( booking.event, [wluser.user for wluser in waiting_list_users], host='http://{}'.format(request.META.get('HTTP_HOST')) ) ActivityLog.objects.create( log='Waiting list email sent to user(s) {} for ' 'event {}'.format( ', '.join( [wluser.user.username for \ wluser in waiting_list_users] ), booking.event ) ) except Exception as e: # send mail to tech support with Exception send_support_email(e, __name__, "DeleteBookingView - waiting list email") messages.error(self.request, "An error occured, please contact " "the studio for information") # messages # logs if cancel_type == 'single': ActivityLog.objects.create( log='Booking id {} for event {}, user {}, was ' 'cancelled'.format( bookings[0].id, bookings[0].event, request.user.username ) ) messages.success( self.request, mark_safe(self.success_message.format(bookings[0].event)) ) else: ActivityLog.objects.create( log='User {} cancelled bookings from block {}: Booking ' 'ids {}'.format( request.user.username, block.name, ', '.join(['{} ({})'.format(booking.id, booking.event) for booking in bookings]) ) ) messages.success( self.request, mark_safe(self.success_message.format( '</br>'.join(['{}'.format(booking.event) for booking in bookings]) ) ) ) # send emails host = 'http://{}'.format(self.request.META.get('HTTP_HOST')) # send email to user ctx = { 'host': host, 'bookings': bookings, 'block_obj': block, 'user': request.user, 'contact_person': bookings[0].event.contact_person, 'contact_email': bookings[0].event.contact_email, } subject = "Block {} cancelled".format(block.name) \ if cancel_type == 'block' \ else "Booking for {} cancelled".format(bookings[0].event) try: send_mail('{} {}'.format( settings.ACCOUNT_EMAIL_SUBJECT_PREFIX, subject), get_template('flex_bookings/email/booking_cancelled.txt').render(ctx), settings.DEFAULT_FROM_EMAIL, [booking.user.email], html_message=get_template( 'flex_bookings/email/booking_cancelled.html').render(ctx), fail_silently=False) except Exception as e: # send mail to tech support with Exception send_support_email(e, __name__, "DeleteBookingView - cancelled email") try: subject = "block {}".format(block) if cancel_type == 'block' else \ "a booking for {}".format(bookings[0].event) # send email to studio send_mail('{} {} {} has just cancelled {}'.format( settings.ACCOUNT_EMAIL_SUBJECT_PREFIX, 'ACTION REQUIRED!' if not block else '', request.user.username, subject), get_template( 'flex_bookings/email/to_studio_booking_cancelled.txt' ).render(ctx), settings.DEFAULT_FROM_EMAIL, [settings.DEFAULT_STUDIO_EMAIL], fail_silently=False) except Exception as e: # send mail to tech support with Exception send_support_email( e, __name__, "DeleteBookingView - cancelled email to studio" ) return HttpResponseRedirect(self.get_success_url())