From 92e66d75ab009e081860b7e69ea7b19f8557cbb2 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Fri, 9 Oct 2020 13:49:09 +0200 Subject: [PATCH] Automatically create mailing lists --- Dockerfile | 2 +- apps/participation/models.py | 15 +++++++++++++++ apps/participation/views.py | 5 +++++ corres2math/lists.py | 13 +++++++++++++ requirements.txt | 3 ++- 5 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 corres2math/lists.py diff --git a/Dockerfile b/Dockerfile index 712c69a..c0d3351 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,7 +4,7 @@ ENV PYTHONUNBUFFERED 1 ENV DJANGO_ALLOW_ASYNC_UNSAFE 1 # Install LaTeX requirements -RUN apk add --no-cache gettext texlive nginx gcc libc-dev libffi-dev postgresql-dev libmagic +RUN apk add --no-cache gettext nginx gcc libc-dev libffi-dev libxml2-dev libxslt-dev postgresql-dev libmagic RUN apk add --no-cache bash diff --git a/apps/participation/models.py b/apps/participation/models.py index 6867bb2..28d8c10 100644 --- a/apps/participation/models.py +++ b/apps/participation/models.py @@ -8,6 +8,8 @@ from django.utils.crypto import get_random_string from django.utils.text import format_lazy from django.utils.translation import gettext_lazy as _ +from corres2math.lists import get_sympa_client + class Team(models.Model): name = models.CharField( @@ -36,9 +38,22 @@ class Team(models.Model): default=False, ) + def create_mailing_list(self): + get_sympa_client().create_list( + f"equipe-{self.trigram.lower()}", + f"Équipe {self.name} ({self.trigram})", + "hotline", # TODO Use a custom sympa template + f"Liste de diffusion pour contacter l'équipe {self.name} des Correspondances", + "education", + ) + + def delete_mailing_list(self): + get_sympa_client().delete_list(f"equipe-{self.trigram}") + def save(self, *args, **kwargs): if not self.access_code: self.access_code = get_random_string(6) + self.create_mailing_list() return super().save(*args, **kwargs) def __str__(self): diff --git a/apps/participation/views.py b/apps/participation/views.py index 498ab92..57fe622 100644 --- a/apps/participation/views.py +++ b/apps/participation/views.py @@ -10,6 +10,7 @@ from django.utils.translation import gettext_lazy as _ from django.views.generic import CreateView, DetailView, FormView, RedirectView, UpdateView from magic import Magic +from corres2math.lists import get_sympa_client from .forms import JoinTeamForm, ParticipationForm, TeamForm, UploadVideoForm from .models import Participation, Team, Video @@ -36,6 +37,8 @@ class CreateTeamView(LoginRequiredMixin, CreateView): registration = user.registration registration.team = form.instance registration.save() + get_sympa_client().subscribe(user.email, f"equipe-{form.instance.trigram.lower()}", False, + f"{user.first_name} {user.last_name}") return ret def get_success_url(self): @@ -65,6 +68,8 @@ class JoinTeamView(LoginRequiredMixin, FormView): registration = user.registration registration.team = form.instance registration.save() + get_sympa_client().subscribe(user.email, f"equipe-{form.instance.trigram.lower()}", False, + f"{user.first_name} {user.last_name}") return ret def get_success_url(self): diff --git a/corres2math/lists.py b/corres2math/lists.py new file mode 100644 index 0000000..187f4e2 --- /dev/null +++ b/corres2math/lists.py @@ -0,0 +1,13 @@ +import os + +from sympasoap import Client + +_client = None + + +def get_sympa_client() -> Client: + global _client + if _client is None: + _client = Client("https://" + os.getenv("SYMPA_URL")) + _client.login(os.getenv("SYMPA_EMAIL"), os.getenv("SYMPA_PASSWORD")) + return _client diff --git a/requirements.txt b/requirements.txt index 8e2482a..d99f044 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,4 +10,5 @@ djangorestframework django-rest-polymorphic ptpython python-magic -gunicorn \ No newline at end of file +gunicorn +sympasoap \ No newline at end of file