My favorites
▼
|
Sign in
django-mptt
Utilities for implementing Modified Preorder Tree Traversal
Project Home
Downloads
Wiki
Issues
Source
Export to GitHub
READ-ONLY: This project has been
archived
. For more information see
this post
.
Search
Search within:
All issues
Open issues
New issues
Issues to verify
for
Advanced search
Search tips
Subscriptions
Issue
13
attachment: rebuild_tree.patch
(3.2 KB)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
=== modified file 'mptt/managers.py'
--- mptt/managers.py 2008-12-05 00:17:36 +0000
+++ mptt/managers.py 2008-12-05 01:37:34 +0000
@@ -208,6 +208,75 @@
"""
return self.filter(**{'%s__isnull' % self.parent_attr: True})
+ def rebuild(self):
+ """
+ Rebuilds whole tree in database using `parent` link.
+ """
+ opts = self.model._meta
+
+ cursor = connection.cursor()
+ cursor.execute('UPDATE %(table)s SET %(left)s = 0, %(right)s = 0, %(level)s = 0, %(tree_id)s = 0' % {
+ 'table': qn(opts.db_table),
+ 'left': qn(opts.get_field(self.left_attr).column),
+ 'right': qn(opts.get_field(self.right_attr).column),
+ 'level': qn(opts.get_field(self.level_attr).column),
+ 'tree_id': qn(opts.get_field(self.tree_id_attr).column)
+ })
+
+ cursor.execute('SELECT %(id_col)s FROM %(table)s WHERE %(parent_col)s is NULL %(orderby)s' % {
+ 'id_col': qn(opts.pk.column),
+ 'table': qn(opts.db_table),
+ 'parent_col': qn(opts.get_field(self.parent_attr).column),
+ 'orderby': 'ORDER BY ' + ', '.join([qn(field) for field in opts.order_insertion_by]) if opts.order_insertion_by else ''
+ })
+
+ idx = 0
+ for (pk, ) in cursor.fetchall():
+ idx += 1
+ self._rebuild_helper(pk, 1, idx)
+ transaction.commit_unless_managed()
+
+ def _rebuild_helper(self, pk, left, tree_id, level=0):
+ opts = self.model._meta
+ right = left + 1
+
+ cursor = connection.cursor()
+ cursor.execute('SELECT %(id_col)s FROM %(table)s WHERE %(parent_col)s = %(parent)d %(orderby)s' % {
+ 'id_col': qn(opts.pk.column),
+ 'table': qn(opts.db_table),
+ 'parent_col': qn(opts.get_field(self.parent_attr).column),
+ 'parent': pk,
+ 'orderby': 'ORDER BY ' + ', '.join([qn(field) for field in opts.order_insertion_by]) if opts.order_insertion_by else ''
+ })
+
+ for (child_id, ) in cursor.fetchall():
+ right = self._rebuild_helper(child_id, right, tree_id, level+1)
+
+ cursor.execute("""
+ UPDATE %(table)s
+ SET
+ %(left_col)s = %(left)d,
+ %(right_col)s = %(right)d,
+ %(level_col)s = %(level)d,
+ %(tree_id_col)s = %(tree_id)d
+ WHERE
+ %(pk_col)s = %(pk)s
+ """ % {
+ 'table': qn(opts.db_table),
+ 'pk_col': qn(opts.pk.column),
+ 'left_col': qn(opts.get_field(self.left_attr).column),
+ 'right_col': qn(opts.get_field(self.right_attr).column),
+ 'level_col': qn(opts.get_field(self.level_attr).column),
+ 'tree_id_col': qn(opts.get_field(self.tree_id_attr).column),
+ 'pk': pk,
+ 'left': left,
+ 'right': right,
+ 'level': level,
+ 'tree_id': tree_id
+ })
+
+ return right + 1
+
def _calculate_inter_tree_move_values(self, node, target, position):
"""
Calculates values required when moving ``node`` relative to
Powered by
Google Project Hosting