|
@@ -6,6 +6,8 @@ import subprocess
|
|
|
|
|
|
from django.conf import settings
|
|
|
from django.db import models
|
|
|
+from django.db.models.signals import m2m_changed
|
|
|
+from django.dispatch import receiver
|
|
|
|
|
|
from coin.members.models import Member
|
|
|
|
|
@@ -42,7 +44,7 @@ class MaillingList(models.Model):
|
|
|
return '\n'.join(
|
|
|
self.subscribers.values_list('email', flat=True))
|
|
|
|
|
|
- def sync_to_list_server(self):
|
|
|
+ def sync_to_list_server(self, force_clear=False):
|
|
|
if not settings.MAILLIST_SYNC_COMMAND:
|
|
|
raise ValueError('You should define MAILLIST_SYNC_COMMAND'
|
|
|
' setting to use maillist module')
|
|
@@ -54,8 +56,38 @@ class MaillingList(models.Model):
|
|
|
p = subprocess.Popen(
|
|
|
cmd, shell=True,
|
|
|
stdin=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
- out_stdout, out_stderr = p.communicate(address_list)
|
|
|
+ if force_clear:
|
|
|
+ text_listing = ''
|
|
|
+ else:
|
|
|
+ text_listing = self.as_text_listing()
|
|
|
+ out_stdout, out_stderr = p.communicate(text_listing)
|
|
|
if p.returncode != 0:
|
|
|
raise SystemError(
|
|
|
"Erreur à l'appel de la commande : \"{}\"".format(
|
|
|
- out_stderr))
|
|
|
+ out_stderr.decode('utf-8')))
|
|
|
+
|
|
|
+
|
|
|
+@receiver(m2m_changed, sender=MaillingList.subscribers.through)
|
|
|
+def push_updated_list(sender, instance, action, reverse, model, pk_set, **kwargs):
|
|
|
+ if action in ('post_add', 'post_remove'):
|
|
|
+ if reverse:
|
|
|
+ impacted_mls = MaillingList.objects.filter(pk__in=pk_set)
|
|
|
+ else:
|
|
|
+ impacted_mls = [instance]
|
|
|
+ elif action == 'post_clear' and not reverse:
|
|
|
+ impacted_mls = [instance]
|
|
|
+
|
|
|
+ # cannot be handled at post_clear (we would have lost the information on
|
|
|
+ # what has been removed)
|
|
|
+ elif action == 'pre_clear' and reverse:
|
|
|
+ impacted_mls = instance.subscribed_maillinglists.all()
|
|
|
+
|
|
|
+ else:
|
|
|
+ return
|
|
|
+
|
|
|
+ for ml in impacted_mls:
|
|
|
+ if action == 'pre_clear':
|
|
|
+ # We have to force it because it has not yet been reflected in DB.
|
|
|
+ ml.sync_to_list_server(force_clear=True)
|
|
|
+ else:
|
|
|
+ ml.sync_to_list_server()
|