changeset 3:2b29d5281b88 4.4

Add attachments in multipart/mixed
author Cédric Krier <ced@b2ck.com>
date Wed, 26 Jul 2017 18:59:14 +0200
parents 1ba103fdf476
children bf14f603f5e4
files notification.py tests/test_notification_email.py
diffstat 2 files changed, 79 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/notification.py	Tue Jul 25 18:33:49 2017 +0200
+++ b/notification.py	Wed Jul 26 18:59:14 2017 +0200
@@ -181,17 +181,23 @@
         pool = Pool()
         Attachment = pool.get('notification.email.attachment')
 
-        msg, title = _get_email_template(
+        content, title = _get_email_template(
             self.content.report_name, record, languages)
+
+        if self.attachments:
+            msg = MIMEMultipart('mixed')
+            msg.attach(content)
+            language = list(languages)[-1]
+            for report in self.attachments:
+                msg.attach(Attachment.get_mime(report, record, language))
+        else:
+            msg = content
+
         msg['To'] = ', '.join(to)
         msg['Cc'] = ', '.join(cc)
         msg['Bcc'] = ', '.join(bcc)
         msg['Subject'] = Header(title, 'utf-8')
         msg['Auto-Submitted'] = 'auto-generated'
-
-        language = list(languages)[-1]
-        for report in self.attachments:
-            msg.attach(Attachment.get_mime(report, record, language))
         return msg
 
     def get_log(self, record, trigger, msg):
--- a/tests/test_notification_email.py	Tue Jul 25 18:33:49 2017 +0200
+++ b/tests/test_notification_email.py	Wed Jul 26 18:59:14 2017 +0200
@@ -58,14 +58,6 @@
         report.model = model.model
         report.save()
 
-        attachment = Report()
-        attachment.name = "Attachment"
-        attachment.report_name = 'notification_notification.test.report'
-        attachment.template_extension = 'plain'
-        attachment.report_content = b'attachment for ${records[0].name}'
-        attachment.model = model.model
-        attachment.save()
-
         user = User(Transaction().user)
         user.email = 'user@example.com'
         user.save()
@@ -76,7 +68,6 @@
                 ('name', '=', 'create_uid'),
                 ])
         notification_email.content = report
-        notification_email.attachments = [attachment]
         notification_email.save()
 
         Trigger.create([{
@@ -102,13 +93,6 @@
             self.assertEqual(msg.get_content_type(), 'multipart/alternative')
             self.assertEqual(
                 msg.get_payload(0).get_payload(), 'Hello Michael Scott')
-            attachment = msg.get_payload(1)
-            self.assertEqual(
-                attachment.get_payload(None, True),
-                'attachment for Michael Scott')
-            self.assertEqual(
-                attachment.get_content_type(), 'application/octet-stream')
-            self.assertEqual(attachment.get_filename(), "Attachment.plain")
 
         log, = Log.search([])
         self.assertEqual(log.trigger.notification_email, notification_email)
@@ -116,6 +100,74 @@
         self.assertEqual(log.recipients_secondary, '')
         self.assertEqual(log.recipients_hidden, '')
 
+    @with_transaction()
+    def test_notification_email_attachment(self):
+        "Test email notificiation with attachment"
+        pool = Pool()
+        Model = pool.get('ir.model')
+        ModelField = pool.get('ir.model.field')
+        Action = pool.get('ir.action')
+        Report = pool.get('ir.action.report')
+        User = pool.get('res.user')
+        NotificationEmail = pool.get('notification.email')
+
+        model, = Model.search([
+                ('model', '=', User.__name__),
+                ])
+        action_model, = Model.search([
+                ('model', '=', 'notification.email'),
+                ])
+
+        action = Action(name="Notification Email", type='ir.action.report')
+        action.save()
+        report = Report()
+        report.report_name = 'notification_notification.test.report'
+        report.action = action
+        report.template_extension = 'plain'
+        report.report_content = b'Hello ${records[0].name}'
+        report.model = model.model
+        report.save()
+
+        attachment = Report()
+        attachment.name = "Attachment"
+        attachment.report_name = 'notification_notification.test.report'
+        attachment.template_extension = 'plain'
+        attachment.report_content = b'attachment for ${records[0].name}'
+        attachment.model = model.model
+        attachment.save()
+
+        user = User(Transaction().user)
+        user.email = 'user@example.com'
+        user.save()
+
+        notification_email = NotificationEmail()
+        notification_email.recipients, = ModelField.search([
+                ('model.model', '=', model.model),
+                ('name', '=', 'create_uid'),
+                ])
+        notification_email.content = report
+        notification_email.attachments = [attachment]
+        notification_email.save()
+
+        user, = User.create([{'name': "Michael Scott", 'login': "msc"}])
+
+        msg = notification_email.get_email(
+            user, ['Administrator <user@example.com>'], [], [], ['en'])
+
+        self.assertEqual(msg['Subject'], 'Notification Email')
+        self.assertEqual(msg['To'], 'Administrator <user@example.com>')
+        self.assertEqual(msg.get_content_type(), 'multipart/mixed')
+        self.assertEqual(
+            msg.get_payload(0).get_content_type(), 'multipart/alternative')
+
+        attachment = msg.get_payload(1)
+        self.assertEqual(
+            attachment.get_payload(None, True),
+            'attachment for Michael Scott')
+        self.assertEqual(
+            attachment.get_content_type(), 'application/octet-stream')
+        self.assertEqual(attachment.get_filename(), "Attachment.plain")
+
 
 def suite():
     suite = test_suite()