From bdd972e3c92709bb0f739baba9af63077e7a9012 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Fri, 31 Aug 2012 16:37:07 +0200 Subject: [PATCH] [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 --- addons/mail/mail_message.py | 36 ++++++++---------------- addons/mail/tests/test_mail.py | 50 +++++++++++----------------------- 2 files changed, 27 insertions(+), 59 deletions(-) diff --git a/addons/mail/mail_message.py b/addons/mail/mail_message.py index 2bf5f0ab078..8fc9fb27b86 100644 --- a/addons/mail/mail_message.py +++ b/addons/mail/mail_message.py @@ -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 #------------------------------------------------------ diff --git a/addons/mail/tests/test_mail.py b/addons/mail/tests/test_mail.py index d288f522a72..f7f9e2e8a3e 100644 --- a/addons/mail/tests/test_mail.py +++ b/addons/mail/tests/test_mail.py @@ -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):