From 8d3f2749e429dbcddcceeac50e1667501e808f3c Mon Sep 17 00:00:00 2001 From: Guewen Baconnier Date: Wed, 27 Jun 2018 14:39:47 +0200 Subject: [PATCH 1/5] web_notify: add feature to reload current view --- web_notify/README.rst | 10 +++++ web_notify/__manifest__.py | 5 ++- web_notify/models/res_users.py | 17 +++++--- web_notify/static/src/css/notification.less | 5 +++ web_notify/static/src/js/notification.js | 40 ++++++++++++++++++ web_notify/static/src/js/web_client.js | 47 +++++++++++++++++++-- web_notify/static/src/xml/notification.xml | 24 +++++++++++ web_notify/views/web_notify.xml | 2 + 8 files changed, 138 insertions(+), 12 deletions(-) create mode 100644 web_notify/static/src/css/notification.less create mode 100644 web_notify/static/src/js/notification.js create mode 100644 web_notify/static/src/xml/notification.xml diff --git a/web_notify/README.rst b/web_notify/README.rst index 2fa24871..0d95e255 100644 --- a/web_notify/README.rst +++ b/web_notify/README.rst @@ -30,6 +30,16 @@ or :scale: 80 % :alt: Sample notifications +The notifications can optionally have some action buttons. + +* One allowing to refresh the active view + +It is activated when sending the notification with: + +.. code-block:: python + + self.env.user.notify_info('My information message', show_reload=True) + Installation ============ diff --git a/web_notify/__manifest__.py b/web_notify/__manifest__.py index 858c3be9..c84ae279 100644 --- a/web_notify/__manifest__.py +++ b/web_notify/__manifest__.py @@ -6,7 +6,7 @@ 'name': 'Web Notify', 'summary': """ Send notification messages to user""", - 'version': '10.0.1.0.0', + 'version': '10.0.1.1.0', 'description': 'Web Notify', 'license': 'AGPL-3', 'author': 'ACSONE SA/NV,Odoo Community Association (OCA)', @@ -18,7 +18,8 @@ 'data': [ 'views/web_notify.xml' ], - 'demo': [ + 'qweb': [ + 'static/src/xml/*.xml', ], 'installable': True, } diff --git a/web_notify/models/res_users.py b/web_notify/models/res_users.py index 29f0eca1..30165f17 100644 --- a/web_notify/models/res_users.py +++ b/web_notify/models/res_users.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # Copyright 2016 ACSONE SA/NV +# Copyright 2018 Camptocamp # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). from odoo import api, fields, models, _ @@ -23,23 +24,27 @@ class ResUsers(models.Model): compute='_compute_channel_names') @api.multi - def notify_info(self, message, title=None, sticky=False): + def notify_info(self, message, title=None, sticky=False, + show_reload=False): title = title or _('Information') self._notify_channel( - 'notify_info_channel_name', message, title, sticky) + 'notify_info_channel_name', message, title, sticky, show_reload) @api.multi - def notify_warning(self, message, title=None, sticky=False): + def notify_warning(self, message, title=None, sticky=False, + show_reload=False): title = title or _('Warning') self._notify_channel( - 'notify_warning_channel_name', message, title, sticky) + 'notify_warning_channel_name', message, title, sticky, show_reload) @api.multi - def _notify_channel(self, channel_name_field, message, title, sticky): + def _notify_channel(self, channel_name_field, message, title, sticky, + show_reload): bus_message = { 'message': message, 'title': title, - 'sticky': sticky + 'sticky': sticky, + 'show_reload': show_reload, } notifications = [(getattr(record, channel_name_field), bus_message) for record in self] diff --git a/web_notify/static/src/css/notification.less b/web_notify/static/src/css/notification.less new file mode 100644 index 00000000..1f1ee9a6 --- /dev/null +++ b/web_notify/static/src/css/notification.less @@ -0,0 +1,5 @@ +.o_notification { + .o_notification_reload { + padding: 10px; + } +} diff --git a/web_notify/static/src/js/notification.js b/web_notify/static/src/js/notification.js new file mode 100644 index 00000000..383b1d3e --- /dev/null +++ b/web_notify/static/src/js/notification.js @@ -0,0 +1,40 @@ +/* Copyright 2018 Camptocamp + * License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */ +odoo.define('web_notify.notification', function (require) { + "use strict"; + + var base_notification = require('web.notification'), + WebClient = require('web.WebClient'), + Notification = base_notification.Notification, + Warning = base_notification.Warning; + + Notification.include({ + events: _.extend( + {}, + Notification.prototype.events, + {'click .o_view_reload': function(e){ + e.preventDefault(); + this.reload_active_view(); + } + } + ), + init: function(parent, title, text, sticky, options) { + this._super.apply(this, arguments); + this.options = options || {}; + }, + reload_active_view: function() { + this.trigger_up('reload_active_view'); + }, + }); + + base_notification.NotificationManager.include({ + notify: function(title, text, sticky, options) { + return this.display(new Notification(this, title, text, sticky, options)); + }, + warn: function(title, text, sticky, options) { + return this.display(new Warning(this, title, text, sticky, options)); + }, + + }); + +}); diff --git a/web_notify/static/src/js/web_client.js b/web_notify/static/src/js/web_client.js index 1ee99224..6fceb35b 100644 --- a/web_notify/static/src/js/web_client.js +++ b/web_notify/static/src/js/web_client.js @@ -1,13 +1,52 @@ odoo.define('web_notify.WebClient', function (require) { "use strict"; -var WebClient = require('web.WebClient'); -var base_bus = require('bus.bus'); +var core = require('web.core'), + WebClient = require('web.WebClient'), + base_bus = require('bus.bus'), + Widget = require('web.Widget'); + + +Widget.include({ + do_notify: function(title, message, sticky, options) { + this.trigger_up('notification', {title: title, message: message, sticky: sticky, options: options}); + }, + do_warn: function(title, message, sticky, options) { + this.trigger_up('warning', {title: title, message: message, sticky: sticky, options: options}); + }, +}); + WebClient.include({ + custom_events: _.extend( + {}, + WebClient.prototype.custom_events, + {reload_active_view: 'reload_active_view', + notification: function (e) { + if(this.notification_manager) { + this.notification_manager.notify(e.data.title, e.data.message, e.data.sticky, e.data.options); + } + }, + warning: function (e) { + if(this.notification_manager) { + this.notification_manager.warn(e.data.title, e.data.message, e.data.sticky, e.data.options); + } + } + } + ), init: function(parent, client_options){ this._super(parent, client_options); }, + reload_active_view: function(){ + var action_manager = this.action_manager; + if(!action_manager){ + return; + } + var active_view = action_manager.inner_widget.active_view; + if(active_view) { + active_view.controller.reload(); + } + }, show_application: function() { var res = this._super(); this.start_polling(); @@ -40,12 +79,12 @@ WebClient.include({ }, on_message_warning: function(message){ if(this.notification_manager) { - this.notification_manager.do_warn(message.title, message.message, message.sticky); + this.notification_manager.do_warn(message.title, message.message, message.sticky, message); } }, on_message_info: function(message){ if(this.notification_manager) { - this.notification_manager.do_notify(message.title, message.message, message.sticky); + this.notification_manager.do_notify(message.title, message.message, message.sticky, message); } } }); diff --git a/web_notify/static/src/xml/notification.xml b/web_notify/static/src/xml/notification.xml new file mode 100644 index 00000000..d73fb6f5 --- /dev/null +++ b/web_notify/static/src/xml/notification.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/web_notify/views/web_notify.xml b/web_notify/views/web_notify.xml index b85d2e6f..4d009595 100644 --- a/web_notify/views/web_notify.xml +++ b/web_notify/views/web_notify.xml @@ -3,7 +3,9 @@