Make notify options variadic

It opens for extensions.
The implemented options now allow to configure the name and the icon of
the link for the action. Addons could easily add new features as the
arguments are no longer predefined, they'll all be available in the
'options' dictionary.
pull/1029/head
Guewen Baconnier 2018-06-27 22:45:55 +02:00 committed by Yannick Vaucher
parent bf7fda1980
commit 4fd13e22da
5 changed files with 41 additions and 34 deletions

View File

@ -50,7 +50,12 @@ The action can be used using the ``action`` keyword:
'res_id': self.id, 'res_id': self.id,
'views': [(False, 'form')], 'views': [(False, 'form')],
}) })
self.env.user.notify_info('My information message', action=action) self.env.user.notify_info(
'My information message',
action=action,
# optional
action_link_name=_('Open Sale'),
)
Installation Installation

View File

@ -26,32 +26,35 @@ class ResUsers(models.Model):
@api.multi @api.multi
def notify_info(self, message, title=None, sticky=False, def notify_info(self, message, title=None, sticky=False,
show_reload=False, action=None): show_reload=False, action=None,
action_link_name=None, **options):
title = title or _('Information') title = title or _('Information')
self._notify_channel( self._notify_channel(
'notify_info_channel_name', message, title, 'notify_info_channel_name', message, title,
sticky, show_reload, action) sticky=sticky, show_reload=show_reload, action=action,
action_link_name=action_link_name, **options
)
@api.multi @api.multi
def notify_warning(self, message, title=None, sticky=False, def notify_warning(self, message, title=None, sticky=False,
show_reload=False, action=None): show_reload=False, action=None,
action_link_name=None, **options):
title = title or _('Warning') title = title or _('Warning')
self._notify_channel( self._notify_channel(
'notify_warning_channel_name', message, title, 'notify_warning_channel_name', message, title,
sticky, show_reload, action) sticky=sticky, show_reload=show_reload, action=action,
action_link_name=action_link_name, **options
)
@api.multi @api.multi
def _notify_channel(self, channel_name_field, message, title, sticky, def _notify_channel(self, channel_name_field, message, title, **options):
show_reload, action): if options.get('action'):
if action: options['action'] = clean_action(options['action'])
action = clean_action(action)
bus_message = { bus_message = {
'message': message, 'message': message,
'title': title, 'title': title,
'sticky': sticky,
'show_reload': show_reload,
'action': action,
} }
bus_message.update(options)
notifications = [(getattr(record, channel_name_field), bus_message) notifications = [(getattr(record, channel_name_field), bus_message)
for record in self] for record in self]
self.env['bus.bus'].sendmany(notifications) self.env['bus.bus'].sendmany(notifications)

View File

@ -5,8 +5,7 @@ odoo.define('web_notify.notification', function (require) {
var base_notification = require('web.notification'), var base_notification = require('web.notification'),
WebClient = require('web.WebClient'), WebClient = require('web.WebClient'),
Notification = base_notification.Notification, Notification = base_notification.Notification;
Warning = base_notification.Warning;
var InteractiveNotification = Notification.extend({ var InteractiveNotification = Notification.extend({
template: 'InteractiveNotification', template: 'InteractiveNotification',
@ -23,15 +22,15 @@ odoo.define('web_notify.notification', function (require) {
} }
} }
), ),
init: function(parent, title, text, sticky, options) { init: function(parent, title, text, options) {
this._super.apply(this, [parent, title, text, sticky]);
this.options = options || {}; this.options = options || {};
var sticky = this.options.sticky;
this._super.apply(this, [parent, title, text, sticky]);
}, },
reload_active_view: function() { reload_active_view: function() {
this.trigger_up('reload_active_view'); this.trigger_up('reload_active_view');
}, },
button_do_action: function() { button_do_action: function() {
console.log(this.options.action);
this.getParent().do_action(this.options.action); this.getParent().do_action(this.options.action);
} }
}); });
@ -41,11 +40,11 @@ odoo.define('web_notify.notification', function (require) {
}); });
base_notification.NotificationManager.include({ base_notification.NotificationManager.include({
interactive_notify(title, text, sticky, options) { interactive_notify(title, text, options) {
return this.display(new InteractiveNotification(this, title, text, sticky, options)); return this.display(new InteractiveNotification(this, title, text, options));
}, },
interactive_warn(title, text, sticky, options) { interactive_warn(title, text, options) {
return this.display(new InteractiveWarning(this, title, text, sticky, options)); return this.display(new InteractiveWarning(this, title, text, options));
} }
}); });

View File

@ -8,17 +8,15 @@ var core = require('web.core'),
Widget.include({ Widget.include({
do_interactive_notify: function(title, message, sticky, options) { do_interactive_notify: function(title, message, options) {
this.trigger_up( this.trigger_up(
'interactive_notification', 'interactive_notification',
{title: title, message: message, {title: title, message: message, options: options});
sticky: sticky, options: options});
}, },
do_interactive_warn: function(title, message, sticky, options) { do_interactive_warn: function(title, message, options) {
this.trigger_up( this.trigger_up(
'interactive_warning', 'interactive_warning',
{title: title, message: message, {title: title, message: message, options: options});
sticky: sticky, options: options});
}, },
}); });
@ -31,16 +29,14 @@ WebClient.include({
interactive_notification: function (e) { interactive_notification: function (e) {
if(this.notification_manager) { if(this.notification_manager) {
this.notification_manager.interactive_notify( this.notification_manager.interactive_notify(
e.data.title, e.data.message, e.data.title, e.data.message, e.data.options
e.data.sticky, e.data.options
); );
} }
}, },
interactive_warning: function (e) { interactive_warning: function (e) {
if(this.notification_manager) { if(this.notification_manager) {
this.notification_manager.interactive_warn( this.notification_manager.interactive_warn(
e.data.title, e.data.message, e.data.title, e.data.message, e.data.options
e.data.sticky, e.data.options
); );
} }
} }
@ -92,14 +88,14 @@ WebClient.include({
on_message_warning: function(message){ on_message_warning: function(message){
if(this.notification_manager) { if(this.notification_manager) {
this.notification_manager.do_interactive_warn( this.notification_manager.do_interactive_warn(
message.title, message.message, message.sticky, message message.title, message.message, message
); );
} }
}, },
on_message_info: function(message){ on_message_info: function(message){
if(this.notification_manager) { if(this.notification_manager) {
this.notification_manager.do_interactive_notify( this.notification_manager.do_interactive_notify(
message.title, message.message, message.sticky, message message.title, message.message, message
); );
} }
} }

View File

@ -12,7 +12,11 @@
<t t-name="Notification.do_action"> <t t-name="Notification.do_action">
<div class="o_notification_action" t-if="widget.options.action"> <div class="o_notification_action" t-if="widget.options.action">
<a href="#" class="o_notification_do_action"> <a href="#" class="o_notification_do_action">
<span class="fa fa-arrow-circle-left"/> Open <span t-att-class="'fa ' + (widget.options.action_fa_icon ? widget.options.action_fa_icon : 'fa-arrow-circle-left')"/>
<t t-if="widget.options.action_link_name">
<t t-esc="widget.options.action_link_name"/>
</t>
<t t-else="">Open</t>
</a> </a>
</div> </div>
</t> </t>