Commit c0790c1d authored by Nhomar Hernández's avatar Nhomar Hernández 💬
Browse files

Merge branch '8.0-jose-message-post-migrated' of...

Merge branch '8.0-jose-message-post-migrated' of https://github.com/vauxoo-dev/addons-vauxoo into 8.0-purchase_requisition_for_everybody
parents 804104f9 122b4154
Message Post Log
================
TODO: Add module description
This module adds an extended log in your model that appears in the mail.message
for each change made in the fields unlike the traditional way in wich Odoo did.
Features
--------
The extended log is added to your model's module in the following way:
_name = "account.invoice"
_inherit = ['account.invoice', 'message.post.show.all']
Also, if you need it, there is a testing module called message_post_test to
prove the funtionality.
Requirements:
-------------
- Go to https://github.com/Vauxoo/addons-vauxoo and download the repo in order to install message_post_model and message_post_test modules.
Contributors
------------
* José Morales <jose@vauxoo.com>
Maintainer
----------
.. image:: https://www.vauxoo.com/logo.png
:alt: Vauxoo
:target: https://vauxoo.com
This module is maintained by Vauxoo.
A latinamerican company that provides training, coaching,
development and implementation of enterprise management
sytems and bases its entire operation strategy in the use
of Open Source Software and its main product is odoo.
To contribute to this module, please visit http://www.vauxoo.com.
......@@ -22,11 +22,10 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# #########################################################################
from openerp.osv import osv
from openerp.tools.translate import _
from openerp import models, api, _
class MessagePostShowAll(osv.Model):
class MessagePostShowAll(models.Model):
"""With this object you can add an extensive log in your model like the
traditional message log don't does
......@@ -40,8 +39,9 @@ class MessagePostShowAll(osv.Model):
_inherit = ['mail.thread']
# pylint: disable=W0622
def get_last_value(self, cr, uid, ids, model=None, field=None,
field_type=None, context=None):
@api.model
def get_last_value(self, ids, model=None, field=None,
fieldtype=None):
"""Return the last value of a record in the model to show a post with the
change
@param ids: int with id record
......@@ -50,25 +50,24 @@ class MessagePostShowAll(osv.Model):
return the value of the field
"""
context = context or {}
field = ids and field or []
model_obj = self.pool.get(model)
model_brw = model_obj.browse(cr, uid, ids, context=context)
if 'many2one' in field_type:
model_obj = self.env[model]
model_brw = model_obj.browse(ids)
if 'many2one' in fieldtype:
value = field and model_brw[field] and \
model_brw[field].name_get() or ''
value = value and value[0][1]
elif 'many2many' in field_type:
elif 'many2many' in fieldtype:
value = [i.id for i in model_brw[field]]
else:
value = field and model_brw[field] or ''
return field and value or ''
def prepare_many_info(self, cr, uid, ids, records, string, n_obj,
last=None, context=None):
context = context or {}
@api.model
def prepare_many_info(self, ids, records, string, n_obj,
last=None):
info = {
0: _('Created New Line'),
1: _('Updated Line'),
......@@ -77,9 +76,10 @@ class MessagePostShowAll(osv.Model):
6: _('many2many'),
}
message = '<ul>'
obj = self.pool.get(n_obj)
obj = self.env[n_obj]
r_name = obj._rec_name
mes = False
last = last or []
for val in records:
if val and info.get(val[0], False):
if val[0] == 0:
......@@ -87,7 +87,7 @@ class MessagePostShowAll(osv.Model):
message = u'%s\n<li><b>%s<b>: %s</li>' % \
(message, info.get(val[0]), value.get(r_name),)
elif val[0] in (2, 3):
model_brw = obj.browse(cr, uid, val[1], context=context)
model_brw = obj.browse(val[1])
last_value = model_brw.name_get()
last_value = last_value and last_value[0][1]
value = val[1]
......@@ -100,14 +100,14 @@ class MessagePostShowAll(osv.Model):
add = _('Added')
delete = _('Deleted')
if lastv and not new:
dele = [obj.name_get(cr, uid, [i])[0][1]
dele = [obj.browse(i).name_get()[0][1]
for i in lastv]
mes = ' - '.join(dele)
message = u'%s\n<li><b>%s %s<b>: %s</li>' % \
(message, add, string, mes)
if not lastv and new:
dele = [obj.name_get(cr, uid, [i])[0][1] for i in new]
dele = [obj.browse(i).name_get()[0][1] for i in new]
mes = '-'.join(dele)
message = u'%s\n<li><b>%s %s<b>: %s</li>' % \
(message, delete, string, mes)
......@@ -116,33 +116,30 @@ class MessagePostShowAll(osv.Model):
vals = val[2]
id_line = 0
for field in vals:
if obj._columns[field]._type in ('one2many',
'many2many'):
is_many = obj._columns[field]._type == 'many2many'
if obj._fields[field].type in \
('one2many', 'many2many'):
is_many = obj._fields[field].type == 'many2many'
last_value = is_many and self.get_last_value(
cr, uid, val[1], n_obj, field, 'many2many',
context)
field_str = obj._columns[field].string
new_n_obj = obj._columns[field]._obj
mes = self.prepare_many_info(cr, uid, val[1],
val[1], n_obj, field, 'many2many')
field_str = obj._fields[field].string
new_n_obj = obj._fields[field].comodel_name
mes = self.prepare_many_info(val[1],
vals[field],
field_str,
new_n_obj,
last_value,
context)
last_value)
elif obj._columns[field]._type == 'many2one':
mes = self.prepare_many2one_info(cr, uid, val[1],
elif obj._fields[field].type == 'many2one':
mes = self.prepare_many2one_info(val[1],
n_obj,
field,
vals,
context)
vals)
elif 'many' not in obj._columns[field]._type:
mes = self.prepare_simple_info(cr, uid, val[1],
elif 'many' not in obj._fields[field].type:
mes = self.prepare_simple_info(val[1],
n_obj, field,
vals, context)
vals)
if mes and mes != '<p>':
message = id_line != val[1] and \
_('%s\n<h3>Line %s</h3>' % (message, val[1])) \
......@@ -153,79 +150,76 @@ class MessagePostShowAll(osv.Model):
message = '%s\n</ul>' % message
return message
def prepare_many2one_info(self, cr, uid, ids, n_obj, field, vals,
context=None):
context = context or {}
obj = self.pool.get(n_obj)
@api.model
def prepare_many2one_info(self, ids, n_obj, field, vals):
obj = self.env[n_obj]
message = '<p>'
last_value = self.get_last_value(
cr, uid, ids, obj._name, field, obj._columns[field]._type, context)
model_obj = self.pool.get(obj._columns[field]._obj)
model_brw = model_obj.browse(cr, uid, vals[field], context=context)
ids, obj._name, field, obj._fields[field].type)
model_obj = self.env[obj._fields[field].comodel_name]
model_brw = model_obj.browse(vals[field])
new_value = model_brw.name_get()
new_value = new_value and new_value[0][1]
if not (last_value == new_value) and any((new_value, last_value)):
message = u'<li><b>%s<b>: %s → %s</li>' % \
(obj._columns[field].string,
(obj._fields[field].string,
last_value,
new_value)
return message
def prepare_simple_info(self, cr, uid, ids, n_obj, field,
vals, context=None):
context = context or {}
obj = self.pool.get(n_obj)
@api.model
def prepare_simple_info(self, ids, n_obj, field,
vals):
obj = self.env[n_obj]
message = '<p>'
last_value = self.get_last_value(
cr, uid, ids, obj._name, field, obj._columns[field]._type, context)
ids, obj._name, field, obj._fields[field].type)
if (not (unicode(last_value) == unicode(vals[field]))
and any((last_value, vals[field]))):
if ((unicode(last_value) != unicode(vals[field])) and
any((last_value, vals[field]))):
message = u'<li><b>%s<b>: %s → %s</li>' % \
(obj._columns[field].string,
(obj._fields[field].string,
last_value,
vals[field])
return message
# pylint: disable=W0106
def write(self, cr, uid, ids, vals, context=None):
context = context or {}
for idx in ids:
@api.multi
def write(self, vals):
for idx in self:
body = '<ul>'
message = False
for field in vals:
if self._columns[field]._type in ('one2many', 'many2many'):
is_many = self._columns[field]._type == 'many2many'
if self._fields[field].type in ('one2many', 'many2many'):
is_many = self._fields[field].type == 'many2many'
last_value = is_many and self.get_last_value(
cr, uid, idx, self._name, field, 'many2many', context)
field_str = self._columns[field].string
n_obj = self._columns[field]._obj
idx.id, self._name, field, 'many2many')
field_str = self._fields[field].string
n_obj = self._fields[field].comodel_name
message = self.prepare_many_info(
cr, uid, idx, vals[field], field_str, n_obj,
last_value, context)
idx.id, vals[field], field_str, n_obj,
last_value)
body = len(message.split('\n')) > 2 and '%s\n%s: %s' % (
body, field_str, message)
elif self._columns[field]._type == 'many2one':
message = self.prepare_many2one_info(cr, uid, idx,
elif self._fields[field].type == 'many2one':
message = self.prepare_many2one_info(idx.id,
self._name,
field,
vals,
context)
vals)
body = '%s\n%s' % (body, message)
elif 'many' not in self._columns[field]._type:
elif 'many' not in self._fields[field].type:
message = self.prepare_simple_info(
cr, uid, idx, self._name, field, vals, context)
idx.id, self._name, field, vals)
body = '%s\n%s' % (body, message)
body = body and '%s\n</ul>' % body
if body and message:
self.message_post(cr, uid, [idx], body, _('Changes in Fields'))
res = super(MessagePostShowAll, self).write(cr, uid, ids, vals,
context=context)
idx.message_post(body, _('Changes in Fields'))
res = super(MessagePostShowAll, self).write(vals)
return res
......@@ -9,8 +9,32 @@
Message Post Log
</h2>
<p class="oe_mt32">
TODO: Add module description
This module adds an extended log in your model that appears in the mail.message
for each change made in the fields unlike the traditional way in wich Odoo did.
</p>
<strong>
Features
</strong>
<p class="oe_mt32">
The extended log is added to your model's module in the following way:
</p>
<p>
_name = "account.invoice"
</p>
<p>
_inherit = ['account.invoice', 'message.post.show.all']
</p>
Also, if you need it, there is a testing module called message_post_test to
prove the funtionality.
</p>
<p class="oe_mt32">
Requirements:
</p>
<ul>
<li>
Go to https://github.com/Vauxoo/addons-vauxoo and download the repo in order to install message_post_model and message_post_test modules.
</li>
</ul>
</div>
</section>
<section class="oe_container">
......
......@@ -24,12 +24,15 @@
##########################################################################
from openerp.osv import osv, fields
from openerp import models
from openerp import fields as newfields
class MessagePostTestLine(osv.Model):
_name = 'message.post.test.line'
# pylint: disable=W8105
_columns = {
'name': fields.char('Name'),
'number': fields.integer('Number'),
......@@ -43,6 +46,7 @@ class MessagePostTest(osv.Model):
_name = 'message.post.test'
_inherit = ['message.post.show.all']
# pylint: disable=W8105
_columns = {
'name': fields.char('Name'),
'user_id': fields.many2one('res.users', 'User'),
......@@ -53,3 +57,29 @@ class MessagePostTest(osv.Model):
'user_id', 'Users'),
'check': fields.boolean('Check'),
}
class MessagePostTestNewApi(models.Model):
_name = 'message.post.test.new.api'
_inherit = ['message.post.show.all']
name = newfields.Char('Name')
user_id = newfields.Many2one('res.users', 'User')
number = newfields.Integer('Number')
line_ids = newfields.One2many('message.post.test.line.new.api', 'test_id',
'Lines')
user_ids = newfields.Many2many('res.users', 'test_user_table_new_api',
'test_id',
'user_id', 'Users')
check = newfields.Boolean('Check')
class MessagePostTestLineNewApi(models.Model):
_name = 'message.post.test.line.new.api'
name = newfields.Char('Name')
number = newfields.Integer('Number')
check = newfields.Boolean('Check')
test_id = newfields.Many2one('message.post.test.new.api', 'Test')
"id","name","model_id:id","group_id:id","perm_read","perm_write","perm_create","perm_unlink"
access_message_post_show_test_all,access_message_post_test_show_all,model_message_post_test,,1,1,1,1
access_message_post_show_test_all,access_message_post_test_show_all,model_message_post_test_line,,1,1,1,1
access_message_post_show_test_all_line,access_message_post_test_show_all,model_message_post_test_line,,1,1,1,1
access_message_post_show_test_all_new_api,access_message_post_test_show_all,model_message_post_test_new_api,,1,1,1,1
access_message_post_show_test_all_line_new_api,access_message_post_test_show_all,model_message_post_test_line_new_api,,1,1,1,1
# coding: utf-8
from . import test_message_post
fast_suite = [
test_message_post
]
......@@ -7,98 +7,107 @@ class TestMessagePost(TransactionCase):
def setUp(self):
super(TestMessagePost, self).setUp()
self.message_test = self.registry('message.post.test')
self.message_test_line = self.registry('message.post.test.line')
self.test_models = [('message.post.test', 'message.post.test.line'),
('message.post.test.new.api',
'message.post.test.line.new.api')]
self.user = self.registry('res.users')
self.message = self.registry('mail.message')
def test_log_git_flow_message_test(self):
cr, uid = self.cr, self.uid
user_id_field = self.message_test._columns.get('user_id').string
number_field = self.message_test._columns.get('number').string
line_ids_field = self.message_test._columns.get('line_ids').string
user_ids_field = self.message_test._columns.get('user_ids').string
check_field = self.message_test._columns.get('check').string
user_1 = self.user.create(cr, uid, {'name': 'Test 1',
'login': 'test1'})
user_2 = self.user.create(cr, uid, {'name': 'Test 2',
'login': 'test2'})
message_test_id = self.message_test.create(cr, uid, {
'name': 'Test Message',
'user_id': SUPERUSER_ID,
'check': True,
'number': 56,
'user_ids': [(6, 0, [user_1, user_2, SUPERUSER_ID])],
'line_ids': [
(0, 0, {'name': 'Test 1', 'number': 2, 'check': True}),
(0, 0, {'name': 'Test 2', 'number': 3, 'check': True}),
(0, 0, {'name': 'Test 3', 'number': 4, 'check': True}),
(0, 0, {'name': 'Test 4', 'number': 5, 'check': True}),
],
})
# Added new lines and modifitying simples fields
self.message_test.write(cr, uid, [message_test_id], {
'check': False,
'number': 78,
'user_id': user_1,
'line_ids': [
(0, 0, {'name': 'Test 5'}),
(0, 0, {'name': 'Test 6'}),
]})
i = 0
for main_model, line_model in self.test_models:
i += 1
message_test = self.registry(main_model)
message_test_line = self.registry(line_model)
user_id_field = message_test._fields.get('user_id').string
number_field = message_test._fields.get('number').string
line_ids_field = message_test._fields.get('line_ids').string
user_ids_field = message_test._fields.get('user_ids').string
check_field = message_test._fields.get('check').string
user_1 = self.user.create(cr, uid,
{'name': 'Test 1',
'login': 'test%s' % str(1 + i)})
user_2 = self.user.create(cr, uid,
{'name': 'Test 2',
'login': 'test%s' % str(8 + i)})
message_test_id = message_test.create(cr, uid, {
'name': 'Test Message',
'user_id': SUPERUSER_ID,
'check': True,
'number': 56,
'user_ids': [(6, 0, [user_1, user_2, SUPERUSER_ID])],
'line_ids': [
(0, 0, {'name': 'Test 1', 'number': 2, 'check': True}),
(0, 0, {'name': 'Test 2', 'number': 3, 'check': True}),
(0, 0, {'name': 'Test 3', 'number': 4, 'check': True}),
(0, 0, {'name': 'Test 4', 'number': 5, 'check': True}),
],
})
# Added new lines and modifitying simples fields
message_test.write(cr, uid, [message_test_id], {
'check': False,
'number': 78,
'user_id': user_1,
'line_ids': [
(0, 0, {'name': 'Test 5'}),
(0, 0, {'name': 'Test 6'}),
]})
message_ids = self.message.search(
cr, uid, [
('res_id', '=', message_test_id),
('model', '=', 'message.post.test'),
('body', 'ilike', '%' + check_field + '%'),
('body', 'ilike', '%' + number_field + '%'),
('body', 'ilike', '%' + user_id_field + '%'),
('body', 'ilike', '%' + line_ids_field + '%Created New Line%'),
('body', 'ilike', '%' + 'False' + '%'),
('body', 'ilike', '%' + '78' + '%'),
('body', 'ilike', '%' + 'Test 1' + '%'),
('body', 'ilike', '%' + 'Test 5' + '%'),
('body', 'ilike', '%' + 'Test 6' + '%'),
])
message_ids = self.message.search(
cr, uid, [
('res_id', '=', message_test_id),
('model', '=', main_model),
('body', 'ilike', '%' + check_field + '%'),
('body', 'ilike', '%' + number_field + '%'),
('body', 'ilike', '%' + user_id_field + '%'),
('body', 'ilike', '%' + line_ids_field +
'%Created New Line%'),
('body', 'ilike', '%' + 'False' + '%'),
('body', 'ilike', '%' + '78' + '%'),
('body', 'ilike', '%' + 'Test 1' + '%'),
('body', 'ilike', '%' + 'Test 5' + '%'),
('body', 'ilike', '%' + 'Test 6' + '%'),
])
self.assertGreaterEqual(len(message_ids),
1,
"The last changes were not registred")
# Updating lines and remove one of them
line_ids = self.message_test_line.search(cr, uid, [])
self.message_test.write(cr, uid, [message_test_id], {
'line_ids': [
(1, line_ids[0], {'name': 'Test Update'}),
(2, line_ids[1]),
]})
self.assertGreaterEqual(len(message_ids),
1,
"The last changes were not registred")
# Updating lines and remove one of them
line_ids = message_test_line.search(cr, uid, [])
message_test.write(cr, uid, [message_test_id], {
'line_ids': [
(1, line_ids[0], {'name': 'Test Update'}),
(2, line_ids[1]),
]})
message_ids = self.message.search(
cr, uid, [
('res_id', '=', message_test_id),
('model', '=', 'message.post.test'),
('body', 'ilike', '%' + line_ids_field + '%'),
('body', 'ilike', '%' + 'Test Update' + '%'),
])
message_ids = self.message.search(
cr, uid, [
('res_id', '=', message_test_id),
('model', '=', main_model),
('body', 'ilike', '%' + line_ids_field + '%'),
('body', 'ilike', '%' + 'Test Update' + '%'),
])
self.assertGreaterEqual(len(message_ids),
1,
"The last changes were not registred")
self.assertGreaterEqual(len(message_ids),
1,
"The last changes were not registred")
# Removing an element of a many2many field
# Removing an element of a many2many field
self.message_test.write(cr, uid, [message_test_id], {
'user_ids': [
(6, 0, [SUPERUSER_ID, user_2]),
]})
message_test.write(cr, uid, [message_test_id], {
'user_ids': [
(6, 0, [SUPERUSER_ID, user_2]),
]})
message_ids = self.message.search(
cr, uid, [
('res_id', '=', message_test_id),
('model', '=', 'message.post.test'),
('body', 'ilike', '%' + user_ids_field + '%Deleted%'),
('body', 'ilike', '%' + 'Test 1' + '%'),
])