Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 154 lines (137 sloc) 5.347 kB
e428a00 @evildmp committing all files (I hope)
authored
1 '''
2 Created on Sep 10, 2009
3
4 @author: stefanfoulis
5
6 /repair_mptt/cms.page/
7
8 '''
9 from cms.models import Page
10 import django.http as http
11 from django.db.models import get_model
12 import django.shortcuts as shortcuts
13 from django.template import RequestContext
14 from django.contrib.auth.decorators import login_required
15
16
17 def setup():
18 from django.contrib.auth.models import User
19 from cms.utils.permissions import set_current_user
20 set_current_user(User.objects.get(id=1))
21 def check_no_moderator():
22 print " checking that CMS_MODERATOR is set to False"
23 from django.conf import settings
24 if settings.CMS_MODERATOR:
25 print "!! Please set CMS_MODERATOR=False in settings before using this script !!"
26 print 'aborted'
27 raise Exception("!! Please set CMS_MODERATOR=False in settings before using this script !!")
28
29
30
31 def fix_tree_id(model):
32 print " rewriting tree_id..."
33 #from django.db.models import Avg, Max, Min, Count
34 #base_tree_id = Page.objects.filter(parent=None).aggregate(tree_id=Max('tree_id'))['tree_id'] + 1
35 base_tree_id = 1
36 for node in model.objects.filter(parent=None).order_by('tree_id'):
37 node.tree_id = base_tree_id
38 node.save()
39 r_fix_tree_id(node.children.all(), base_tree_id)
40 base_tree_id += 1
41 return "I fixed some trees"
42
43 def r_fix_tree_id(nodes, tree_id):
44 for node in nodes:
45 node.tree_id = tree_id
46 if node.__class__ == Page:
47 node.save(no_signals=True)
48 else:
49 node.save()
50 r_fix_tree_id(node.children.all(), tree_id)
51
52 def fix_leftright(model,do_alteration=True):
53 setup()
54 check_no_moderator()
55 print " fixing left and right..."
56 def recur(node, counter):
57 node.lft = counter
58 counter += 1
59 for child in node.children.all().order_by('tree_id', 'parent', 'lft'):
60 counter = recur(child, counter)
61 node.rght = counter
62 counter += 1
63 if node.__class__ == Page:
64 node.save(no_signals=True)
65 else:
66 node.save()
67 return counter
68 for root_node in model.objects.filter(parent=None).order_by('tree_id', 'parent', 'lft'):
69 counter = recur(root_node, 1)
70 # sanity check
71 total_nodes = model.objects.filter(tree_id=root_node.tree_id).count()
72 if not total_nodes * 2 == counter-1:
73 print " something is wrong! %s != %s" % (total_nodes * 2, counter-1)
74 return "did some leftright checking"
75
76 def fix_level(model):
77 setup()
78 print " fixing level..."
79 bad_level_count = 0
80 level = 0
81 all_nodes = model.objects.order_by('tree_id', 'parent', 'lft')
82 for root_node in all_nodes.filter(parent=None):
83 bad_level_count += r_fix_level(root_node, level=level)
84 print " fixed level of %s nodes" % bad_level_count
85 return "I fixed level of %s nodes" % bad_level_count
86
87 def r_fix_level(node, level):
88 bad_level_count = 0
89 if not node.level == level:
90 node.level = level
91 if node.__class__ == Page:
92 node.save(no_signals=True)
93 else:
94 node.save()
95 bad_level_count += 1
96 else:
97 pass#print " comparing level of page id:%s level:%s to level:%s: ok" % (page, page.level, level)
98 for subnode in node.children.all():
99 bad_level_count += r_fix_level(subnode, level+1)
100 return bad_level_count
101
102
103 @login_required
104 def fix(request,slug):
105 print "fixing mptt tree"
106 model = get_model(*slug.split('.'))
107 #print type(model)
108 if model == Page:
109 print "it's a page"
110 #print model.objects.all()
111 setup()
112 check_no_moderator()
113 tree_report = fix_tree_id(model)
114 level_report = fix_level(model)
115 leftright_report = fix_leftright(model)
116 print "all done"
117 #return http.HttpResponse("fixed")
118 return shortcuts.render_to_response(
3e00f86 @evildmp starting work on the housekeeping system
authored
119 "housekeeping/test.html",
e428a00 @evildmp committing all files (I hope)
authored
120 {"tree_report":tree_report,
121 "level_report":level_report,
122 "leftright_report":leftright_report,},
123 RequestContext(request),
124 )
125
126
127
128 def check_leftright():
129 report = []
130 report.append("Checking left/right")
131 print " checking left and right..."
132 errors = {}
133 def add_error(node, msg):
134 if not node.id in errors.keys():
135 errors[node.id] = [u"node: %s" % node]
136 errors[node.id].append(msg)
137 def recur(node, counter):
138 if not node.lft == counter: add_error(node, u"lft is %s, should be %s." % (node.lft, counter))
139 counter += 1
140 for child in node.children.all().order_by('tree_id', 'parent', 'lft'):
141 counter = recur(child, counter)
142 if not node.rght == counter: add_error(node, u"rght is %s, should be %s." % (node.rght, counter))
143 counter += 1
144 return counter
145 for root_page in Page.objects.filter(parent=None).order_by('tree_id', 'parent', 'lft'):
146 counter = recur(root_page, 1)
147 # sanity check
148 total_pages = Page.objects.filter(tree_id=root_page.tree_id).count()
149 if not total_pages * 2 == counter-1:
150 print " something is wrong! %s != %s" % (total_pages * 2, counter-1)
151 report.append("something is wrong! %s != %s" % (total_pages * 2, counter-1))
152 from pprint import pprint
153 pprint(errors)
154 return errors
Something went wrong with that request. Please try again.