Mailing lists are working

This commit is contained in:
Yohann D'ANELLO 2021-01-16 22:29:10 +01:00
parent 71169048fb
commit 1e413229a1
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
3 changed files with 95 additions and 28 deletions

View File

@ -3,8 +3,8 @@
from django.core.management import BaseCommand from django.core.management import BaseCommand
from django.db.models import Q from django.db.models import Q
from participation.models import Team from participation.models import Team, Tournament
from registration.models import CoachRegistration, StudentRegistration from registration.models import AdminRegistration, ParticipantRegistration, VolunteerRegistration
from tfjm.lists import get_sympa_client from tfjm.lists import get_sympa_client
@ -15,29 +15,60 @@ class Command(BaseCommand):
""" """
sympa = get_sympa_client() sympa = get_sympa_client()
sympa.create_list("equipes", "Équipes du TFJM²", "hotline", sympa.create_list("equipes", "Equipes du TFJM2", "hotline",
"Liste de diffusion pour contacter toutes les équipes validées du TFJM².", "Liste de diffusion pour contacter toutes les equipes validees du TFJM2.",
"education", raise_error=False) "education", raise_error=False)
sympa.create_list("equipes-non-valides", "Équipes du TFJM²", "hotline", sympa.create_list("equipes-non-valides", "Equipes non valides du TFJM2", "hotline",
"Liste de diffusion pour contacter toutes les équipes non validées du TFJM².", "Liste de diffusion pour contacter toutes les equipes non validees du TFJM2.",
"education", raise_error=False) "education", raise_error=False)
for problem in range(1, 4): sympa.create_list("admins", "Administrateurs du TFJM2", "hotline",
sympa.create_list(f"probleme-{problem}", "Liste de diffusion pour contacter tous les administrateurs du TFJM2.",
f"Équipes du TFJM² participant au problème {problem}", "hotline", "education", raise_error=False)
f"Liste de diffusion pour contacter les équipes participant au problème {problem}" sympa.create_list("organisateurs", "Organisateurs du TFJM2", "hotline",
f" du TFJM².", "education", raise_error=False) "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(): for team in Team.objects.filter(participation__valid=True).all():
team.create_mailing_list() team.create_mailing_list()
sympa.subscribe(team.email, "equipes", f"Equipe {team.name}", True) sympa.unsubscribe(team.email, "equipes-non-valides", True)
sympa.subscribe(team.email, f"probleme-{team.participation.problem}", f"Equipe {team.name}", 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(): for team in Team.objects.filter(Q(participation__valid=False) | Q(participation__valid__isnull=True)).all():
team.create_mailing_list() team.create_mailing_list()
sympa.subscribe(team.email, "equipes-non-valides", f"Equipe {team.name}", True) sympa.subscribe(team.email, "equipes-non-valides", f"Equipe {team.name}", True)
for student in StudentRegistration.objects.filter(team__isnull=False).all(): for participant in ParticipantRegistration.objects.filter(team__isnull=False).all():
sympa.subscribe(student.user.email, f"equipe-{student.team.trigram.lower}", True, f"{student}") sympa.subscribe(participant.user.email, f"equipe-{participant.team.trigram.lower}", True, f"{participant}")
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 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)

View File

@ -65,26 +65,20 @@ class Team(models.Model):
""" """
get_sympa_client().create_list( get_sympa_client().create_list(
f"equipe-{self.trigram.lower()}", f"equipe-{self.trigram.lower()}",
f"Équipe {self.name} ({self.trigram})", f"Equipe {self.name} ({self.trigram})",
"hotline", # TODO Use a custom sympa template "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", "education",
raise_error=False, 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): def delete_mailing_list(self):
""" """
Drop the Sympa mailing list, if the team is empty or if the trigram changed. Drop the Sympa mailing list, if the team is empty or if the trigram changed.
""" """
if self.participation.valid: # pragma: no cover if self.participation.valid: # pragma: no cover
get_sympa_client().unsubscribe(self.email, "equipes", False) get_sympa_client().unsubscribe(
get_sympa_client().unsubscribe(self.email, f"probleme-{self.participation.problem}", False) self.email, f"equipes-{self.participation.tournament.name.lower().replace(' ', '-')}", False)
else: else:
get_sympa_client().unsubscribe(self.email, "equipes-non-valides", False) get_sympa_client().unsubscribe(self.email, "equipes-non-valides", False)
get_sympa_client().delete_list(f"equipe-{self.trigram}") get_sympa_client().delete_list(f"equipe-{self.trigram}")
@ -197,6 +191,48 @@ class Tournament(models.Model):
default=False, 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 @staticmethod
def final_tournament(): def final_tournament():
qs = Tournament.objects.filter(final=True) qs = Tournament.objects.filter(final=True)

View File

@ -22,7 +22,7 @@ def update_mailing_list(instance: Team, **_):
""" """
if instance.pk: if instance.pk:
old_team = Team.objects.get(pk=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 # TODO Rename Matrix room
# Delete old mailing list, create a new one # Delete old mailing list, create a new one
old_team.delete_mailing_list() old_team.delete_mailing_list()