mirror of
https://github.com/OCA/web.git
synced 2025-02-22 13:21:25 +02:00
Improve logic and add tests
This commit is contained in:
committed by
Jairo Llopis
parent
f916f2c9f1
commit
f2de5d8663
@@ -2,31 +2,45 @@
|
|||||||
# Copyright 2016 ACSONE SA/NV
|
# Copyright 2016 ACSONE SA/NV
|
||||||
# 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 openerp import api, models, _
|
from openerp import api, fields, models, _
|
||||||
|
|
||||||
|
|
||||||
class ResUsers(models.Model):
|
class ResUsers(models.Model):
|
||||||
|
|
||||||
_inherit = 'res.users'
|
_inherit = 'res.users'
|
||||||
|
|
||||||
|
@api.multi
|
||||||
|
@api.depends('create_date')
|
||||||
|
def _compute_channel_names(self):
|
||||||
|
for record in self:
|
||||||
|
res_id = record.id
|
||||||
|
record.notify_info_channel_name = 'notify_info_%s' % res_id
|
||||||
|
record.notify_warning_channel_name = 'notify_warning_%s' % res_id
|
||||||
|
|
||||||
|
notify_info_channel_name = fields.Char(
|
||||||
|
compute='_compute_channel_names', store=True, required=True)
|
||||||
|
notify_warning_channel_name = fields.Char(
|
||||||
|
compute='_compute_channel_names', store=True, requried=True)
|
||||||
|
|
||||||
@api.multi
|
@api.multi
|
||||||
def notify_info(self, message, title=None, sticky=False):
|
def notify_info(self, message, title=None, sticky=False):
|
||||||
title = title or _('Information')
|
title = title or _('Information')
|
||||||
self._notify_channel('notify_info', message, title, sticky)
|
self._notify_channel(
|
||||||
|
'notify_info_channel_name', message, title, sticky)
|
||||||
|
|
||||||
@api.multi
|
@api.multi
|
||||||
def notify_warning(self, message, title=None, sticky=False):
|
def notify_warning(self, message, title=None, sticky=False):
|
||||||
title = title or _('Warning')
|
title = title or _('Warning')
|
||||||
self._notify_channel('notify_warning', message, title, sticky)
|
self._notify_channel(
|
||||||
|
'notify_warning_channel_name', message, title, sticky)
|
||||||
|
|
||||||
@api.multi
|
@api.multi
|
||||||
def _notify_channel(self, channel_name_prefix, message, title, sticky):
|
def _notify_channel(self, channel_name_field, message, title, sticky):
|
||||||
notification = {
|
bus_message = {
|
||||||
'message': message,
|
'message': message,
|
||||||
'title': title,
|
'title': title,
|
||||||
'sticky': sticky
|
'sticky': sticky
|
||||||
}
|
}
|
||||||
bus_bus = self.env['bus.bus']
|
notifications = [(getattr(record, channel_name_field), bus_message)
|
||||||
for record in self:
|
for record in self]
|
||||||
channel_name = channel_name_prefix + "_%s" % record.id
|
self.env['bus.bus'].sendmany(notifications)
|
||||||
bus_bus.sendone(channel_name, notification)
|
|
||||||
|
|||||||
1
web_notify/tests/__init__.py
Normal file
1
web_notify/tests/__init__.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
from . import test_res_users
|
||||||
51
web_notify/tests/test_res_users.py
Normal file
51
web_notify/tests/test_res_users.py
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# Copyright 2016 ACSONE SA/NV
|
||||||
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||||
|
|
||||||
|
from openerp.tests import common
|
||||||
|
from openerp.addons.bus.models.bus import json_dump
|
||||||
|
import mock
|
||||||
|
|
||||||
|
|
||||||
|
class TestResUsers(common.TransactionCase):
|
||||||
|
|
||||||
|
def test_notify_info(self):
|
||||||
|
bus_bus = self.env['bus.bus']
|
||||||
|
domain = [
|
||||||
|
('channel', '=',
|
||||||
|
json_dump(self.env.user.notify_info_channel_name))
|
||||||
|
]
|
||||||
|
existing = bus_bus.search(domain)
|
||||||
|
self.env.user.notify_info(
|
||||||
|
message='message', title='title', sticky=True)
|
||||||
|
news = bus_bus.search(domain) - existing
|
||||||
|
self.assertEqual(1, len(news))
|
||||||
|
self.assertEqual(
|
||||||
|
'{"message":"message","sticky":true,"title":"title"}',
|
||||||
|
news.message)
|
||||||
|
|
||||||
|
def test_notify_warning(self):
|
||||||
|
bus_bus = self.env['bus.bus']
|
||||||
|
domain = [
|
||||||
|
('channel', '=',
|
||||||
|
json_dump(self.env.user.notify_warning_channel_name))
|
||||||
|
]
|
||||||
|
existing = bus_bus.search(domain)
|
||||||
|
self.env.user.notify_warning(
|
||||||
|
message='message', title='title', sticky=True)
|
||||||
|
news = bus_bus.search(domain) - existing
|
||||||
|
self.assertEqual(1, len(news))
|
||||||
|
self.assertEqual(
|
||||||
|
'{"message":"message","sticky":true,"title":"title"}',
|
||||||
|
news.message)
|
||||||
|
|
||||||
|
def test_notify_many(self):
|
||||||
|
# check that the notification of a list of users is done with
|
||||||
|
# a single call to the bus
|
||||||
|
with mock.patch('openerp.addons.bus.models.bus.ImBus.sendmany'
|
||||||
|
) as mockedSendMany:
|
||||||
|
users = self.env.user.search([(1, "=", 1)])
|
||||||
|
self.assertTrue(len(users) > 1)
|
||||||
|
users.notify_warning('message')
|
||||||
|
self.assertEqual(1, mockedSendMany.call_count)
|
||||||
|
self.assertEqual(len(users), len(mockedSendMany.call_args))
|
||||||
Reference in New Issue
Block a user