diff --git a/tournaments/services/email_service.py b/tournaments/services/email_service.py index 8bdc6b8..ce58560 100644 --- a/tournaments/services/email_service.py +++ b/tournaments/services/email_service.py @@ -39,7 +39,8 @@ class TournamentEmailService: return base_subject @staticmethod - def _build_email_body(request, tournament, team_registration, tournament_details_str, name_str, waiting_list_position): + def _build_email_body(request, tournament, team_registration, tournament_details_str, + name_str, waiting_list_position): inscription_date = timezone.now().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) @@ -66,13 +67,12 @@ class TournamentEmailService: return "\n".join(body_parts) @staticmethod - def send_unregistration_confirmation(request, captain, tournament, other_player): + def send_unregistration_confirmation(captain, tournament, other_player): tournament_details_str = tournament.build_tournament_details_str() name_str = tournament.build_name_details_str() email_subject = f"Confirmation de désistement du tournoi {tournament_details_str} {name_str}" email_body = TournamentEmailService._build_unregistration_email_body( - request, tournament, captain, tournament_details_str, @@ -87,14 +87,29 @@ class TournamentEmailService: ) email.send() + if other_player.email is not None: + email_body = TournamentEmailService._build_unregistration_email_body( + tournament, + other_player, + tournament_details_str, + name_str, + captain + ) + + email = EmailMessage( + subject=email_subject, + body=email_body, + to=[other_player.email] + ) + email.send() + @staticmethod - def send_out_of_waiting_list_confirmation(request, captain, tournament, other_player): + def send_out_of_waiting_list_confirmation(captain, tournament, other_player): tournament_details_str = tournament.build_tournament_details_str() name_str = tournament.build_name_details_str() email_subject = f"Participation au tournoi {tournament_details_str} {name_str}" email_body = TournamentEmailService._buil_out_of_waiting_list_email_body( - request, tournament, captain, tournament_details_str, @@ -109,8 +124,25 @@ class TournamentEmailService: ) 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, + name_str, + captain + ) + + email = EmailMessage( + subject=email_subject, + body=email_body, + to=[other_player.email] + ) + email.send() + + @staticmethod - def _build_unregistration_email_body(request, tournament, captain, tournament_details_str, name_str, other_player): + def _build_unregistration_email_body(tournament, captain, tournament_details_str, name_str, other_player): body_parts = [ "Bonjour,\n", f"Votre inscription au tournoi {tournament_details_str} {name_str}, prévu le {tournament.start_date.strftime('%d/%m/%Y')} au club {tournament.event.club.name} a été annulée" @@ -121,10 +153,6 @@ class TournamentEmailService: f"\n\nVous étiez inscrit avec {other_player.name()}, n'oubliez pas de prévenir votre partenaire." ) - body_parts.extend([ - f"\n\nVoir les informations du tournoi sur {request.build_absolute_uri(f'/tournament/{tournament.id}/')}", - ]) - 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.", @@ -135,21 +163,23 @@ class TournamentEmailService: return "".join(body_parts) @staticmethod - def _buil_out_of_waiting_list_email_body(request, tournament, captain, tournament_details_str, name_str, other_player): + def _buil_out_of_waiting_list_email_body(tournament, captain, tournament_details_str, name_str, other_player): body_parts = [ "Bonjour,\n", f"Suite au désistement d'une paire, vous êtes maintenant inscrit au tournoi {tournament_details_str} {name_str}, prévu le {tournament.start_date.strftime('%d/%m/%Y')} au club {tournament.event.club.name}" ] unregister_url = reverse('unregister-tournament', args=[tournament.id]) + # Make it an absolute URL by adding the domain + absolute_url = f"https://127.0.0.1:8000{unregister_url}" # Replace with your domain - absolute_url = request.build_absolute_uri(unregister_url) 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}" diff --git a/tournaments/services/tournament_unregistration.py b/tournaments/services/tournament_unregistration.py index bf04dbe..880c2f0 100644 --- a/tournaments/services/tournament_unregistration.py +++ b/tournaments/services/tournament_unregistration.py @@ -47,62 +47,6 @@ class TournamentUnregistrationService: def _delete_registered_team(self): team_registration = self.player_registration.team_registration - tournament=team_registration.tournament - - print("Unregistering team from tournament") - unregistered_team = UnregisteredTeam.objects.create( - tournament=tournament, - unregistration_date=timezone.now(), - ) - - captain = None - other_player = None - for player in team_registration.playerregistration_set.all(): - if player.captain is True: - captain = player - else: - other_player = player - UnregisteredPlayer.objects.create( - unregistered_team=unregistered_team, - first_name=player.first_name, - last_name=player.last_name, - licence_id=player.licence_id, - ) - - first_waiting_list_team = tournament.first_waiting_list_team() - - if first_waiting_list_team: - waiting_captain = None - waiting_other_player = None - for player in first_waiting_list_team.playerregistration_set.all(): - if player.captain is True: - waiting_captain = player - else: - waiting_other_player = player - - if waiting_captain and waiting_captain.source is not PlayerDataSource.ONLINE_REGISTRATION and not waiting_captain.email: - return - - TournamentEmailService.send_out_of_waiting_list_confirmation( - self.request, - waiting_captain, - tournament, - waiting_other_player - ) - - - - if captain and captain.source is not PlayerDataSource.ONLINE_REGISTRATION and not captain.email: - return - - TournamentEmailService.send_unregistration_confirmation( - self.request, - captain, - tournament, - other_player - ) - - team_registration.delete() def _cleanup_session(self): diff --git a/tournaments/signals.py b/tournaments/signals.py index 79a9ea6..950396d 100644 --- a/tournaments/signals.py +++ b/tournaments/signals.py @@ -57,3 +57,58 @@ def send_discord_message(webhook_url, content): raise ValueError( f'Error sending message to Discord webhook: {response.status_code}, {response.text}' ) + +@receiver(pre_delete, sender=TeamRegistration) +def unregister_team(sender, instance, **kwargs): + team_registration = instance + tournament=instance.tournament + print("Unregistering team from tournament") + unregistered_team = UnregisteredTeam.objects.create( + tournament=tournament, + unregistration_date=timezone.now(), + ) + + captain = None + other_player = None + for player in team_registration.playerregistration_set.all(): + if player.captain is True: + captain = player + else: + other_player = player + UnregisteredPlayer.objects.create( + unregistered_team=unregistered_team, + first_name=player.first_name, + last_name=player.last_name, + licence_id=player.licence_id, + ) + + first_waiting_list_team = tournament.first_waiting_list_team() + + if first_waiting_list_team: + waiting_captain = None + waiting_other_player = None + for player in first_waiting_list_team.playerregistration_set.all(): + if player.captain is True: + waiting_captain = player + else: + waiting_other_player = player + + if waiting_captain and waiting_captain.source is not PlayerDataSource.ONLINE_REGISTRATION and not waiting_captain.email: + return + + TournamentEmailService.send_out_of_waiting_list_confirmation( + waiting_captain, + tournament, + waiting_other_player + ) + + + + if captain and captain.source is not PlayerDataSource.ONLINE_REGISTRATION and not captain.email: + return + + TournamentEmailService.send_unregistration_confirmation( + captain, + tournament, + other_player + )