diff --git a/CHANGELOG b/CHANGELOG index cdde858205..a4d1ab0d34 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +* Add fallback users for recipients * Add contact mechanism usage Version 4.6.0 - 2017-10-30 diff --git a/notification.py b/notification.py index f81b2ed129..9e37bfbe5c 100644 --- a/notification.py +++ b/notification.py @@ -40,6 +40,16 @@ class Email(ModelSQL, ModelView): ], depends=['model'], help="The field that contains the recipient(s).") + fallback_recipients = fields.Many2One( + 'res.user', "Recipients Fallback User", + domain=[ + ('email', '!=', None), + ], + states={ + 'invisible': ~Eval('recipients'), + }, + depends=['recipients'], + help="User notified when no recipients e-mail is found") recipients_secondary = fields.Many2One( 'ir.model.field', "Secondary Recipients", domain=[ @@ -48,6 +58,16 @@ class Email(ModelSQL, ModelView): ], depends=['model'], help="The field that contains the secondary recipient(s).") + fallback_recipients_secondary = fields.Many2One( + 'res.user', "Secondary Recipients Fallback User", + domain=[ + ('email', '!=', None), + ], + states={ + 'invisible': ~Eval('recipients_secondary'), + }, + depends=['recipients'], + help="User notified when no secondary recipients e-mail is found") recipients_hidden = fields.Many2One( 'ir.model.field', "Hidden Recipients", domain=[ @@ -56,6 +76,16 @@ class Email(ModelSQL, ModelView): ], depends=['model'], help="The field that contains the hidden recipient(s).") + fallback_recipients_hidden = fields.Many2One( + 'res.user', "Hidden Recipients Fallback User", + domain=[ + ('email', '!=', None), + ], + states={ + 'invisible': ~Eval('recipients_hidden'), + }, + depends=['recipients_hidden'], + help="User notified when no hidden recipients e-mail is found") contact_mechanism = fields.Selection( 'get_contact_mechanisms', "Contact Mechanism", @@ -227,6 +257,11 @@ class Email(ModelSQL, ModelView): if recipients: languagues.update(self._get_languages(recipients)) to = self._get_addresses(recipients) + if not to and self.fallback_recipients: + languagues.update( + self._get_languages(self.fallback_recipients)) + to = self._get_addresses(self.fallback_recipients) + cc = [] if self.recipients_secondary: recipients_secondary = getattr( @@ -235,6 +270,11 @@ class Email(ModelSQL, ModelView): languagues.update( self._get_languages(recipients_secondary)) cc = self._get_addresses(recipients_secondary) + if not cc and self.fallback_recipients_secondary: + languagues.update( + self._get_languages(self.fallback_recipients_secondary)) + cc = self._get_addresses(self.fallback_recipients_secondary) + bcc = [] if self.recipients_hidden: recipients_hidden = getattr( @@ -242,6 +282,10 @@ class Email(ModelSQL, ModelView): if recipients_hidden: languagues.update(self._get_languages(recipients_hidden)) bcc = self._get_addresses(recipients_hidden) + if not bcc and self.fallback_recipients_hidden: + languagues.update( + self._get_languages(self.fallback_recipients_hidden)) + bcc = self._get_addresses(self.fallback_recipients_hidden) msg = self.get_email(record, from_, to, cc, bcc, languagues) to_addrs = [e for _, e in getaddresses(to + cc + bcc)] diff --git a/tests/test_notification_email.py b/tests/test_notification_email.py index b7f0511ee1..442b23f6a7 100644 --- a/tests/test_notification_email.py +++ b/tests/test_notification_email.py @@ -29,20 +29,14 @@ class NotificationEmailTestCase(ModuleTestCase): config.set('email', 'from', FROM) self.addCleanup(lambda: config.set('email', 'from', reset_from)) - @unittest.skipIf( - (3, 5, 0) <= sys.version_info < (3, 5, 2), "python bug #25195") - @with_transaction() - def test_notification_email(self): - "Test email notificiation is sent on trigger" + def _setup_notification(self): pool = Pool() Model = pool.get('ir.model') ModelField = pool.get('ir.model.field') Action = pool.get('ir.action') Report = pool.get('ir.action.report') - Trigger = pool.get('ir.trigger') User = pool.get('res.user') NotificationEmail = pool.get('notification.email') - Log = pool.get('notification.email.log') model, = Model.search([ ('model', '=', User.__name__), @@ -73,6 +67,27 @@ class NotificationEmailTestCase(ModuleTestCase): notification_email.content = report notification_email.save() + @unittest.skipIf( + (3, 5, 0) <= sys.version_info < (3, 5, 2), "python bug #25195") + @with_transaction() + def test_notification_email(self): + "Test email notificiation is sent on trigger" + pool = Pool() + User = pool.get('res.user') + Trigger = pool.get('ir.trigger') + Model = pool.get('ir.model') + NotificationEmail = pool.get('notification.email') + Log = pool.get('notification.email.log') + + self._setup_notification() + notification_email, = NotificationEmail.search([]) + + model, = Model.search([ + ('model', '=', User.__name__), + ]) + action_model, = Model.search([ + ('model', '=', 'notification.email'), + ]) Trigger.create([{ 'name': 'Test creation', 'model': model.id, @@ -110,13 +125,12 @@ class NotificationEmailTestCase(ModuleTestCase): "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') Language = pool.get('ir.lang') + self._setup_notification() model, = Model.search([ ('model', '=', User.__name__), ]) @@ -125,16 +139,6 @@ class NotificationEmailTestCase(ModuleTestCase): ]) en, = Language.search([('code', '=', 'en')]) - 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 = 'txt' - 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' @@ -143,16 +147,7 @@ class NotificationEmailTestCase(ModuleTestCase): 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, = NotificationEmail.search([]) notification_email.attachments = [attachment] notification_email.save() @@ -176,6 +171,56 @@ class NotificationEmailTestCase(ModuleTestCase): attachment.get_content_type(), 'text/plain') self.assertEqual(attachment.get_filename(), "Attachment.txt") + @unittest.skipIf( + (3, 5, 0) <= sys.version_info < (3, 5, 2), "python bug #25195") + @with_transaction() + def test_notification_email_fallback(self): + "Test email notification fallback" + pool = Pool() + User = pool.get('res.user') + Trigger = pool.get('ir.trigger') + Model = pool.get('ir.model') + NotificationEmail = pool.get('notification.email') + User = pool.get('res.user') + + fallback_user = User() + fallback_user.name = 'Fallback' + fallback_user.email = 'fallback@example.com' + fallback_user.login = 'fallback' + fallback_user.save() + + self._setup_notification() + notification_email, = NotificationEmail.search([]) + notification_email.recipients = None + notification_email.fallback_recipients = fallback_user + notification_email.save() + + model, = Model.search([ + ('model', '=', User.__name__), + ]) + action_model, = Model.search([ + ('model', '=', 'notification.email'), + ]) + Trigger.create([{ + 'name': 'Test creation', + 'model': model.id, + 'on_create': True, + 'condition': 'true', + 'notification_email': notification_email.id, + 'action_model': action_model.id, + 'action_function': 'trigger', + }]) + + with patch.object( + notification_module, 'sendmail_transactional') as sendmail, \ + patch.object(notification_module, 'SMTPDataManager'): + User.create([{'name': "Michael Scott", 'login': "msc"}]) + sendmail.assert_called_once_with( + FROM, ['fallback@example.com'], ANY, + datamanager=ANY) + _, _, msg = sendmail.call_args[0] + self.assertEqual(msg['To'], 'Fallback ') + def suite(): suite = test_suite() diff --git a/view/email_form.xml b/view/email_form.xml index d5bb9febfa..565c122fee 100644 --- a/view/email_form.xml +++ b/view/email_form.xml @@ -1,19 +1,25 @@ -
+