# -*- coding: utf-8 -*- from __future__ import unicode_literals import subprocess from django.conf import settings from django.db import models from coin.members.models import Member class MaillingList(models.Model): short_name = models.CharField( 'identifiant technique', max_length=50, help_text=( "c'est l'identifiant qui servira à " "communiquer avec le système de mailling-list" "(typiquement, la partie avant le \"@\" dans l'adress )" ) ) email = models.EmailField("adresse mail d'envoi") verbose_name = models.CharField( 'nom complet', max_length=130, help_text="Nom affiché dans l'interface membre" ) description = models.TextField() subscribers = models.ManyToManyField( Member, related_name='subscribed_maillinglists', verbose_name='abonné·e·s') class Meta: verbose_name = 'liste mail' verbose_name_plural = 'listes mail' def __unicode__(self): return '{} ({})'.format(self.verbose_name, self.email) def as_text_listing(self): """ One subscriber email per line """ return '\n'.join( self.subscribers.values_list('email', flat=True)) def sync_to_list_server(self): if not settings.MAILLIST_SYNC_COMMAND: raise ValueError('You should define MAILLIST_SYNC_COMMAND' ' setting to use maillist module') else: cmd = settings.MAILLIST_SYNC_COMMAND.format( email=self.email, short_name=self.short_name, ) p = subprocess.Popen( cmd, shell=True, stdin=subprocess.PIPE, stderr=subprocess.PIPE) out_stdout, out_stderr = p.communicate(address_list) if p.returncode != 0: raise SystemError( "Erreur à l'appel de la commande : \"{}\"".format( out_stderr))