from django.core.mail import EmailMessage from django.utils import timezone from django.urls import reverse class TournamentEmailService: @staticmethod def _convert_newlines_to_html(text): html_content = text.replace('\n', '
') return f""" {html_content} """ @staticmethod def send_registration_confirmation(request, tournament, team_registration, waiting_list_position): tournament_details_str = tournament.build_tournament_details_str() email_subject = TournamentEmailService._build_email_subject( tournament_details_str, waiting_list_position ) email_body = TournamentEmailService._build_email_body( request, tournament, team_registration, tournament_details_str, waiting_list_position ) email = EmailMessage( subject=email_subject, body=TournamentEmailService._convert_newlines_to_html(email_body), to=[request.user.email] ) email.content_subtype = "html" email.send() @staticmethod def _build_email_subject(tournament_details_str, waiting_list_position): base_subject = f"Confirmation d'inscription au tournoi {tournament_details_str}" if waiting_list_position >= 0: base_subject = f"En liste d'attente du tournoi {tournament_details_str}" return base_subject @staticmethod def _build_email_body(request, tournament, team_registration, tournament_details_str, waiting_list_position): inscription_date = team_registration.local_registration_date().strftime("%d/%m/%Y à %H:%M") team_members = [player.name() for player in team_registration.playerregistration_set.all()] team_members_str = " et ".join(team_members) body_parts = [] body_parts.append("Bonjour,\n") if waiting_list_position >= 0: body_parts.append(f"Votre inscription en liste d'attente du tournoi {tournament_details_str} est confirmée.") else: body_parts.append(f"Votre inscription au tournoi {tournament_details_str} est confirmée.") absolute_url = f"{request.build_absolute_uri(f'/tournament/{tournament.id}/')}" link_text = "informations sur le tournoi" absolute_url = f'{link_text}' body_parts.extend([ f"\nDate d'inscription: {inscription_date}", f"\nÉquipe inscrite: {team_members_str}", f"\nLe tournoi commencera le {tournament.start_date.strftime('%d/%m/%Y')} au club {tournament.event.club.name}", f"\nVoir les {absolute_url}", "\nPour toute question, veuillez contacter votre juge-arbitre. Si vous n'êtes pas à l'origine de cette inscription, merci de le contacter rapidement.", f"\n{tournament.event.creator.full_name()}\n{tournament.event.creator.email}", "\nCeci est un e-mail automatique, veuillez ne pas y répondre.", "\nCordialement,\n\nPadel Club" ]) return "\n".join(body_parts) @staticmethod def send_unregistration_confirmation(captain, tournament, other_player): tournament_details_str = tournament.build_tournament_details_str() email_subject = f"Désistement du tournoi {tournament_details_str}" email_body = TournamentEmailService._build_unregistration_email_body( tournament, captain, tournament_details_str, other_player ) email = EmailMessage( subject=email_subject, body=TournamentEmailService._convert_newlines_to_html(email_body), to=[captain.email] ) email.content_subtype = "html" email.send() if other_player.email is not None: email_body = TournamentEmailService._build_unregistration_email_body( tournament, other_player, tournament_details_str, captain ) email = EmailMessage( subject=email_subject, body=TournamentEmailService._convert_newlines_to_html(email_body), to=[other_player.email] ) email.content_subtype = "html" email.send() @staticmethod def send_out_of_waiting_list_confirmation(captain, tournament, other_player): tournament_details_str = tournament.build_tournament_details_str() email_subject = f"Participation au tournoi {tournament_details_str}" email_body = TournamentEmailService._buil_out_of_waiting_list_email_body( tournament, captain, tournament_details_str, other_player ) email = EmailMessage( subject=email_subject, body=TournamentEmailService._convert_newlines_to_html(email_body), to=[captain.email] ) email.content_subtype = "html" email.send() if other_player.email is not None: email_body = TournamentEmailService._buil_out_of_waiting_list_email_body( tournament, other_player, tournament_details_str, captain ) email = EmailMessage( subject=email_subject, body=TournamentEmailService._convert_newlines_to_html(email_body), to=[other_player.email] ) email.content_subtype = "html" email.send() @staticmethod def _build_unregistration_email_body(tournament, captain, tournament_details_str, other_player): body_parts = [ "Bonjour,\n\n", f"Votre inscription au tournoi {tournament_details_str}, prévu le {tournament.start_date.strftime('%d/%m/%Y')} au club {tournament.event.club.name} a été annulée" ] if other_player is not None: body_parts.append( f"\n\nVous étiez inscrit avec {other_player.name()}, n'oubliez pas de prévenir votre partenaire." ) body_parts.extend([ "\n\nPour toute question, veuillez contacter votre juge-arbitre. " "Si vous n'êtes pas à l'origine de cette inscription, merci de le contacter rapidement.", f"\n{tournament.event.creator.full_name()}\n{tournament.event.creator.email}", "\n\nCeci est un e-mail automatique, veuillez ne pas y répondre." ]) return "".join(body_parts) @staticmethod def _buil_out_of_waiting_list_email_body(tournament, captain, tournament_details_str, other_player): body_parts = [ "Bonjour,\n\n", f"Suite au désistement d'une paire, vous êtes maintenant inscrit au tournoi {tournament_details_str}, prévu le {tournament.start_date.strftime('%d/%m/%Y')} au club {tournament.event.club.name}" ] absolute_url = f"https://padelclub.app/tournament/{tournament.id}/info" link_text = "informations sur le tournoi" absolute_url = f'{link_text}' if other_player is not None: body_parts.append( f"\nVoici le partenaire indiqué dans l'inscription : {other_player.name()}, n'oubliez pas de le prévenir." ) body_parts.append( "\nSi vous n'êtes plus disponible pour participer à ce tournoi, cliquez sur ce lien ou contactez rapidement le juge-arbitre." f"\n{absolute_url}" ) body_parts.extend([ f"\n\n{tournament.event.creator.full_name()}\n{tournament.event.creator.email}", "\n\nCeci est un e-mail automatique, veuillez ne pas y répondre." ]) return "".join(body_parts) @staticmethod def send_tournament_cancellation_notification(player, tournament, other_player): tournament_details_str = tournament.build_tournament_details_str() email_subject = f"Annulation du tournoi {tournament_details_str}" email_body = TournamentEmailService._build_tournament_cancellation_email_body( tournament, player, tournament_details_str, other_player ) email = EmailMessage( subject=email_subject, body=TournamentEmailService._convert_newlines_to_html(email_body), to=[player.email] ) email.content_subtype = "html" email.send() @staticmethod def _build_tournament_cancellation_email_body(tournament, player, tournament_details_str, other_player): body_parts = [ "Bonjour,\n\n", f"Le tournoi {tournament_details_str}, prévu le {tournament.start_date.strftime('%d/%m/%Y')} au club {tournament.event.club.name} a été annulé par le juge-arbitre." ] if other_player is not None: body_parts.append( f"\nVous étiez inscrit avec {other_player.name()}, n'oubliez pas de prévenir votre partenaire." ) body_parts.extend([ "\n\nPour toute question, veuillez contacter votre juge-arbitre:", f"\n{tournament.event.creator.full_name()}\n{tournament.event.creator.email}", "\n\nCeci est un e-mail automatique, veuillez ne pas y répondre." ]) return "".join(body_parts)