From 4fd13e22da3786239a08711a8314166b3acc15fa Mon Sep 17 00:00:00 2001 From: Guewen Baconnier Date: Wed, 27 Jun 2018 22:45:55 +0200 Subject: [PATCH] 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. --- web_notify/README.rst | 7 +++++- web_notify/models/res_users.py | 25 ++++++++++++---------- web_notify/static/src/js/notification.js | 17 +++++++-------- web_notify/static/src/js/web_client.js | 20 +++++++---------- web_notify/static/src/xml/notification.xml | 6 +++++- 5 files changed, 41 insertions(+), 34 deletions(-) diff --git a/web_notify/README.rst b/web_notify/README.rst index bff70210..ab0517e5 100644 --- a/web_notify/README.rst +++ b/web_notify/README.rst @@ -50,7 +50,12 @@ The action can be used using the ``action`` keyword: 'res_id': self.id, '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 diff --git a/web_notify/models/res_users.py b/web_notify/models/res_users.py index 56cca763..718a996b 100644 --- a/web_notify/models/res_users.py +++ b/web_notify/models/res_users.py @@ -26,32 +26,35 @@ class ResUsers(models.Model): @api.multi 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') self._notify_channel( '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 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') self._notify_channel( '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 - def _notify_channel(self, channel_name_field, message, title, sticky, - show_reload, action): - if action: - action = clean_action(action) + def _notify_channel(self, channel_name_field, message, title, **options): + if options.get('action'): + options['action'] = clean_action(options['action']) bus_message = { 'message': message, 'title': title, - 'sticky': sticky, - 'show_reload': show_reload, - 'action': action, } + bus_message.update(options) notifications = [(getattr(record, channel_name_field), bus_message) for record in self] self.env['bus.bus'].sendmany(notifications) diff --git a/web_notify/static/src/js/notification.js b/web_notify/static/src/js/notification.js index e4cb27ba..38cb87ae 100644 --- a/web_notify/static/src/js/notification.js +++ b/web_notify/static/src/js/notification.js @@ -5,8 +5,7 @@ odoo.define('web_notify.notification', function (require) { var base_notification = require('web.notification'), WebClient = require('web.WebClient'), - Notification = base_notification.Notification, - Warning = base_notification.Warning; + Notification = base_notification.Notification; var InteractiveNotification = Notification.extend({ template: 'InteractiveNotification', @@ -23,15 +22,15 @@ odoo.define('web_notify.notification', function (require) { } } ), - init: function(parent, title, text, sticky, options) { - this._super.apply(this, [parent, title, text, sticky]); + init: function(parent, title, text, options) { this.options = options || {}; + var sticky = this.options.sticky; + this._super.apply(this, [parent, title, text, sticky]); }, reload_active_view: function() { this.trigger_up('reload_active_view'); }, button_do_action: function() { - console.log(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({ - interactive_notify(title, text, sticky, options) { - return this.display(new InteractiveNotification(this, title, text, sticky, options)); + interactive_notify(title, text, options) { + return this.display(new InteractiveNotification(this, title, text, options)); }, - interactive_warn(title, text, sticky, options) { - return this.display(new InteractiveWarning(this, title, text, sticky, options)); + interactive_warn(title, text, options) { + return this.display(new InteractiveWarning(this, title, text, options)); } }); diff --git a/web_notify/static/src/js/web_client.js b/web_notify/static/src/js/web_client.js index d7309726..d87b52ff 100644 --- a/web_notify/static/src/js/web_client.js +++ b/web_notify/static/src/js/web_client.js @@ -8,17 +8,15 @@ var core = require('web.core'), Widget.include({ - do_interactive_notify: function(title, message, sticky, options) { + do_interactive_notify: function(title, message, options) { this.trigger_up( 'interactive_notification', - {title: title, message: message, - sticky: sticky, options: options}); + {title: title, message: message, options: options}); }, - do_interactive_warn: function(title, message, sticky, options) { + do_interactive_warn: function(title, message, options) { this.trigger_up( 'interactive_warning', - {title: title, message: message, - sticky: sticky, options: options}); + {title: title, message: message, options: options}); }, }); @@ -31,16 +29,14 @@ WebClient.include({ interactive_notification: function (e) { if(this.notification_manager) { this.notification_manager.interactive_notify( - e.data.title, e.data.message, - e.data.sticky, e.data.options + e.data.title, e.data.message, e.data.options ); } }, interactive_warning: function (e) { if(this.notification_manager) { this.notification_manager.interactive_warn( - e.data.title, e.data.message, - e.data.sticky, e.data.options + e.data.title, e.data.message, e.data.options ); } } @@ -92,14 +88,14 @@ WebClient.include({ on_message_warning: function(message){ if(this.notification_manager) { this.notification_manager.do_interactive_warn( - message.title, message.message, message.sticky, message + message.title, message.message, message ); } }, on_message_info: function(message){ if(this.notification_manager) { this.notification_manager.do_interactive_notify( - message.title, message.message, message.sticky, message + message.title, message.message, message ); } } diff --git a/web_notify/static/src/xml/notification.xml b/web_notify/static/src/xml/notification.xml index f620fa5c..3fbec2f1 100644 --- a/web_notify/static/src/xml/notification.xml +++ b/web_notify/static/src/xml/notification.xml @@ -12,7 +12,11 @@