diff --git a/tournaments/migrations/0103_remove_unregisteredplayer_reason_and_more.py b/tournaments/migrations/0103_remove_unregisteredplayer_reason_and_more.py new file mode 100644 index 0000000..1dc6c6f --- /dev/null +++ b/tournaments/migrations/0103_remove_unregisteredplayer_reason_and_more.py @@ -0,0 +1,33 @@ +# Generated by Django 4.2.11 on 2024-12-17 14:14 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('tournaments', '0102_remove_teamregistration_unregistered_and_more'), + ] + + operations = [ + migrations.RemoveField( + model_name='unregisteredplayer', + name='reason', + ), + migrations.RemoveField( + model_name='unregisteredteam', + name='comment', + ), + migrations.RemoveField( + model_name='unregisteredteam', + name='reason', + ), + migrations.RemoveField( + model_name='unregisteredteam', + name='source', + ), + migrations.RemoveField( + model_name='unregisteredteam', + name='source_value', + ), + ] diff --git a/tournaments/services/email_service.py b/tournaments/services/email_service.py index b799bf7..83e7bbe 100644 --- a/tournaments/services/email_service.py +++ b/tournaments/services/email_service.py @@ -32,9 +32,9 @@ class TournamentEmailService: @staticmethod def _build_email_subject(tournament_details_str, name_str, waiting_list_position): - base_subject = f"Confirmation d'inscription au tournoi {tournament_details_str}{name_str}" + base_subject = f"Confirmation d'inscription au tournoi{tournament_details_str}{name_str}" if waiting_list_position >= 0: - base_subject = f"En liste d'attente du tournoi {tournament_details_str}{name_str}" + base_subject = f"En liste d'attente du tournoi{tournament_details_str}{name_str}" return base_subject @staticmethod @@ -48,7 +48,7 @@ class TournamentEmailService: body_parts.append(f"Bonjour,\n") if waiting_list_position >= 0: - body_parts.append(f"Votre inscription en liste d'attente du tournoi {tournament_details_str}{name_str} est confirmée.") + body_parts.append(f"Votre inscription en liste d'attente du tournoi{tournament_details_str}{name_str} est confirmée.") else: body_parts.append(f"Votre inscription au tournoi{tournament_details_str}{name_str} est confirmée.") @@ -66,14 +66,14 @@ class TournamentEmailService: return "\n".join(body_parts) @staticmethod - def send_unregistration_confirmation(request, 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, name_str, other_player @@ -82,26 +82,23 @@ class TournamentEmailService: email = EmailMessage( subject=email_subject, body=email_body, - to=[request.user.email] + to=[captain.email] ) email.send() @staticmethod - def _build_unregistration_email_body(request, tournament, tournament_details_str, - name_str, other_player): + def _build_unregistration_email_body(tournament, captain, tournament_details_str, name_str, other_player): body_parts = [ - f"Bonjour,\n", - f"Vous venez de vous désinscrire du tournoi {tournament_details_str}{name_str}", - f" du {tournament.start_date.strftime('%d/%m/%Y')} @ {tournament.event.club.name}" + "Bonjour,\n", + f"Votre inscription au tournoi{tournament_details_str}{name_str} du {tournament.start_date.strftime('%d/%m/%Y')} @ {tournament.event.club.name} a été annulée" ] - if other_player: + 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([ - f"\n\nVoir les informations sur {request.build_absolute_uri(f'/tournament/{tournament.id}/')}", "\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}", diff --git a/tournaments/services/tournament_registration.py b/tournaments/services/tournament_registration.py index 658bb84..389e9c2 100644 --- a/tournaments/services/tournament_registration.py +++ b/tournaments/services/tournament_registration.py @@ -95,6 +95,7 @@ class TournamentRegistrationService: return TournamentRegistrationForm(initial=initial_data) def initialize_session_data(self): + print("initialize_session_data") self.request.session['team_registration'] = [] if self.request.user.is_authenticated: self._add_authenticated_user_to_session() @@ -106,6 +107,7 @@ class TournamentRegistrationService: player_data = self._get_authenticated_user_data() if player_data: + self.context['current_players'] = self.request.session.get('team_registration', []) self.request.session['team_registration'].insert(0, player_data) self.request.session.modified = True diff --git a/tournaments/services/tournament_unregistration.py b/tournaments/services/tournament_unregistration.py index 010189f..880c2f0 100644 --- a/tournaments/services/tournament_unregistration.py +++ b/tournaments/services/tournament_unregistration.py @@ -29,8 +29,7 @@ class TournamentUnregistrationService: "La désincription a échouée. Veuillez contacter le juge-arbitre.") return False - self._create_unregistered_team() - self._send_unregistration_email() + self._delete_registered_team() self._cleanup_session() return True @@ -46,33 +45,10 @@ class TournamentUnregistrationService: return True return False - def _create_unregistered_team(self): + def _delete_registered_team(self): team_registration = self.player_registration.team_registration - - unregistered_team = UnregisteredTeam.objects.create( - tournament=self.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, - ) - team_registration.delete() def _cleanup_session(self): self.request.session['team_registration'] = [] - - def _send_unregistration_email(self): - if not self.request.user.email: - return - - TournamentEmailService.send_unregistration_confirmation( - self.request, - self.tournament, - self.other_player - ) + self.request.session.modified = True diff --git a/tournaments/signals.py b/tournaments/signals.py index a5ae266..fada07b 100644 --- a/tournaments/signals.py +++ b/tournaments/signals.py @@ -1,11 +1,15 @@ import random import string -from django.db.models.signals import post_save +from django.db.models.signals import post_save, pre_delete from django.dispatch import receiver from django.conf import settings +from tournaments.models.unregistered_player import UnregisteredPlayer +from django.utils import timezone -from .models import Club, FailedApiCall, CustomUser, Log +from .models import Club, FailedApiCall, CustomUser, Log, TeamRegistration, PlayerRegistration, UnregisteredTeam, UnregisteredPlayer import requests +from tournaments.services.email_service import TournamentEmailService +from tournaments.models import PlayerDataSource def generate_unique_code(): characters = string.ascii_lowercase + string.digits @@ -53,3 +57,36 @@ 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, + ) + + if captain and captain.source is not PlayerDataSource.ONLINE_REGISTRATION and not captain.email: + return + + TournamentEmailService.send_unregistration_confirmation( + captain, + tournament, + other_player + ) diff --git a/tournaments/views.py b/tournaments/views.py index 25a9d6f..e0a78be 100644 --- a/tournaments/views.py +++ b/tournaments/views.py @@ -611,7 +611,7 @@ def register_tournament(request, tournament_id): tournament = get_object_or_404(Tournament, id=tournament_id) service = TournamentRegistrationService(request, tournament) service.initialize_context() - + print("initialize_context") if request.method == 'POST': service.handle_post_request() else: