import logging from django.conf import settings from django.core.mail import send_mail logger = logging.getLogger(__name__) CONFIRMATION_PATH = "/api/reservations/confirm/" def build_confirmation_link(raw_confirmation_token): return f"{settings.SITE_BASE_URL}{CONFIRMATION_PATH}?token={raw_confirmation_token}" def _log_confirmation_link_for_local_debug(*, confirmation_link): if not settings.LOG_RESERVATION_CONFIRMATION_URLS: return logger.warning( "LOCAL DEV confirmation URL: %s", confirmation_link, ) def send_confirmation_email(*, reservation, raw_confirmation_token): confirmation_link = build_confirmation_link(raw_confirmation_token) subject = f"Confirm your reservation for {reservation.performance.show.title}" message = ( "Thank you for your reservation request.\n\n" "Please confirm your reservation by opening this link:\n" f"{confirmation_link}\n\n" "If you did not request this reservation, you can ignore this email." ) _log_confirmation_link_for_local_debug( confirmation_link=confirmation_link, ) try: send_mail( subject=subject, message=message, from_email=None, recipient_list=[reservation.email], fail_silently=False, ) except Exception: if settings.LOG_RESERVATION_CONFIRMATION_URLS: logger.warning( "Local/debug email delivery failed for reservation %s.", reservation.id, ) return logger.exception( "Failed to send confirmation email for reservation %s.", reservation.id, )