[REV] mail_message: reverted changes on message_read, back to its fist version, excep...
authorThibault Delavallée <tde@openerp.com>
Mon, 27 Aug 2012 16:22:37 +0000 (18:22 +0200)
committerThibault Delavallée <tde@openerp.com>
Mon, 27 Aug 2012 16:22:37 +0000 (18:22 +0200)
bzr revid: tde@openerp.com-20120827162237-eljqnvenwy0xv5dc

addons/mail/mail_message.py
addons/mail/tests/test_mail.py

index a4536fb..304592e 100644 (file)
@@ -141,54 +141,60 @@ class mail_message(osv.Model):
             'child_ids': [],
         }
 
-    def message_read_tree_get(self, cr, uid, messages, domain, limit, flat=True, context=None):
-        """ Get a tree representation of the browse records.
-            :param messages: mail.message browse record list
+    def _debug_print_tree(self, tree, prefix=''):
+        for elem in tree:
+            print '%s%s' % (prefix, elem['id'])
+            if elem['child_ids']:
+                self._debug_print_tree(elem['child_ids'], prefix+'-')
+
+    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 [
+            
+            ]
         """
-        roots = []
+        limit = limit or self._message_read_limit
+        context = context or {}
+        if ids is False:
+            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 = []
         tree = {} # key: ID, value: record
         for msg in messages:
-            record = self._message_dict_get(cr, uid, msg, context=context)
-            if not flat and msg.parent_id:
-                while msg.parent_id:
-                    if msg.parent_id.id in tree:
-                        record_parent = tree[msg.parent_id.id]
-                    else:
-                        record_parent = self._message_dict_get(cr, uid, msg.parent_id, context=context)
-                        if msg.parent_id.parent_id:
-                            tree[msg.parent_id.id] = record_parent
-                    record_parent['child_ids'].append(record)
-                    record = record_parent
-                    msg = msg.parent_id
-            if msg.id not in tree:
-                roots.append(record)
-                tree[msg.id] = record
-            if len(roots) >= limit:
-                roots.append({
+            if len(result)<(limit-1):
+                record = self._message_dict_get(cr, uid, msg, context=context)
+                if thread_level and msg.parent_id:
+                    while msg.parent_id:
+                        if msg.parent_id.id in tree:
+                            record_parent = tree[msg.parent_id.id]
+                        else:
+                            record_parent = self._message_dict_get(cr, uid, msg.parent_id, context=context)
+                            if msg.parent_id.parent_id and msg.parent_id.id not in tree:
+                                tree[msg.parent_id.id] = record_parent
+                        record_parent['child_ids'].append(record)
+                        record = record_parent
+                        msg = msg.parent_id
+                if msg.id not in tree:
+                    result.append(record)
+                    tree[msg.id] = record
+            else:
+                result.append({
                     'type': 'expandable',
                     'domain': [('id','<=', msg.id)]+domain,
                     'context': context,
                     'thread_level': thread_level  # should be improve accodting to level of records
                 })
-        return roots
-
-    def message_read(self, cr, uid, ids=False, domain=[], thread_level=0, limit=None, context=None):
-        """ Fetch and read messages. 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 result: A tree structure: list of msg_dict, with child_ids
-                being also msg_dict.
-        """
-        limit = limit or self._message_read_limit
-        if ids is False:
-            ids = self.search(cr, uid, domain, context=context, limit=limit)
-        messages = self.browse(cr, uid, ids, context=context)
-        # FP note - TDE note: TODO: flatten - order
-        # TDE NOTE: add expandable: TO FIX WHEN SPECIFIED
-        trees = self.message_read_tree_get(cr, uid, messages, domain, limit, thread_level==0, context=context)
-        return trees
+                break
+        # TDE temp: debug print
+        # self._debug_print_tree(result)
+        return result
 
 
     #------------------------------------------------------
index e63aa57..f425007 100644 (file)
@@ -121,7 +121,7 @@ class test_mail(common.TransactionCase):
                           self.mail_thread.message_process,
                           cr, uid, None, mail_spam)
 
-    def test_01_many2many_reference_field(self):
+    def test_10_many2many_reference_field(self):
         """ Tests designed for the many2many_reference field (follower_ids).
             We will test to perform writes using the many2many commands 0, 3, 4,
             5 and 6. """
@@ -205,7 +205,7 @@ class test_mail(common.TransactionCase):
         self.assertTrue(all(id in follower_ids for id in [partner_bert_id, user_admin.partner_id.id]),
             'Bert and Admin should be the followers of dummy mail.group data')
 
-    def test_02_message_followers(self):
+    def test_11_message_followers(self):
         """ Tests designed for the subscriber API. """
         cr, uid = self.cr, self.uid
         user_admin = self.res_users.browse(cr, uid, uid)
@@ -234,41 +234,8 @@ class test_mail(common.TransactionCase):
         self.assertTrue(all(id in follower_ids for id in [user_admin.partner_id.id]),
             'Admin the only Pigs group followers')
 
-    def test_03_message_read(self):
-        """ Tests designed for message_read. """
-        def _flatten(read_dict):
-            res = []
-            for val in read_dict:
-                current = {'_id': val['id']}
-                if val.get('child_ids'):
-                    current['child_ids'] = _flatten(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
-        group_pigs = self.mail_group.browse(cr, uid, self.group_pigs_id)
-
-        # Add a few messages to pigs group
-        msgid1 = group_pigs.message_post(body='My Body', subject='1', parent_id=False)
-        msgid2 = group_pigs.message_post(body='My Body', subject='1-1', parent_id=msgid1)
-        msgid3 = group_pigs.message_post(body='My Body', subject='1-2', parent_id=msgid1)
-        msgid4 = group_pigs.message_post(body='My Body', subject='2', parent_id=False)
-        msgid5 = group_pigs.message_post(body='My Body', subject='1-1-1', parent_id=msgid2)
-        msgid6 = group_pigs.message_post(body='My Body', subject='2-1', parent_id=msgid4)
-        msgid7 = group_pigs.message_post(body='My Body', subject='1-3', parent_id=msgid1)
-        msgid8 = group_pigs.message_post(body='My Body', subject='1-3-1', parent_id=msgid7)
-
-        # First try: read flat
-        first_try_ids = [msgid8, msgid7, msgid6, msgid5, msgid4, msgid3, msgid2, msgid1]
-        res = self.mail_message.message_read(cr, uid, ids=False, domain=[('model', '=', 'mail.group'), ('res_id', '=', self.group_pigs_id)], thread_level=0)
-        
-        # Second try: read with thread_level 1
-        res = self.mail_message.message_read(cr, uid, ids=False, domain=[('model', '=', 'mail.group'), ('res_id', '=', self.group_pigs_id)], thread_level=1)
-
-    def test_10_mail_composer(self):
-        """ Tests designed for the mail.compose.message wizard. """
+    def test_20_message_post_and_compose(self):
+        """ Tests designed for message_post and the mail.compose.message wizard. """
         cr, uid = self.cr, self.uid
         mail_compose = self.registry('mail.compose.message')
         user_admin = self.res_users.browse(cr, uid, uid)
@@ -338,3 +305,42 @@ class test_mail(common.TransactionCase):
             'Wizard message has model: %s and res_id:%s; should be mail.group and %s' % (compose.model, compose.res_id, self.group_pigs_id))
         self.assertTrue(compose.parent_id.id == first_com.id,
             'Wizard parent_id is %d; should be %d' % (compose.parent_id.id, first_com.id))
+
+    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'] = _flatten(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
+        group_pigs = self.mail_group.browse(cr, uid, self.group_pigs_id)
+
+        # Add a few messages to pigs group
+        msgid1 = group_pigs.message_post(body='My Body', subject='1', parent_id=False)
+        msgid2 = group_pigs.message_post(body='My Body', subject='1-1', parent_id=msgid1)
+        msgid3 = group_pigs.message_post(body='My Body', subject='1-2', parent_id=msgid1)
+        msgid4 = group_pigs.message_post(body='My Body', subject='2', parent_id=False)
+        msgid5 = group_pigs.message_post(body='My Body', subject='1-1-1', parent_id=msgid2)
+        # msgid6 = group_pigs.message_post(body='My Body', subject='2-1', parent_id=msgid4)
+        # msgid7 = group_pigs.message_post(body='My Body', subject='1-3', parent_id=msgid1)
+        # msgid8 = group_pigs.message_post(body='My Body', subject='1-3-1', parent_id=msgid7)
+
+        # First try: read flat
+        # first_try_ids = [msgid8, msgid7, msgid6, msgid5, msgid4, msgid3, msgid2, msgid1]
+        # res = self.mail_message.message_read(cr, uid, ids=False, domain=[('model', '=', 'mail.group'), ('res_id', '=', self.group_pigs_id)], thread_level=0)
+
+        # Second try: read with thread_level 1
+        res = self.mail_message.message_read(cr, uid, ids=False, domain=[('model', '=', 'mail.group'), ('res_id', '=', self.group_pigs_id)], thread_level=1)
+        # print res
+        self.assertTrue(len(res) == 2, 'Incorrect number of child in message_read')
+        self.assertTrue(len(res[0]['child_ids']) == 2, 'Incorrect number of child in message_read')
+        self.assertTrue(len(res[0]['child_ids'][0]['child_ids']) == 1, 'Incorrect number of child in message_read')
+        # trees = self.mail_message.message_read_tree_flatten_main(cr, uid, res, thread_level=0)
+        # print trees