From 1e413229a10efca11bbe875b62d8db336b01a330 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Sat, 16 Jan 2021 22:29:10 +0100 Subject: [PATCH] Mailing lists are working --- .../management/commands/fix_sympa_lists.py | 65 ++++++++++++++----- apps/participation/models.py | 56 +++++++++++++--- apps/participation/signals.py | 2 +- 3 files changed, 95 insertions(+), 28 deletions(-) diff --git a/apps/participation/management/commands/fix_sympa_lists.py b/apps/participation/management/commands/fix_sympa_lists.py index bbe1af9..ef70e53 100644 --- a/apps/participation/management/commands/fix_sympa_lists.py +++ b/apps/participation/management/commands/fix_sympa_lists.py @@ -3,8 +3,8 @@ from django.core.management import BaseCommand from django.db.models import Q -from participation.models import Team -from registration.models import CoachRegistration, StudentRegistration +from participation.models import Team, Tournament +from registration.models import AdminRegistration, ParticipantRegistration, VolunteerRegistration from tfjm.lists import get_sympa_client @@ -15,29 +15,60 @@ class Command(BaseCommand): """ sympa = get_sympa_client() - sympa.create_list("equipes", "Équipes du TFJM²", "hotline", - "Liste de diffusion pour contacter toutes les équipes validées du TFJM².", + sympa.create_list("equipes", "Equipes du TFJM2", "hotline", + "Liste de diffusion pour contacter toutes les equipes validees du TFJM2.", "education", raise_error=False) - sympa.create_list("equipes-non-valides", "Équipes du TFJM²", "hotline", - "Liste de diffusion pour contacter toutes les équipes non validées du TFJM².", + sympa.create_list("equipes-non-valides", "Equipes non valides du TFJM2", "hotline", + "Liste de diffusion pour contacter toutes les equipes non validees du TFJM2.", "education", raise_error=False) - for problem in range(1, 4): - sympa.create_list(f"probleme-{problem}", - f"Équipes du TFJM² participant au problème {problem}", "hotline", - f"Liste de diffusion pour contacter les équipes participant au problème {problem}" - f" du TFJM².", "education", raise_error=False) + sympa.create_list("admins", "Administrateurs du TFJM2", "hotline", + "Liste de diffusion pour contacter tous les administrateurs du TFJM2.", + "education", raise_error=False) + sympa.create_list("organisateurs", "Organisateurs du TFJM2", "hotline", + "Liste de diffusion pour contacter tous les organisateurs du TFJM2.", + "education", raise_error=False) + sympa.create_list("jurys", "Jurys du TFJM2", "hotline", + "Liste de diffusion pour contacter tous les jurys du TFJM2.", + "education", raise_error=False) + + for tournament in Tournament.objects.all(): + slug = tournament.name.lower().replace(" ", "-") + sympa.create_list(f"equipes-{slug}", f"Equipes du tournoi {tournament.name}", "hotline", + f"Liste de diffusion pour contacter toutes les equipes du tournoi {tournament.name}" + " du TFJM2.", "education", raise_error=False) + sympa.create_list(f"organisateurs-{slug}", f"Organisateurs du tournoi {tournament.name}", "hotline", + "Liste de diffusion pour contacter tous les organisateurs du tournoi " + f"{tournament.name} du TFJM2.", "education", raise_error=False) + sympa.create_list(f"jurys-{slug}", f"Jurys du tournoi {tournament.name}", "hotline", + f"Liste de diffusion pour contacter tous les jurys du tournoi {tournament.name}" + f" du TFJM2.", "education", raise_error=False) + + sympa.subscribe(tournament.teams_email, "equipes", True) + sympa.subscribe(tournament.organizers_email, "organisateurs", True) + sympa.subscribe(tournament.jurys_email, "jurys", True) for team in Team.objects.filter(participation__valid=True).all(): team.create_mailing_list() - sympa.subscribe(team.email, "equipes", f"Equipe {team.name}", True) - sympa.subscribe(team.email, f"probleme-{team.participation.problem}", f"Equipe {team.name}", True) + sympa.unsubscribe(team.email, "equipes-non-valides", True) + sympa.subscribe(team.email, f"equipes-{team.participation.tournament.name.lower().replace(' ', '-')}", + True, f"Equipe {team.name}") for team in Team.objects.filter(Q(participation__valid=False) | Q(participation__valid__isnull=True)).all(): team.create_mailing_list() sympa.subscribe(team.email, "equipes-non-valides", f"Equipe {team.name}", True) - for student in StudentRegistration.objects.filter(team__isnull=False).all(): - sympa.subscribe(student.user.email, f"equipe-{student.team.trigram.lower}", True, f"{student}") - for coach in CoachRegistration.objects.filter(team__isnull=False).all(): - sympa.subscribe(coach.user.email, f"equipe-{coach.team.trigram.lower}", True, f"{coach}") + for participant in ParticipantRegistration.objects.filter(team__isnull=False).all(): + sympa.subscribe(participant.user.email, f"equipe-{participant.team.trigram.lower}", True, f"{participant}") + + for volunteer in VolunteerRegistration.objects.all(): + for organized_tournament in volunteer.organized_tournaments.all(): + slug = organized_tournament.name.lower().replace(" ", "-") + sympa.subscribe(volunteer.user.email, f"organisateurs-{slug}", True) + + for jury_in in volunteer.jury_in.all(): + slug = jury_in.tournament.name.lower().replace(" ", "-") + sympa.subscribe(volunteer.user.email, f"jurys-{slug}", True) + + for admin in AdminRegistration.objects.all(): + sympa.subscribe(admin.user.email, f"admins", True) diff --git a/apps/participation/models.py b/apps/participation/models.py index 41e7669..d506aee 100644 --- a/apps/participation/models.py +++ b/apps/participation/models.py @@ -65,26 +65,20 @@ class Team(models.Model): """ get_sympa_client().create_list( f"equipe-{self.trigram.lower()}", - f"Équipe {self.name} ({self.trigram})", + f"Equipe {self.name} ({self.trigram})", "hotline", # TODO Use a custom sympa template - f"Liste de diffusion pour contacter l'équipe {self.name} du TFJM²", + f"Liste de diffusion pour contacter l'equipe {self.name} du TFJM2", "education", raise_error=False, ) - if self.pk and self.participation.valid: # pragma: no cover - get_sympa_client().subscribe(self.email, "equipes", False, f"Equipe {self.name}") - get_sympa_client().subscribe(self.email, f"probleme-{self.participation.problem}", False, - f"Equipe {self.name}") - else: - get_sympa_client().subscribe(self.email, "equipes-non-valides", False) def delete_mailing_list(self): """ Drop the Sympa mailing list, if the team is empty or if the trigram changed. """ if self.participation.valid: # pragma: no cover - get_sympa_client().unsubscribe(self.email, "equipes", False) - get_sympa_client().unsubscribe(self.email, f"probleme-{self.participation.problem}", False) + get_sympa_client().unsubscribe( + self.email, f"equipes-{self.participation.tournament.name.lower().replace(' ', '-')}", False) else: get_sympa_client().unsubscribe(self.email, "equipes-non-valides", False) get_sympa_client().delete_list(f"equipe-{self.trigram}") @@ -197,6 +191,48 @@ class Tournament(models.Model): default=False, ) + @property + def teams_email(self): + """ + :return: The mailing list to contact the team members. + """ + return f"equipes-{self.name.lower().replace(' ', '-')}@{os.getenv('SYMPA_HOST', 'localhost')}" + + @property + def organizers_email(self): + """ + :return: The mailing list to contact the team members. + """ + return f"organisateurs-{self.name.lower().replace(' ', '-')}@{os.getenv('SYMPA_HOST', 'localhost')}" + + @property + def jurys_email(self): + """ + :return: The mailing list to contact the team members. + """ + return f"organisateurs-{self.name.lower().replace(' ', '-')}@{os.getenv('SYMPA_HOST', 'localhost')}" + + def create_mailing_lists(self): + """ + Create a new Sympa mailing list to contact the team. + """ + get_sympa_client().create_list( + f"equipes-{self.name.lower().replace(' ', '-')}", + f"Equipes du tournoi de {self.name}", + "hotline", # TODO Use a custom sympa template + f"Liste de diffusion pour contacter les equipes du tournoi {self.name} du TFJM²", + "education", + raise_error=False, + ) + get_sympa_client().create_list( + f"organisateurs-{self.name.lower().replace(' ', '-')}", + f"Organisateurs du tournoi de {self.name}", + "hotline", # TODO Use a custom sympa template + f"Liste de diffusion pour contacter les equipes du tournoi {self.name} du TFJM²", + "education", + raise_error=False, + ) + @staticmethod def final_tournament(): qs = Tournament.objects.filter(final=True) diff --git a/apps/participation/signals.py b/apps/participation/signals.py index 680f1f6..d058258 100644 --- a/apps/participation/signals.py +++ b/apps/participation/signals.py @@ -22,7 +22,7 @@ def update_mailing_list(instance: Team, **_): """ if instance.pk: old_team = Team.objects.get(pk=instance.pk) - if old_team.name != instance.name or old_team.trigram != instance.trigram: + if old_team.trigram != instance.trigram: # TODO Rename Matrix room # Delete old mailing list, create a new one old_team.delete_mailing_list()