Browse Source

web_notify: add feature to reload current view

pull/1029/head
Guewen Baconnier 7 years ago
committed by Yannick Vaucher
parent
commit
8d3f2749e4
  1. 10
      web_notify/README.rst
  2. 5
      web_notify/__manifest__.py
  3. 17
      web_notify/models/res_users.py
  4. 5
      web_notify/static/src/css/notification.less
  5. 40
      web_notify/static/src/js/notification.js
  6. 47
      web_notify/static/src/js/web_client.js
  7. 24
      web_notify/static/src/xml/notification.xml
  8. 2
      web_notify/views/web_notify.xml

10
web_notify/README.rst

@ -30,6 +30,16 @@ or
:scale: 80 % :scale: 80 %
:alt: Sample notifications :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 Installation
============ ============

5
web_notify/__manifest__.py

@ -6,7 +6,7 @@
'name': 'Web Notify', 'name': 'Web Notify',
'summary': """ 'summary': """
Send notification messages to user""", Send notification messages to user""",
'version': '10.0.1.0.0',
'version': '10.0.1.1.0',
'description': 'Web Notify', 'description': 'Web Notify',
'license': 'AGPL-3', 'license': 'AGPL-3',
'author': 'ACSONE SA/NV,Odoo Community Association (OCA)', 'author': 'ACSONE SA/NV,Odoo Community Association (OCA)',
@ -18,7 +18,8 @@
'data': [ 'data': [
'views/web_notify.xml' 'views/web_notify.xml'
], ],
'demo': [
'qweb': [
'static/src/xml/*.xml',
], ],
'installable': True, 'installable': True,
} }

17
web_notify/models/res_users.py

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Copyright 2016 ACSONE SA/NV # Copyright 2016 ACSONE SA/NV
# Copyright 2018 Camptocamp
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import api, fields, models, _ from odoo import api, fields, models, _
@ -23,23 +24,27 @@ class ResUsers(models.Model):
compute='_compute_channel_names') compute='_compute_channel_names')
@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):
title = title or _('Information') title = title or _('Information')
self._notify_channel( self._notify_channel(
'notify_info_channel_name', message, title, sticky)
'notify_info_channel_name', message, title, sticky, show_reload)
@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):
title = title or _('Warning') title = title or _('Warning')
self._notify_channel( self._notify_channel(
'notify_warning_channel_name', message, title, sticky)
'notify_warning_channel_name', message, title, sticky, show_reload)
@api.multi @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 = { bus_message = {
'message': message, 'message': message,
'title': title, 'title': title,
'sticky': sticky
'sticky': sticky,
'show_reload': show_reload,
} }
notifications = [(getattr(record, channel_name_field), bus_message) notifications = [(getattr(record, channel_name_field), bus_message)
for record in self] for record in self]

5
web_notify/static/src/css/notification.less

@ -0,0 +1,5 @@
.o_notification {
.o_notification_reload {
padding: 10px;
}
}

40
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));
},
});
});

47
web_notify/static/src/js/web_client.js

@ -1,13 +1,52 @@
odoo.define('web_notify.WebClient', function (require) { odoo.define('web_notify.WebClient', function (require) {
"use strict"; "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({ 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){ init: function(parent, client_options){
this._super(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() { show_application: function() {
var res = this._super(); var res = this._super();
this.start_polling(); this.start_polling();
@ -40,12 +79,12 @@ WebClient.include({
}, },
on_message_warning: function(message){ on_message_warning: function(message){
if(this.notification_manager) { 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){ on_message_info: function(message){
if(this.notification_manager) { 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);
} }
} }
}); });

24
web_notify/static/src/xml/notification.xml

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<templates id="template" xml:space="preserve">
<t t-name="Notification.reload">
<div class="o_notification_reload" t-if="widget.options.show_reload">
<a href="#" class="o_view_reload">
<span class="fa fa-refresh"/> Reload current view
</a>
</div>
</t>
<t t-extend="Notification">
<t t-jquery=".o_notification_content" t-operation="after">
<t t-call="Notification.reload"/>
</t>
</t>
<t t-extend="Warning">
<t t-jquery=".o_notification_content" t-operation="after">
<t t-call="Notification.reload"/>
</t>
</t>
</templates>

2
web_notify/views/web_notify.xml

@ -3,7 +3,9 @@
<data> <data>
<template id="assets_backend" name="web_noify assets" inherit_id="web.assets_backend"> <template id="assets_backend" name="web_noify assets" inherit_id="web.assets_backend">
<xpath expr="." position="inside"> <xpath expr="." position="inside">
<link rel="stylesheet" href="/web_notify/static/src/css/notification.less"/>
<script type="text/javascript" src="/web_notify/static/src/js/web_client.js"/> <script type="text/javascript" src="/web_notify/static/src/js/web_client.js"/>
<script type="text/javascript" src="/web_notify/static/src/js/notification.js"/>
</xpath> </xpath>
</template> </template>
</data> </data>

Loading…
Cancel
Save