models.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import subprocess
  4. from django.conf import settings
  5. from django.db import models
  6. from coin.members.models import Member
  7. class MaillingList(models.Model):
  8. short_name = models.CharField(
  9. 'identifiant technique', max_length=50,
  10. help_text=(
  11. "c'est l'identifiant qui servira à "
  12. "communiquer avec le système de mailling-list"
  13. "(typiquement, la partie avant le \"@\" dans l'adress )"
  14. )
  15. )
  16. email = models.EmailField("adresse mail d'envoi")
  17. verbose_name = models.CharField(
  18. 'nom complet', max_length=130,
  19. help_text="Nom affiché dans l'interface membre"
  20. )
  21. description = models.TextField()
  22. subscribers = models.ManyToManyField(
  23. Member, related_name='subscribed_maillinglists',
  24. verbose_name='abonné·e·s')
  25. class Meta:
  26. verbose_name = 'liste mail'
  27. verbose_name_plural = 'listes mail'
  28. def __unicode__(self):
  29. return '{} ({})'.format(self.verbose_name, self.email)
  30. def as_text_listing(self):
  31. """ One subscriber email per line
  32. """
  33. return '\n'.join(
  34. self.subscribers.values_list('email', flat=True))
  35. def sync_to_list_server(self):
  36. if not settings.MAILLIST_SYNC_COMMAND:
  37. raise ValueError('You should define MAILLIST_SYNC_COMMAND'
  38. ' setting to use maillist module')
  39. else:
  40. cmd = settings.MAILLIST_SYNC_COMMAND.format(
  41. email=self.email,
  42. short_name=self.short_name,
  43. )
  44. p = subprocess.Popen(
  45. cmd, shell=True,
  46. stdin=subprocess.PIPE, stderr=subprocess.PIPE)
  47. out_stdout, out_stderr = p.communicate(address_list)
  48. if p.returncode != 0:
  49. raise SystemError(
  50. "Erreur à l'appel de la commande : \"{}\"".format(
  51. out_stderr))