Community mailing list archives

community@mail.odoo.com

Re: categories: what are parent_left and parent_rigth

by
redCOR AG, robert rottermann
- 09/11/2015 09:49:44
On 11.09.2015 10:40, robert wrote:
<blockquote cite="mid:55F29378.7010600@redcor.ch" type="cite">
Thanks Jay,

On 11.09.2015 09:26, Jay Vora wrote:
<blockquote cite="mid:CACHFH7nhPT2G4QWbkdKSh=wfQFQErpHR28KH4eFSa52CyQObzA@mail.gmail.com" type="cite">
Robert,

Please check here.
very interesting,

now I wonder, how are parent_left and parent_rigth set, when a new category record is created.
I see, that whenever I create a new category, the values of the existing ones are adapted.
debugging trough the code I found out myself.
so if somebody has the same question:
it is in odoo/openerp/models at line ~4230:
        if self._parent_store and not context.get('defer_parent_store_computation'):
            if self.pool._init:
                self.pool._init_parent[self._name] = True
            else:
                parent = vals.get(self._parent_name, False)
                if parent:
                    cr.execute('select parent_right from '+self._table+' where '+self._parent_name+'=%s order by '+(self._parent_order or self._order), (parent,))
                    pleft_old = None
                    result_p = cr.fetchall()
                    for (pleft,) in result_p:
                        if not pleft:
                            break
                        pleft_old = pleft
                    if not pleft_old:
                        cr.execute('select parent_left from '+self._table+' where id=%s', (parent,))
                        pleft_old = cr.fetchone()[0]
                    pleft = pleft_old
                else:
                    cr.execute('select max(parent_right) from '+self._table)
                    pleft = cr.fetchone()[0] or 0
                cr.execute('update '+self._table+' set parent_left=parent_left+2 where parent_left>%s', (pleft,))
                cr.execute('update '+self._table+' set parent_right=parent_right+2 where parent_right>%s', (pleft,))
                cr.execute('update '+self._table+' set parent_left=%s,parent_right=%s where id=%s', (pleft+1, pleft+2, id_new))
                recs.invalidate_cache(['parent_left', 'parent_right'])

robert