pfwb/sendmail: add support for recipients like liste-listes-des-parlementaires@ to send documents to mailing lists
fixes #3959
This commit is contained in:
parent
12fd8cedb4
commit
b5928dc7cd
|
@ -14,6 +14,7 @@ from django.core.files.base import ContentFile
|
|||
from django.db import transaction
|
||||
from django.core.exceptions import MultipleObjectsReturned
|
||||
from django.utils.timezone import utc, make_aware
|
||||
from django.template.defaultfilters import slugify
|
||||
|
||||
from docbow_project.docbow import models, timestamp
|
||||
from docbow_project.docbow.email_utils import u2u_decode
|
||||
|
@ -50,6 +51,7 @@ In case of failure the following return value is returned:
|
|||
def handle(self, *args, **options):
|
||||
if not options.get('sender'):
|
||||
self.error('7.7.1 No sender', exit_code=8)
|
||||
self.setup_mailing_list_dict()
|
||||
try:
|
||||
if options.get('file'):
|
||||
mail = email.message_from_file(file(options['file']))
|
||||
|
@ -84,11 +86,22 @@ In case of failure the following return value is returned:
|
|||
else:
|
||||
return filename
|
||||
|
||||
def setup_mailing_list_dict(self):
|
||||
self.mailing_lists = {}
|
||||
for mailing_list in models.MailingList.objects.all():
|
||||
self.mailing_lists[slugify(mailing_list.name)] = mailing_list
|
||||
|
||||
def resolve_username_for_list(self, username):
|
||||
if not username.startswith('liste-'):
|
||||
return None
|
||||
return self.mailing_lists.get(username[len('liste-'):])
|
||||
|
||||
@transaction.commit_on_success
|
||||
def handle_mail(self, mail, mail_recipients, **options):
|
||||
content_errors = []
|
||||
attachments = []
|
||||
recipients = []
|
||||
mailing_list_recipients = []
|
||||
description = u''
|
||||
from_email = email.utils.parseaddr(options['sender'])[1]
|
||||
if not from_email:
|
||||
|
@ -184,6 +197,12 @@ In case of failure the following return value is returned:
|
|||
|
||||
for email_address in all_recipients:
|
||||
username, domain = email_address.split('@', 1)
|
||||
# mailing list case
|
||||
mailing_list = self.resolve_username_for_list(username)
|
||||
if mailing_list is not None:
|
||||
mailing_list_recipients.append(mailing_list)
|
||||
continue
|
||||
# classic user case
|
||||
try:
|
||||
user = auth_models.User.objects.get(username=username)
|
||||
recipients.append(user)
|
||||
|
@ -228,6 +247,7 @@ In case of failure the following return value is returned:
|
|||
comment=description, filetype=filetype)
|
||||
document.save()
|
||||
document.to_user = recipients
|
||||
document.to_list = mailing_list_recipients
|
||||
for filename, payload in attachments:
|
||||
content = ContentFile(payload)
|
||||
attached_file = models.AttachedFile(document=document,
|
||||
|
|
|
@ -13,10 +13,11 @@ from django.core import management
|
|||
from django.contrib.auth.models import User
|
||||
|
||||
from docbow_project.pfwb.models import TabellioDocType
|
||||
from docbow_project.docbow.models import FileType
|
||||
from docbow_project.docbow.models import FileType, MailingList, Document
|
||||
|
||||
EXPEDITION_EMAIL = 'expedition@example.com'
|
||||
RECIPIENT_EMAIL = 'recipient@example.com'
|
||||
RECIPIENT_LIST_EMAIL = 'liste-ma-liste@example.com'
|
||||
|
||||
@override_settings(DOCBOW_PFWB_SENDMAIL_DEFAULT_TYPE_ID=1)
|
||||
@override_settings(DOCBOW_PFWB_SENDMAIL_DEFAULT_TYPE_NAME='Default')
|
||||
|
@ -33,6 +34,9 @@ class SendMailTestCase(TestCase):
|
|||
tabellio_doc_type='PJD')
|
||||
self.expedition_user = User.objects.create(username='expedition', id=1)
|
||||
self.to_user = User.objects.create(username='recipient', email=RECIPIENT_EMAIL)
|
||||
self.to_list = MailingList.objects.create(name='ma liste')
|
||||
self.to_list.members.add(self.to_user)
|
||||
|
||||
|
||||
def test_expedition_mode(self):
|
||||
"""
|
||||
|
@ -58,3 +62,35 @@ Coucou
|
|||
f.flush()
|
||||
management.call_command('sendmail', RECIPIENT_EMAIL, file=f.name,
|
||||
sender=EXPEDITION_EMAIL)
|
||||
self.assertEqual(Document.objects.count(), 1)
|
||||
self.assertEquals(Document.objects.get().to_user.count(), 1)
|
||||
self.assertEquals(Document.objects.get().to_list.count(), 0)
|
||||
|
||||
def test_expedition_mode_to_list(self):
|
||||
"""
|
||||
Test sendmail management command when using the expedition mode
|
||||
"""
|
||||
with tempfile.NamedTemporaryFile() as f:
|
||||
content = '''\
|
||||
Message-ID: <232323232@example.com>
|
||||
From: %(expedition_email)s
|
||||
To: %(recipient_email)s
|
||||
Subject: Mouais: monfichier.pdf
|
||||
MIME-Version: 1.0
|
||||
Content-Type: text/plain; charset=us-ascii
|
||||
Content-Disposition: inline
|
||||
User-Agent: Mutt/1.5.20 (2009-06-14)
|
||||
X-Tabellio-Doc-Type: PJD
|
||||
X-Tabellio-Doc-URL: https://ramcloud.stanford.edu/wiki/download/attachments/11370504/raft.pdf
|
||||
Status: RO
|
||||
|
||||
Coucou
|
||||
'''
|
||||
f.write(content)
|
||||
f.flush()
|
||||
management.call_command('sendmail', RECIPIENT_LIST_EMAIL, file=f.name,
|
||||
sender=EXPEDITION_EMAIL)
|
||||
self.assertEqual(Document.objects.count(), 1)
|
||||
self.assertEquals(Document.objects.get().to_user.count(), 0)
|
||||
self.assertEquals(Document.objects.get().to_list.count(), 1)
|
||||
self.assertEquals(Document.objects.get().to_list.all()[0], self.to_list)
|
||||
|
|
Loading…
Reference in New Issue