From 7515f15b7f4f222bd93f4276026530f60a4ec32e Mon Sep 17 00:00:00 2001 From: Raz Date: Wed, 29 Jan 2025 15:18:12 +0100 Subject: [PATCH] fix issue with registration email service --- tournaments/models/tournament.py | 18 +++++-- tournaments/services/email_service.py | 33 ++++++------ .../services/tournament_registration.py | 23 +++++---- .../services/tournament_unregistration.py | 18 +++++++ tournaments/signals.py | 50 ++++++++----------- 5 files changed, 82 insertions(+), 60 deletions(-) diff --git a/tournaments/models/tournament.py b/tournaments/models/tournament.py index d9dfcdf..56fde14 100644 --- a/tournaments/models/tournament.py +++ b/tournaments/models/tournament.py @@ -1090,17 +1090,25 @@ class Tournament(models.Model): # In waiting list with no limit return current_team_count - self.team_count - def build_tournament_details_str(self): + def build_tournament_type_array(self): tournament_details = [] - - name_str = self.build_name_details_str() - if self.federal_level_category > 0: tournament_details.append(self.level()) if self.category(): tournament_details.append(self.category()) - if self.age(): + if self.age() and self.federal_age_category != FederalAgeCategory.SENIOR: tournament_details.append(self.age()) + return tournament_details + + def build_tournament_type_str(self): + tournament_details = self.build_tournament_type_array() + return " ".join(filter(None, tournament_details)) + + def build_tournament_details_str(self): + tournament_details = self.build_tournament_type_array() + + name_str = self.build_name_details_str() + if len(name_str) > 0: tournament_details.append(name_str) diff --git a/tournaments/services/email_service.py b/tournaments/services/email_service.py index d119b40..f019848 100644 --- a/tournaments/services/email_service.py +++ b/tournaments/services/email_service.py @@ -14,11 +14,17 @@ class TournamentEmailService: """ + @staticmethod + def email_subject(tournament, topic): + base_subject = f"[{tournament.build_tournament_type_str()}] [{tournament.formatted_start_date()}] " + topic + return base_subject + @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, tournament_details_str, waiting_list_position ) @@ -41,11 +47,12 @@ class TournamentEmailService: email.send() @staticmethod - def _build_email_subject(tournament_details_str, waiting_list_position): - base_subject = f"Confirmation d'inscription au tournoi {tournament_details_str}" + def _build_email_subject(tournament, tournament_details_str, waiting_list_position): if waiting_list_position >= 0: - base_subject = f"En liste d'attente du tournoi {tournament_details_str}" - return base_subject + base_subject = "En liste d'attente du tournoi" + else: + base_subject = "Confirmation d'inscription au tournoi" + return TournamentEmailService.email_subject(tournament, base_subject) @staticmethod def _build_email_body(request, tournament, team_registration, tournament_details_str, waiting_list_position): @@ -68,7 +75,7 @@ class TournamentEmailService: 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"\nLe tournoi commencera le {tournament.formatted_start_date()} 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}", @@ -82,7 +89,7 @@ class TournamentEmailService: 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_subject = TournamentEmailService.email_subject(tournament, "Désistement du tournoi") email_body = TournamentEmailService._build_unregistration_email_body( tournament, captain, @@ -119,8 +126,7 @@ class TournamentEmailService: @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_subject = TournamentEmailService.email_subject(tournament, "Participation au tournoi") email_body = TournamentEmailService._buil_out_of_waiting_list_email_body( tournament, captain, @@ -158,7 +164,7 @@ class TournamentEmailService: 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" + f"Votre inscription au tournoi {tournament_details_str}, prévu le {tournament.formatted_start_date()} au club {tournament.event.club.name} a été annulée" ] if other_player is not None: @@ -168,7 +174,7 @@ class TournamentEmailService: 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.", + "Si vous n'êtes pas à l'origine de cette désinscription, 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." ]) @@ -179,7 +185,7 @@ class TournamentEmailService: 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}" + f"Suite au désistement d'une paire, vous êtes maintenant inscrit au tournoi {tournament_details_str}, prévu le {tournament.formatted_start_date()} au club {tournament.event.club.name}" ] absolute_url = f"https://padelclub.app/tournament/{tournament.id}/info" @@ -206,8 +212,7 @@ class TournamentEmailService: @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_subject = TournamentEmailService.email_subject(tournament, "Annulation du tournoi") email_body = TournamentEmailService._build_tournament_cancellation_email_body( tournament, player, @@ -228,7 +233,7 @@ class TournamentEmailService: 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." + f"Le tournoi {tournament_details_str}, prévu le {tournament.formatted_start_date()} au club {tournament.event.club.name} a été annulé par le juge-arbitre." ] if other_player is not None: diff --git a/tournaments/services/tournament_registration.py b/tournaments/services/tournament_registration.py index 5294434..f7fbb3a 100644 --- a/tournaments/services/tournament_registration.py +++ b/tournaments/services/tournament_registration.py @@ -22,7 +22,6 @@ class TournamentRegistrationService: 'team_form': None, 'add_player_form': None, 'current_players': self.request.session.get('team_registration', []), - 'user_without_licence': self.request.session.get('user_without_licence', False) } return self.context @@ -54,11 +53,18 @@ class TournamentRegistrationService: if self._is_already_registered(licence_id): return - # Handle player data - if self.context['add_player_form'].names_is_valid(): - self._handle_valid_names(player_data) - else: + if self.request.user.licence_id is None and len(self.context['current_players']) == 0: + # if no licence id for authentificated user and trying to add him as first player of the team, we check his federal data self._handle_invalid_names(licence_id, player_data) + else: + # Handle player data + if self.context['add_player_form'].names_is_valid(): + self._handle_valid_names(player_data) + else: + self._handle_invalid_names(licence_id, player_data) + + if self.request.user.is_authenticated and self.request.user.licence_id is None: + self._update_user_license(player_data.get('licence_id')) def handle_team_registration(self): if not self.context['team_form'].is_valid(): @@ -214,12 +220,6 @@ class TournamentRegistrationService: self._set_default_rank(player_data) self.add_player_to_session(player_data) - - if self.request.user.is_authenticated and self.request.user.licence_id is None: - self._update_user_license(player_data.get('licence_id')) - self.context['add_player_form'].user_without_licence = False - self.request.session.modified = True - self.context['add_player_form'] = AddPlayerForm() self.context['add_player_form'].first_tournament = False @@ -255,6 +255,7 @@ class TournamentRegistrationService: self.request.user.licence_id = validator.computed_licence_id self.request.user.save() self.request.user.refresh_from_db() + self.request.session.modified = True # Reset the form state self.context['add_player_form'] = AddPlayerForm() self.context['add_player_form'].first_tournament = False diff --git a/tournaments/services/tournament_unregistration.py b/tournaments/services/tournament_unregistration.py index 3013186..cfad4c4 100644 --- a/tournaments/services/tournament_unregistration.py +++ b/tournaments/services/tournament_unregistration.py @@ -29,10 +29,28 @@ class TournamentUnregistrationService: "La désincription a échouée. Veuillez contacter le juge-arbitre.") return False + self._unregister_team() self._delete_registered_team() self._cleanup_session() return True + def _unregister_team(self): + # Create unregistered team record + team_registration = self.player_registration.team_registration + + unregistered_team = UnregisteredTeam.objects.create( + tournament=team_registration.tournament, + unregistration_date=timezone.now(), + ) + + for player in team_registration.playerregistration_set.all(): + UnregisteredPlayer.objects.create( + unregistered_team=unregistered_team, + first_name=player.first_name, + last_name=player.last_name, + licence_id=player.licence_id, + ) + def _find_player_registration(self): self.player_registration = PlayerRegistration.objects.filter( licence_id__startswith=self.request.user.licence_id, diff --git a/tournaments/signals.py b/tournaments/signals.py index ac9e8e2..c39a9b7 100644 --- a/tournaments/signals.py +++ b/tournaments/signals.py @@ -61,22 +61,10 @@ def send_discord_message(webhook_url, content): f'Error sending message to Discord webhook: {response.status_code}, {response.text}' ) -@receiver(post_delete, sender=TeamRegistration) +@receiver(pre_delete, sender=TeamRegistration) def unregister_team(sender, instance, **kwargs): team_registration = instance tournament = instance.tournament - global tournament_deletion_in_progress - print('pre_delete TeamRegistration', tournament_deletion_in_progress) - if tournament.id in tournament_deletion_in_progress: - return - - first_waiting_list_team = tournament.first_waiting_list_team() - - # Create unregistered team record - unregistered_team = UnregisteredTeam.objects.create( - tournament=tournament, - unregistration_date=timezone.now(), - ) # Create unregistered player records and track captain/other player captain = None @@ -87,13 +75,25 @@ def unregister_team(sender, instance, **kwargs): 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, + # Send unregistration confirmation + if captain and captain.registered_online and captain.email: + TournamentEmailService.send_unregistration_confirmation( + captain, + tournament, + other_player ) +@receiver(post_delete, sender=TeamRegistration) +def handle_waiting_list(sender, instance, **kwargs): + team_registration = instance + tournament = team_registration.tournament + global tournament_deletion_in_progress + print('post_delete TeamRegistration', tournament_deletion_in_progress) + if tournament.id in tournament_deletion_in_progress: + return + + first_waiting_list_team = tournament.first_waiting_list_team() + # Handle waiting list notifications if first_waiting_list_team: waiting_captain = None @@ -111,14 +111,6 @@ def unregister_team(sender, instance, **kwargs): waiting_other_player ) - # Send unregistration confirmation - if captain and captain.registered_online and captain.email: - TournamentEmailService.send_unregistration_confirmation( - captain, - tournament, - other_player - ) - @receiver(post_delete, sender=Tournament) def tournament_deletion(sender, instance, **kwargs): global tournament_deletion_in_progress @@ -140,16 +132,14 @@ def notify_players_of_tournament_cancellation(sender, instance, **kwargs): # Get players who registered online and have email for player in team_registration.playerregistration_set.all(): - if not player.registered_online: - continue - + print(player, player.registered_online) if player.captain: captain = player else: other_player = player # Send email to captain - if captain: + if captain and captain.registered_online and captain.email: TournamentEmailService.send_tournament_cancellation_notification( captain, tournament,