changeset 364ddcaeafc4 in trytond:default
details: https://hg.tryton.org/trytond?cmd=changeset;node=364ddcaeafc4
description:
        Allow sending email with record's attachments

        issue10135
        review345471002
diffstat:

 CHANGELOG                |   1 +
 trytond/ir/email_.py     |  20 +++++++++++---------
 trytond/tests/test_ir.py |   2 +-
 3 files changed, 13 insertions(+), 10 deletions(-)

diffs (74 lines):

diff -r 8114deea2464 -r 364ddcaeafc4 CHANGELOG
--- a/CHANGELOG Thu Mar 11 19:07:33 2021 +0100
+++ b/CHANGELOG Thu Mar 11 19:14:07 2021 +0100
@@ -1,3 +1,4 @@
+* Allow sending email with record's attachments
 * Add which records to use for actions
 * Add parent to group to inherit accesses
 * Add __access__ to Model
diff -r 8114deea2464 -r 364ddcaeafc4 trytond/ir/email_.py
--- a/trytond/ir/email_.py      Thu Mar 11 19:07:33 2021 +0100
+++ b/trytond/ir/email_.py      Thu Mar 11 19:14:07 2021 +0100
@@ -96,7 +96,7 @@
 
     @classmethod
     def send(cls, to='', cc='', bcc='', subject='', body='',
-            attachments=None, record=None, reports=None):
+            files=None, record=None, reports=None, attachments=None):
         pool = Pool()
         User = pool.get('res.user')
         ActionReport = pool.get('ir.action.report')
@@ -127,13 +127,13 @@
             content.attach(part)
         part = MIMEText(body_html, 'html', _charset='utf-8')
         content.attach(part)
-        if reports or attachments:
+        if files or reports or attachments:
             msg = MIMEMultipart('mixed')
             msg.attach(content)
-            if attachments is None:
-                attachments = []
+            if files is None:
+                files = []
             else:
-                attachments = list(attachments)
+                files = list(files)
 
             for report_id in (reports or []):
                 report = ActionReport(report_id)
@@ -145,9 +145,11 @@
                 name = '%s.%s' % (title, ext)
                 if isinstance(content, str):
                     content = content.encode('utf-8')
-                attachments.append((name, content))
-
-            for name, data in attachments:
+                files.append((name, content))
+            if attachments:
+                files += [
+                    (a.name, a.data) for a in Attachment.browse(attachments)]
+            for name, data in files:
                 mimetype, _ = mimetypes.guess_type(name)
                 if mimetype:
                     attachment = MIMENonMultipart(*mimetype.split('/'))
@@ -190,7 +192,7 @@
         email.save()
         with Transaction().set_context(_check_access=False):
             attachments_ = []
-            for name, data in attachments:
+            for name, data in files:
                 attachments_.append(
                     Attachment(resource=email, name=name, data=data))
             Attachment.save(attachments_)
diff -r 8114deea2464 -r 364ddcaeafc4 trytond/tests/test_ir.py
--- a/trytond/tests/test_ir.py  Thu Mar 11 19:07:33 2021 +0100
+++ b/trytond/tests/test_ir.py  Thu Mar 11 19:14:07 2021 +0100
@@ -154,7 +154,7 @@
                 bcc='[email protected]',
                 subject="Email subject",
                 body='<p>Hello</p>',
-                attachments=[('file.txt', b'data')],
+                files=[('file.txt', b'data')],
                 record=('res.user', 1),
                 reports=[report.id])
 

Reply via email to