mirror of
				https://gitlab.com/animath/si/plateforme-corres2math.git
				synced 2025-11-04 10:22:15 +01:00 
			
		
		
		
	Automatically create mailing lists
This commit is contained in:
		@@ -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
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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):
 | 
			
		||||
 
 | 
			
		||||
@@ -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):
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										13
									
								
								corres2math/lists.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								corres2math/lists.py
									
									
									
									
									
										Normal file
									
								
							@@ -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
 | 
			
		||||
@@ -10,4 +10,5 @@ djangorestframework
 | 
			
		||||
django-rest-polymorphic
 | 
			
		||||
ptpython
 | 
			
		||||
python-magic
 | 
			
		||||
gunicorn
 | 
			
		||||
gunicorn
 | 
			
		||||
sympasoap
 | 
			
		||||
		Reference in New Issue
	
	Block a user