@ -4,6 +4,8 @@ from django.urls import reverse
from enum import Enum
from enum import Enum
class TeamEmailType ( Enum ) :
class TeamEmailType ( Enum ) :
REGISTERED = " registered "
WAITING_LIST = " waiting_list "
UNREGISTERED = " unregistered "
UNREGISTERED = " unregistered "
OUT_OF_WAITING_LIST = " out_of_waiting_list "
OUT_OF_WAITING_LIST = " out_of_waiting_list "
TOURNAMENT_CANCELED = " tournament_canceled "
TOURNAMENT_CANCELED = " tournament_canceled "
@ -16,6 +18,8 @@ class TeamEmailType(Enum):
def email_subject ( self ) - > str :
def email_subject ( self ) - > str :
subjects = {
subjects = {
self . REGISTERED : " Participation confirmée " ,
self . WAITING_LIST : " Liste d ' attente " ,
self . UNREGISTERED : " Désistement " ,
self . UNREGISTERED : " Désistement " ,
self . OUT_OF_WAITING_LIST : " Participation confirmée " ,
self . OUT_OF_WAITING_LIST : " Participation confirmée " ,
self . TOURNAMENT_CANCELED : " Tournoi annulé " ,
self . TOURNAMENT_CANCELED : " Tournoi annulé " ,
@ -47,52 +51,37 @@ class TournamentEmailService:
@staticmethod
@staticmethod
def send_registration_confirmation ( request , tournament , team_registration , waiting_list_position ) :
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 ,
tournament_details_str ,
waiting_list_position
)
email_body = TournamentEmailService . _build_email_body (
request ,
tournament ,
team_registration ,
tournament_details_str ,
waiting_list_position
)
TournamentEmailService . _send_email ( request . user . email , email_subject , email_body )
@staticmethod
def _build_email_subject ( tournament , tournament_details_str , waiting_list_position ) :
if waiting_list_position > = 0 :
if waiting_list_position > = 0 :
base_subject = " Liste d ' attente "
TournamentEmailService . notify_team ( team_registration , tournament , TeamEmailType . WAITING_LIST )
else :
else :
base_subject = " Participation confirmée "
TournamentEmailService . notify_team ( team_registration , tournament , TeamEmailType . REGISTERED )
return TournamentEmailService . email_subject ( tournament , base_subject )
@staticmethod
@staticmethod
def _build_email_body ( request , tournament , team_registration , tournament_details_str , waiting_list_position ) :
def _build_registration_confirmation_email_body ( tournament , captain , tournament_details_str , other_player ) :
inscription_date = team_registration . local_registration_date ( ) . strftime ( " %d / % m/ % Y à % H: % M " )
return TournamentEmailService . _build_registration_email_body ( tournament , captain , tournament_details_str , other_player , False )
team_members = [ player . name ( ) for player in team_registration . playerregistration_set . all ( ) ]
team_members_str = " et " . join ( team_members )
@staticmethod
def _build_waiting_list_confirmation_email_body ( tournament , captain , tournament_details_str , other_player ) :
return TournamentEmailService . _build_registration_email_body ( tournament , captain , tournament_details_str , other_player , True )
@staticmethod
def _build_registration_email_body ( tournament , captain , tournament_details_str , other_player , waiting_list ) :
inscription_date = captain . team_registration . local_registration_date ( ) . strftime ( " %d / % m/ % Y à % H: % M " )
body_parts = [ ]
body_parts = [ ]
body_parts . append ( " Bonjour, \n " )
body_parts . append ( " Bonjour, \n " )
if waiting_list_position > = 0 :
if waiting_list :
body_parts . append ( f " Votre inscription en liste d ' attente du tournoi { tournament_details_str } est confirmée. " )
body_parts . append ( f " Votre inscription en liste d ' attente du tournoi { tournament_details_str } est confirmée. " )
else :
else :
body_parts . append ( f " Votre inscription au tournoi { tournament_details_str } est confirmée. " )
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 } / ' ) } "
absolute_url = f " https://padelclub.app /tournament/{ tournament . id } /info "
link_text = " informations sur le tournoi "
link_text = " informations sur le tournoi "
absolute_url = f ' <a href= " { absolute_url } " > { link_text } </a> '
absolute_url = f ' <a href= " { absolute_url } " > { link_text } </a> '
body_parts . extend ( [
body_parts . extend ( [
f " \n Date d ' inscription: { inscription_date } " ,
f " \n Date d ' inscription: { inscription_date } " ,
f " \n Équipe inscrite: { team_members_str } " ,
f " \n Équipe inscrite: { captain . name ( ) } et { other_player . name ( ) } " ,
f " \n Le tournoi commencera le { tournament . formatted_start_date ( ) } au club { tournament . event . club . name } " ,
f " \n Le tournoi commencera le { tournament . formatted_start_date ( ) } au club { tournament . event . club . name } " ,
f " \n Voir les { absolute_url } " ,
f " \n Voir les { absolute_url } " ,
" \n Pour toute question, veuillez contacter votre juge-arbitre. Si vous n ' êtes pas à l ' origine de cette inscription, merci de le contacter rapidement. " ,
" \n Pour toute question, veuillez contacter votre juge-arbitre. Si vous n ' êtes pas à l ' origine de cette inscription, merci de le contacter rapidement. " ,
@ -343,6 +332,7 @@ class TournamentEmailService:
@staticmethod
@staticmethod
def notify ( captain , other_player , tournament , message_type : TeamEmailType ) :
def notify ( captain , other_player , tournament , message_type : TeamEmailType ) :
print ( " TournamentEmailService.notify " , captain . email , captain . registered_online , tournament , message_type )
if not captain or not captain . registered_online or not captain . email :
if not captain or not captain . registered_online or not captain . email :
return
return
@ -360,8 +350,16 @@ class TournamentEmailService:
TournamentEmailService . _send_email ( captain . email , email_subject , email_body )
TournamentEmailService . _send_email ( captain . email , email_subject , email_body )
@staticmethod
@staticmethod
def _build_email_content ( message_type , recipient , tournament , tournament_details_str , other_player ) :
def _build_email_content ( message_type , recipient , tournament , tournament_details_str , other_player , request = None , waiting_list_position = None ) :
if message_type == TeamEmailType . OUT_OF_WAITING_LIST :
if message_type == TeamEmailType . REGISTERED :
body = TournamentEmailService . _build_registration_confirmation_email_body (
tournament , recipient , tournament_details_str , other_player
)
elif message_type == TeamEmailType . WAITING_LIST :
body = TournamentEmailService . _build_waiting_list_confirmation_email_body (
tournament , recipient , tournament_details_str , other_player
)
elif message_type == TeamEmailType . OUT_OF_WAITING_LIST :
body = TournamentEmailService . _build_out_of_waiting_list_email_body (
body = TournamentEmailService . _build_out_of_waiting_list_email_body (
tournament , recipient , tournament_details_str , other_player
tournament , recipient , tournament_details_str , other_player
)
)
@ -415,24 +413,14 @@ class TournamentEmailService:
@staticmethod
@staticmethod
def notify_team ( team , tournament , message_type : TeamEmailType ) :
def notify_team ( team , tournament , message_type : TeamEmailType ) :
captain = None
other_player = None
for player in team . playerregistration_set . all ( ) :
if player . captain :
captain = player
else :
other_player = player
if captain :
TournamentEmailService . notify ( captain , other_player , tournament , message_type )
else :
# Notify both players separately if there is no captain or the captain is unavailable
# Notify both players separately if there is no captain or the captain is unavailable
players = list ( team . playerregistration_set . all ( ) )
players = list ( team . playerregistration_set . all ( ) )
if len ( players ) == 2 :
if len ( players ) == 2 :
print ( " TournamentEmailService.notify_team 2p " , team )
first_player , second_player = players
first_player , second_player = players
TournamentEmailService . notify ( first_player , second_player , tournament , message_type )
TournamentEmailService . notify ( first_player , second_player , tournament , message_type )
TournamentEmailService . notify ( second_player , first_player , tournament , message_type )
TournamentEmailService . notify ( second_player , first_player , tournament , message_type )
elif len ( players ) == 1 :
elif len ( players ) == 1 :
print ( " TournamentEmailService.notify_team 1p " , team )
# If there's only one player, just send them the notification
# If there's only one player, just send them the notification
TournamentEmailService . notify ( players [ 0 ] , None , tournament , message_type )
TournamentEmailService . notify ( players [ 0 ] , None , tournament , message_type )