|
@@ -6,7 +6,7 @@ import subprocess
|
|
|
|
|
|
from django.conf import settings
|
|
from django.conf import settings
|
|
from django.db import models
|
|
from django.db import models
|
|
-from django.db.models.signals import pre_save, post_save, m2m_changed
|
|
|
|
|
|
+from django.db.models.signals import pre_save, post_save, post_delete
|
|
from django.dispatch import receiver
|
|
from django.dispatch import receiver
|
|
|
|
|
|
from coin.members.models import Member
|
|
from coin.members.models import Member
|
|
@@ -15,6 +15,16 @@ from coin.members.models import Member
|
|
class SyncCommandError(SystemError):
|
|
class SyncCommandError(SystemError):
|
|
pass
|
|
pass
|
|
|
|
|
|
|
|
+class MaillingListSubscription(models.Model):
|
|
|
|
+ member = models.ForeignKey(Member, verbose_name='membre')
|
|
|
|
+ maillinglist = models.ForeignKey('MaillingList', verbose_name='liste mail')
|
|
|
|
+
|
|
|
|
+ class Meta:
|
|
|
|
+ verbose_name = 'abonnement à une liste mail'
|
|
|
|
+ verbose_name_plural = 'abonnements à des listes mail'
|
|
|
|
+ unique_together = ('member', 'maillinglist')
|
|
|
|
+
|
|
|
|
+
|
|
class MaillingList(models.Model):
|
|
class MaillingList(models.Model):
|
|
short_name = models.CharField(
|
|
short_name = models.CharField(
|
|
'identifiant technique', max_length=50,
|
|
'identifiant technique', max_length=50,
|
|
@@ -32,6 +42,7 @@ class MaillingList(models.Model):
|
|
description = models.TextField()
|
|
description = models.TextField()
|
|
subscribers = models.ManyToManyField(
|
|
subscribers = models.ManyToManyField(
|
|
Member, related_name='subscribed_maillinglists',
|
|
Member, related_name='subscribed_maillinglists',
|
|
|
|
+ through=MaillingListSubscription,
|
|
verbose_name='abonné·e·s', blank=True)
|
|
verbose_name='abonné·e·s', blank=True)
|
|
|
|
|
|
class Meta:
|
|
class Meta:
|
|
@@ -71,30 +82,17 @@ class MaillingList(models.Model):
|
|
out_stderr.decode('utf-8')))
|
|
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()
|
|
|
|
-
|
|
|
|
|
|
+@receiver(post_save, sender=MaillingListSubscription)
|
|
|
|
+def push_new_subscription(sender, instance, created, raw, *args, **kwargs):
|
|
|
|
+ if raw:
|
|
|
|
+ print("The synchronization of mailling list with Coin was not performed, please launch it by hand in the admin interface.")
|
|
else:
|
|
else:
|
|
- return
|
|
|
|
|
|
+ instance.maillinglist.sync_to_list_server()
|
|
|
|
+
|
|
|
|
+@receiver(post_delete, sender=MaillingListSubscription)
|
|
|
|
+def push_remove_subscription(sender, instance, *args, **kwargs):
|
|
|
|
+ instance.maillinglist.sync_to_list_server()
|
|
|
|
|
|
- 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()
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(pre_save, sender=Member)
|
|
@receiver(pre_save, sender=Member)
|