Add fallback users for the recipients

issue7029
review39051002
This commit is contained in:
Nicolas Évrard
2018-03-10 17:37:01 +01:00
parent 3d101183a8
commit edf9f9b2bf
4 changed files with 128 additions and 32 deletions
+1
View File
@@ -1,3 +1,4 @@
* Add fallback users for recipients
* Add contact mechanism usage
Version 4.6.0 - 2017-10-30
+44
View File
@@ -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)]
+74 -29
View File
@@ -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 <fallback@example.com>')
def suite():
suite = test_suite()
+9 -3
View File
@@ -1,19 +1,25 @@
<?xml version="1.0"?>
<!-- This file is part of Tryton. The COPYRIGHT file at the top level of
this repository contains the full copyright notices and license terms. -->
<form col="2">
<form>
<label name="from_"/>
<field name="from_"/>
<label name="content"/>
<field name="content"/>
<label name="recipients"/>
<field name="recipients"/>
<label name="fallback_recipients"/>
<field name="fallback_recipients"/>
<label name="recipients_secondary"/>
<field name="recipients_secondary"/>
<label name="fallback_recipients_secondary"/>
<field name="fallback_recipients_secondary"/>
<label name="recipients_hidden"/>
<field name="recipients_hidden"/>
<label name="fallback_recipients_hidden"/>
<field name="fallback_recipients_hidden"/>
<label name="contact_mechanism"/>
<field name="contact_mechanism"/>
<field name="attachments" colspan="2"/>
<field name="triggers" colspan="2"/>
<field name="attachments" colspan="4"/>
<field name="triggers" colspan="4"/>
</form>