From 9b8d4641836553864a77b644e2077379136d6c4b Mon Sep 17 00:00:00 2001
From: Raz
Date: Tue, 10 Dec 2024 10:51:38 +0100
Subject: [PATCH] improve online register add email generation
---
tournaments/models/team_registration.py | 6 ++
tournaments/models/tournament.py | 43 +++++++++++++
.../templates/register_tournament.html | 2 +-
.../tournaments/tournament_info.html | 16 ++++-
tournaments/views.py | 63 +++++++++++++++++++
5 files changed, 127 insertions(+), 3 deletions(-)
diff --git a/tournaments/models/team_registration.py b/tournaments/models/team_registration.py
index af2774a..a040e90 100644
--- a/tournaments/models/team_registration.py
+++ b/tournaments/models/team_registration.py
@@ -119,3 +119,9 @@ class TeamRegistration(models.Model):
def out_of_tournament(self):
return self.walk_out or self.unregistered
+
+ def get_other_player(self, player):
+ for p in self.playerregistration_set.all():
+ if p != player:
+ return p
+ return None
diff --git a/tournaments/models/tournament.py b/tournaments/models/tournament.py
index bb10fd8..d01cf87 100644
--- a/tournaments/models/tournament.py
+++ b/tournaments/models/tournament.py
@@ -927,6 +927,8 @@ class Tournament(models.Model):
def online_register_is_enabled(self):
if self.supposedly_in_progress():
return False
+ if self.closed_registration_date is not None:
+ return False
if self.end_date is not None:
return False
@@ -961,6 +963,8 @@ class Tournament(models.Model):
def get_online_registration_status(self):
if self.supposedly_in_progress():
return OnlineRegistrationStatus.IN_PROGRESS
+ if self.closed_registration_date is not None:
+ return OnlineRegistrationStatus.ENDED
if self.end_date is not None:
return OnlineRegistrationStatus.ENDED_WITH_RESULTS
@@ -997,6 +1001,45 @@ class Tournament(models.Model):
return OnlineRegistrationStatus.OPEN
+ def is_unregistration_possible(self):
+ # Check if tournament has started
+ if self.supposedly_in_progress():
+ return False
+
+ if self.closed_registration_date is not None:
+ return False
+
+ # Check if tournament is finished
+ if self.end_date is not None:
+ return False
+
+ # Check if registration is closed
+ if self.registration_date_limit is not None:
+ if timezone.now() > self.registration_date_limit:
+ return False
+
+ # Otherwise unregistration is allowed
+ return True
+
+ def build_tournament_details_str(self):
+ tournament_details = []
+ tournament_details.append(self.level())
+ if self.category():
+ tournament_details.append(self.category())
+ if self.age():
+ tournament_details.append(self.age())
+ return " ".join(filter(None, tournament_details))
+
+ def build_name_details_str(self):
+ name_details = []
+ if self.name:
+ name_details.append(self.name)
+ if self.event.name:
+ name_details.append(self.event.name)
+ name_str = " - ".join(filter(None, name_details))
+ if name_str:
+ name_str = f" {name_str}"
+ return name_str
class MatchGroup:
def __init__(self, name, matches, formatted_schedule):
diff --git a/tournaments/templates/register_tournament.html b/tournaments/templates/register_tournament.html
index ff7cf89..6bd51f4 100644
--- a/tournaments/templates/register_tournament.html
+++ b/tournaments/templates/register_tournament.html
@@ -22,7 +22,7 @@
{% if registration_successful %}
Merci, l'inscription a bien été envoyé au juge-arbitre.
-
+ Un email de confirmation a été envoyé à {{ user.email }} pour confirmer votre inscription. Pensez à vérifier vos spams si vous ne recevez pas l'email. En cas de problème, contactez le juge-arbitre.
{% else %}
{% endif %}
+ {{ tournament.build_tournament_details_str|linebreaksbr }}
+ {{ tournament.build_name_details_str|linebreaksbr }}
+
{% if tournament.online_register_is_enabled %}
@@ -128,8 +131,15 @@
Inscrit le {{ team.registration_date }}
-
+ {% if tournament.is_unregistration_possible %}
+
+
+ {% for message in messages %}
+
{{ message }}
+ {% endfor %}
+
+
@@ -141,7 +151,9 @@
{% else %}
Vous n'êtes pas le capitaine de l'équipe, la désinscription en ligne n'est pas disponible. Veuillez contacter le JAP ou votre partenaire.
- -->
+
+ {% endif %}
+ -->
{% endif %}
{% endif %}
diff --git a/tournaments/views.py b/tournaments/views.py
index b28937a..54031d1 100644
--- a/tournaments/views.py
+++ b/tournaments/views.py
@@ -770,6 +770,30 @@ def register_tournament(request, tournament_id):
request.session['team_registration'] = []
registration_successful = True
+
+ # Send confirmation email
+ tournament_details_str = tournament.build_tournament_details_str()
+ name_str = tournament.build_name_details_str()
+ email_subject = f"Confirmation d'inscription au {tournament_details_str}{name_str}"
+ 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)
+ email_body = f"Bonjour,\n\nVotre inscription au tournoi {tournament_details_str}{name_str} est confirmée."
+ email_body += f"\n\nDate d'inscription: {inscription_date}"
+ email_body += f"\n\nÉquipe inscrite: {team_members_str}"
+ email_body += f"\n\nLe tournoi commencera le {tournament.start_date.strftime('%d/%m/%Y')}"
+ email_body += f" @ {tournament.event.club.name}"
+ email_body += f"\n\nVoir les informations sur {request.build_absolute_uri(f'/tournament/{tournament.id}/')}"
+ email_body += "\n\nPour toute question, veuillez contacter votre juge-arbitre. Si vous n'êtes pas à l'origine de cette inscription, merci de le contacter rapidement.\nCeci est un e-mail automatique, veuillez ne pas y répondre."
+ email_body += "\n\nCordialement,\n\nPadel Club"
+
+
+ email = EmailMessage(
+ subject=email_subject,
+ body=email_body,
+ to=[team_form.cleaned_data['email']]
+ )
+ email.send()
else:
add_player_form = AddPlayerForm()
@@ -835,17 +859,56 @@ def register_tournament(request, tournament_id):
@login_required
def unregister_tournament(request, tournament_id):
+ tournament = get_object_or_404(Tournament, id=tournament_id)
+
+ if not tournament or not tournament.is_unregistration_possible():
+ messages.error(request, "Le désistement n'est plus possible pour ce tournoi.")
+ return redirect('tournament-info', tournament_id=tournament_id)
+
user_licence_id = request.user.licence_id
+ if not user_licence_id:
+ messages.error(request, "Vous ne pouvez pas vous désinscrire car vous n'avez pas de numéro de licence associé.")
+ return redirect('tournament-info', tournament_id=tournament_id)
+
+
player_registration = PlayerRegistration.objects.filter(
licence_id__startswith=user_licence_id,
team_registration__tournament_id=tournament_id
).first() # Get the first match, if any
+
+ other_player = None
if player_registration:
team_registration = player_registration.team_registration # Get the related TeamRegistration
+ other_player = team_registration.get_other_player(player_registration)
team_registration.delete() # Delete the team registration
+ else:
+ messages.error(request, "La désincription a échouée. Veuillez contacter le juge-arbitre.")
+ return redirect('tournament-info', tournament_id=tournament_id)
request.session['team_registration'] = []
+ # Get the tournament information and player details before deleting
+ if tournament and request.user.email: # Ensure we have valid tournament and user email
+ tournament_details_str = tournament.build_tournament_details_str()
+ name_str = tournament.build_name_details_str()
+ email_subject = f"Confirmation de désistement au {tournament_details_str}{name_str}"
+
+ email_body = f"Bonjour,\n\nVous venez de vous désinscrire du tournoi {tournament_details_str}{name_str}"
+ email_body += f" du {tournament.start_date.strftime('%d/%m/%Y')}"
+ email_body += f" au {tournament.event.club.name}"
+ if other_player is not None:
+ email_body += f"\n\nVous étiez inscrit avec {other_player.name()}, n'oubliez pas de prévenir votre partenaire."
+
+ email_body += f"\n\nVoir les informations sur {request.build_absolute_uri(f'/tournament/{tournament.id}/')}"
+ email_body += "\n\nPour toute question, veuillez contacter votre juge-arbitre. Si vous n'êtes pas à l'origine de ce désistement, merci de le contacter rapidement.\nCeci est un e-mail automatique, veuillez ne pas y répondre."
+ email_body += "\n\nCordialement,\n\nPadel Club."
+
+ email = EmailMessage(
+ subject=email_subject,
+ body=email_body,
+ to=[request.user.email]
+ )
+ email.send()
return redirect('tournament-info', tournament_id=tournament_id)