From b6624ca2ca3ef30f4acc55db5de209010c4cad79 Mon Sep 17 00:00:00 2001 From: Raz Date: Mon, 17 Mar 2025 13:48:59 +0100 Subject: [PATCH] fix signal and success page --- tournaments/repositories.py | 6 +- tournaments/services/email_service.py | 92 ++++++++----------- .../services/tournament_registration.py | 22 +++++ tournaments/signals.py | 24 +---- .../registration/activation_failed.html | 23 +++++ .../registration/activation_success.html | 18 ++++ .../tournaments/tournament_info.html | 2 + tournaments/urls.py | 2 + tournaments/views.py | 12 ++- 9 files changed, 119 insertions(+), 82 deletions(-) create mode 100644 tournaments/templates/registration/activation_failed.html create mode 100644 tournaments/templates/registration/activation_success.html diff --git a/tournaments/repositories.py b/tournaments/repositories.py index cd394bd..634a835 100644 --- a/tournaments/repositories.py +++ b/tournaments/repositories.py @@ -53,12 +53,10 @@ class TournamentRegistrationRepository: rank=rank, computed_rank=computed_rank, licence_id=player_data['licence_id'], + email=player_data.get('email'), + phone_number=player_data.get('mobile_number'), ) - if is_captain is True: - player_registration.email=team_form_data['email'] - player_registration.phone_number=team_form_data['mobile_number'] - player_registration.save() team_registration.set_weight() diff --git a/tournaments/services/email_service.py b/tournaments/services/email_service.py index 8349693..8495d9c 100644 --- a/tournaments/services/email_service.py +++ b/tournaments/services/email_service.py @@ -4,6 +4,8 @@ from django.urls import reverse from enum import Enum class TeamEmailType(Enum): + REGISTERED = "registered" + WAITING_LIST = "waiting_list" UNREGISTERED = "unregistered" OUT_OF_WAITING_LIST = "out_of_waiting_list" TOURNAMENT_CANCELED = "tournament_canceled" @@ -16,6 +18,8 @@ class TeamEmailType(Enum): def email_subject(self) -> str: subjects = { + self.REGISTERED: "Participation confirmée", + self.WAITING_LIST: "Liste d'attente", self.UNREGISTERED: "Désistement", self.OUT_OF_WAITING_LIST: "Participation confirmée", self.TOURNAMENT_CANCELED: "Tournoi annulé", @@ -47,52 +51,37 @@ class TournamentEmailService: @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 - ) - - 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: - base_subject = "Liste d'attente" + TournamentEmailService.notify_team(team_registration, tournament, TeamEmailType.WAITING_LIST) else: - base_subject = "Participation confirmée" - return TournamentEmailService.email_subject(tournament, base_subject) + TournamentEmailService.notify_team(team_registration, tournament, TeamEmailType.REGISTERED) @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) + def _build_registration_confirmation_email_body(tournament, captain, tournament_details_str, other_player): + return TournamentEmailService._build_registration_email_body(tournament, captain, tournament_details_str, other_player, False) + @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.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.") 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}/')}" + absolute_url = f"https://padelclub.app/tournament/{tournament.id}/info" 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"\nÉquipe inscrite: {captain.name()} et {other_player.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.", @@ -343,6 +332,7 @@ class TournamentEmailService: @staticmethod 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: return @@ -360,8 +350,16 @@ class TournamentEmailService: TournamentEmailService._send_email(captain.email, email_subject, email_body) @staticmethod - def _build_email_content(message_type, recipient, tournament, tournament_details_str, other_player): - if message_type == TeamEmailType.OUT_OF_WAITING_LIST: + def _build_email_content(message_type, recipient, tournament, tournament_details_str, other_player, request=None, waiting_list_position=None): + 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( tournament, recipient, tournament_details_str, other_player ) @@ -415,24 +413,14 @@ class TournamentEmailService: @staticmethod 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 - players = list(team.playerregistration_set.all()) - if len(players) == 2: - first_player, second_player = players - TournamentEmailService.notify(first_player, second_player, tournament, message_type) - TournamentEmailService.notify(second_player, first_player, tournament, message_type) - elif len(players) == 1: - # If there's only one player, just send them the notification - TournamentEmailService.notify(players[0], None, tournament, message_type) + # Notify both players separately if there is no captain or the captain is unavailable + players = list(team.playerregistration_set.all()) + if len(players) == 2: + print("TournamentEmailService.notify_team 2p", team) + first_player, second_player = players + TournamentEmailService.notify(first_player, second_player, tournament, message_type) + TournamentEmailService.notify(second_player, first_player, tournament, message_type) + elif len(players) == 1: + print("TournamentEmailService.notify_team 1p", team) + # If there's only one player, just send them the notification + TournamentEmailService.notify(players[0], None, tournament, message_type) diff --git a/tournaments/services/tournament_registration.py b/tournaments/services/tournament_registration.py index 8f033f0..6d01eb9 100644 --- a/tournaments/services/tournament_registration.py +++ b/tournaments/services/tournament_registration.py @@ -290,6 +290,28 @@ class TournamentRegistrationService: 'phone': None, }) + from django.contrib.auth import get_user_model + User = get_user_model() + + # Get the license ID from player_data + licence_id = player_data.get('licence_id') + validator = LicenseValidator(licence_id) + if validator and validator.stripped_license: + try: + # Try to find a user with matching license + user_with_same_license = User.objects.get(licence_id__icontains=validator.stripped_license) + + # If found, update the email and phone + if user_with_same_license: + player_data.update({ + 'email': user_with_same_license.email, + 'phone': user_with_same_license.phone + }) + print(f"Found user with license {licence_id}, updated email and phone") + except User.DoesNotExist: + # No user found with this license, continue with None email and phone + pass + def _handle_first_tournament_case(self, data): print("_handle_first_tournament_case", data) if data: diff --git a/tournaments/signals.py b/tournaments/signals.py index 735f1ec..7da0831 100644 --- a/tournaments/signals.py +++ b/tournaments/signals.py @@ -60,29 +60,7 @@ def notify_team(team, tournament, message_type): if tournament.supposedly_in_progress(): return - 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) - if not captain.registered_online or not captain.email: - TournamentEmailService.notify(other_player, captain, tournament, message_type) - else: - # Notify both players separately if there is no captain or the captain is unavailable - players = list(team.playerregistration_set.all()) - if len(players) == 2: - first_player, second_player = players - TournamentEmailService.notify(first_player, second_player, tournament, message_type) - TournamentEmailService.notify(second_player, first_player, tournament, message_type) - elif len(players) == 1: - # If there's only one player, just send them the notification - TournamentEmailService.notify(players[0], None, tournament, message_type) + TournamentEmailService.notify_team(team, tournament, message_type) @receiver(pre_delete, sender=TeamRegistration) def unregister_team(sender, instance, **kwargs): diff --git a/tournaments/templates/registration/activation_failed.html b/tournaments/templates/registration/activation_failed.html new file mode 100644 index 0000000..dfa42e6 --- /dev/null +++ b/tournaments/templates/registration/activation_failed.html @@ -0,0 +1,23 @@ +{% extends 'tournaments/base.html' %} +{% block head_title %} Activation {% endblock %} +{% block first_title %} Padel Club {% endblock %} +{% block second_title %} Activation {% endblock %} + +{% block content %} +{% load static %} +{% load tz %} + +
+
+
+ +

Le lien d'activation est invalide ou a expiré.

+
+ Aller à la page d'accueil +
+
+ Contacter le support +
+
+
+{% endblock %} diff --git a/tournaments/templates/registration/activation_success.html b/tournaments/templates/registration/activation_success.html new file mode 100644 index 0000000..6e17e01 --- /dev/null +++ b/tournaments/templates/registration/activation_success.html @@ -0,0 +1,18 @@ +{% extends 'tournaments/base.html' %} +{% block head_title %} Activation {% endblock %} +{% block first_title %} Padel Club {% endblock %} +{% block second_title %} Activation {% endblock %} + +{% block content %} +{% load static %} +{% load tz %} + +
+
+
+ +

Votre compte a été activé et vous êtes maintenant connecté.

+ Aller à la page d'accueil +
+
+{% endblock %} diff --git a/tournaments/templates/tournaments/tournament_info.html b/tournaments/templates/tournaments/tournament_info.html index f54c34b..0e655fd 100644 --- a/tournaments/templates/tournaments/tournament_info.html +++ b/tournaments/templates/tournaments/tournament_info.html @@ -169,6 +169,8 @@

Vous avez besoin d'un compte Padel Club pour pouvoir vous inscrire en ligne. +
+
Créer le tout de suite !

diff --git a/tournaments/urls.py b/tournaments/urls.py index 4f315c2..b2255d9 100644 --- a/tournaments/urls.py +++ b/tournaments/urls.py @@ -73,5 +73,7 @@ urlpatterns = [ path('admin/tournament-import/', views.tournament_import_view, name='tournament_import'), path('admin/tournament-import-tr/', views.tournament_import_team_reg, name='tournament_import'), path('admin/users-export/', views.UserListExportView.as_view(), name='users_export'), + path('activation-success/', views.activation_success, name='activation_success'), + path('activation-failed/', views.activation_failed, name='activation_failed'), ] diff --git a/tournaments/views.py b/tournaments/views.py index 1a5df8a..081fa02 100644 --- a/tournaments/views.py +++ b/tournaments/views.py @@ -525,10 +525,16 @@ def activate(request, uidb64, token): from django.contrib.auth import login login(request, user, backend='django.contrib.auth.backends.ModelBackend') - next_url = request.GET.get('next', '/') - return redirect(next_url) + return redirect('activation_success') else: - return HttpResponse('Le lien est invalide.') + # Redirect to a custom error page + return redirect('activation_failed') # Define this URL in your urls.py + +def activation_success(request): + return render(request, 'registration/activation_success.html') + +def activation_failed(request): + return render(request, 'registration/activation_failed.html') def club_broadcast(request, broadcast_code): club = get_object_or_404(Club, broadcast_code=broadcast_code)