[FIX] mail.message: message_read: fixed a call to a dict keyu not necessarily existing. Cleaned a bit the code, as well as the code of the related tests.

bzr revid: tde@openerp.com-20120831143707-ezcn17g5u39tcepq
This commit is contained in:
Thibault Delavallée
2012-08-31 16:37:07 +02:00
parent c973288701
commit bdd972e3c9
2 changed files with 27 additions and 59 deletions
+11 -25
View File
@@ -184,42 +184,29 @@ class mail_message(osv.Model):
return [msg_dict] + child_ids
# Depth-first flattening
for message in messages:
if message['type'] == 'expandable':
if message.get('type') == 'expandable':
continue
message['child_ids'] = self.message_read_tree_flatten(cr, uid, message['child_ids'], current_level+1, level, context=context)
# Flatten if above maximum depth
if current_level < level:
return messages
new_list = []
for x in range(0, len(messages)):
flatenned = _flatten(messages[x])
for flat in flatenned:
new_list.append(flat)
messages = new_list
return messages
def _debug_print_tree(self, tree, prefix=''):
for elem in tree:
print '%s%s (%s childs: %s)' % (prefix, elem['id'], len(elem['child_ids']), [xelem['id'] for xelem in elem['child_ids']])
if elem['child_ids']:
self._debug_print_tree(elem['child_ids'], prefix+'--')
for message in messages:
for flat_message in _flatten(message):
new_list.append(flat_message)
return new_list
def message_read(self, cr, uid, ids=False, domain=[], thread_level=0, limit=None, context=None):
"""
If IDS are provided, fetch these records, otherwise use the domain to
fetch the matching records. After having fetched the records provided
by IDS, it will fetch children (according to thread_level).
Return [
]
""" If IDs are provided, fetch these records. Otherwise use the domain
to fetch the matching records.
After having fetched the records provided by IDs, it will fetch the
parents to have well-formed threads.
:return list: list of trees of messages
"""
limit = limit or self._message_read_limit
context = context or {}
if ids is False:
if not ids:
ids = self.search(cr, uid, domain, context=context, limit=limit)
# FP Todo: flatten to max X level of mail_thread
messages = self.browse(cr, uid, ids, context=context)
result = []
@@ -254,7 +241,6 @@ class mail_message(osv.Model):
# Flatten the result
if thread_level > 0:
result = self.message_read_tree_flatten(cr, uid, result, 0, thread_level, context=context)
return result
#------------------------------------------------------
+16 -34
View File
@@ -416,15 +416,6 @@ class test_mail(common.TransactionCase):
def test_30_message_read(self):
""" Tests designed for message_read. """
def _simplify_struct(read_dict):
res = []
for val in read_dict:
current = {'_id': val['id']}
if val.get('child_ids'):
current['child_ids'] = _simplify_struct(val.get('child_ids'))
res.append(current)
return res
# TDE NOTE: this test is not finished, as the message_read method is not fully specified.
# It wil be updated as soon as we have fixed specs !
cr, uid = self.cr, self.uid
@@ -432,38 +423,31 @@ class test_mail(common.TransactionCase):
# Test message_read_tree_flatten that flattens a thread according to a given thread_level
import copy
tree = [
{'id': 1, 'child_ids':[
{'id': 3, 'child_ids': [] },
{'id': 4, 'child_ids': [
{'id': 5, 'child_ids': []},
{'id': 12, 'child_ids': []},
tree = [{'id': 1, 'child_ids':[
{'id': 3, 'child_ids': [] },
{'id': 4, 'child_ids': [
{'id': 5, 'child_ids': []},
{'id': 12, 'child_ids': []},
] },
{'id': 8, 'child_ids': [
{'id': 10, 'child_ids': []},
] },
] },
{'id': 8, 'child_ids': [
{'id': 10, 'child_ids': []},
{'id': 2, 'child_ids': [
{'id': 7, 'child_ids': [
{'id': 9, 'child_ids': []},
] },
] },
] },
{'id': 2, 'child_ids': [
{'id': 7, 'child_ids': [
{'id': 9, 'child_ids': []},
{'id': 6, 'child_ids': [
{'id': 11, 'child_ids': [] },
] },
] },
{'id': 6, 'child_ids': [
{'id': 11, 'child_ids': [] },
] },
]
]
new_tree = self.mail_message.message_read_tree_flatten(cr, uid, copy.deepcopy(tree), 0, 0)
# self.mail_message._debug_print_tree(new_tree)
# print '-------------------'
self.assertTrue(len(new_tree) == 12, 'Flattening wrongly produced')
new_tree = self.mail_message.message_read_tree_flatten(cr, uid, copy.deepcopy(tree), 0, 1)
# self.mail_message._debug_print_tree(new_tree)
# print '-------------------'
self.assertTrue(len(new_tree) == 3 and len(new_tree[0]['child_ids']) == 6 and len(new_tree[1]['child_ids']) == 2 and len(new_tree[2]['child_ids']) == 1,
'Flattening wrongly produced')
new_tree = self.mail_message.message_read_tree_flatten(cr, uid, copy.deepcopy(tree), 0, 2)
# self.mail_message._debug_print_tree(new_tree)
# print '-------------------'
self.assertTrue(len(new_tree) == 3 and len(new_tree[0]['child_ids']) == 3 and len(new_tree[0]['child_ids'][1]) == 2,
'Flattening wrongly produced')
@@ -483,12 +467,10 @@ class test_mail(common.TransactionCase):
# Second try: read with thread_level 1
tree = self.mail_message.message_read(cr, uid, ids=False, domain=[('model', '=', 'mail.group'), ('res_id', '=', self.group_pigs_id)], thread_level=1)
# self.mail_message._debug_print_tree(tree)
self.assertTrue(len(tree) == 2 and len(tree[1]['child_ids']) == 3, 'Incorrect number of child in message_read')
# Third try: read with thread_level 2
tree = self.mail_message.message_read(cr, uid, ids=False, domain=[('model', '=', 'mail.group'), ('res_id', '=', self.group_pigs_id)], thread_level=2)
# self.mail_message._debug_print_tree(tree)
self.assertTrue(len(tree) == 2 and len(tree[1]['child_ids']) == 2 and len(tree[1]['child_ids'][0]['child_ids']) == 1, 'Incorrect number of child in message_read')
def test_40_needaction(self):