From be6dff397db1e964162b50173494281f3b3b92fe Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Thu, 22 Sep 2011 15:58:07 +0530 Subject: [PATCH] nestedset: root node numbering should follow some order (alphabetically) --- cgi-bin/webnotes/utils/nestedset.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/cgi-bin/webnotes/utils/nestedset.py b/cgi-bin/webnotes/utils/nestedset.py index 47d78af053..ecc1418bc1 100644 --- a/cgi-bin/webnotes/utils/nestedset.py +++ b/cgi-bin/webnotes/utils/nestedset.py @@ -153,19 +153,16 @@ def rebuild_tree(doctype, parent_field): """ # get all roots right = 1 - result = webnotes.conn.sql("SELECT name FROM `tab%s` WHERE `%s`='' or `%s` IS NULL" % (doctype, parent_field, parent_field)) + result = webnotes.conn.sql("SELECT name FROM `tab%s` WHERE `%s`='' or `%s` IS NULL ORDER BY name ASC" % (doctype, parent_field, parent_field)) for r in result: right = rebuild_node(doctype, r[0], right, parent_field) webnotes.conn.sql("commit") webnotes.conn.sql("start transaction") -cnt = 0 - -def rebuild_node(doctype, parent, left, parent_field): +def rebuild_node(doctype, parent, left, parent_field, cnt = 0): """ reset lft, rgt and recursive call for all children """ - global cnt from webnotes.utils import now n = now() @@ -175,8 +172,7 @@ def rebuild_node(doctype, parent, left, parent_field): # get all children of this node result = webnotes.conn.sql("SELECT name FROM `tab%s` WHERE `%s`='%s'" % (doctype, parent_field, parent)) for r in result: - - right = rebuild_node(doctype, r[0], right, parent_field) + right = rebuild_node(doctype, r[0], right, parent_field, cnt) # we've got the left value, and now that we've processed # the children of this node we also know the right value @@ -184,7 +180,7 @@ def rebuild_node(doctype, parent, left, parent_field): # commit after every 100 cnt += 1 - if cnt % 500 == 0: + if cnt % 100 == 0: cnt = 0 webnotes.conn.sql("commit") webnotes.conn.sql("start transaction")