Browse files

less queries when rendering menus

  • Loading branch information...
1 parent e392ba9 commit f30f6231c63808d8b352012b31f9edc8c3809abd @fivethreeo fivethreeo committed Jun 20, 2011
Showing with 31 additions and 5 deletions.
  1. +20 −1 cms/menu.py
  2. +11 −4 cms/tests/menu.py
View
21 cms/menu.py
@@ -9,6 +9,25 @@
from cms.apphook_pool import apphook_pool
from cms.models.titlemodels import Title
+def populate_ancestors(page_list):
+ ancestors = []
+ populated_pages = []
+ for idx, page in enumerate(page_list):
+ if idx == 0:
+ ancestors = page.get_cached_ancestors(ascending=True)
+ try:
+ next_page = page_list[idx+1]
+ except IndexError:
+ next_page = None
+ page.ancestors_ascending = ancestors
+ if next_page:
+ if next_page.level > page.level:
+ ancestors.append(page)
+ if next_page.level < page.level:
+ ancestors.pop()
+ populated_pages.append(page)
+ return populated_pages
+
def page_to_node(page, home, cut):
'''
Transform a CMS page into a navigation node.
@@ -98,7 +117,7 @@ def get_nodes(self, request):
home = None
actual_pages = []
- for page in pages:
+ for page in populate_ancestors(pages):
# Pages are ordered by tree_id, therefore the first page is the root
# of the page tree (a.k.a "home")
if not page.has_view_permission(request):
View
15 cms/tests/menu.py
@@ -8,6 +8,7 @@
from cms.test_utils.testcases import SettingsOverrideTestCase
from cms.test_utils.util.mock import AttributeObject
from django.conf import settings
+from django import db
from django.template import Template
from menus.base import NavigationNode
from menus.menu_pool import menu_pool, _build_nodes_inner_for_one_menu
@@ -16,7 +17,8 @@
class BaseMenuTest(SettingsOverrideTestCase):
settings_overrides = {
- 'CMS_MODERATOR': False
+ 'CMS_MODERATOR': False,
+ 'DEBUG': True
}
def _get_nodes(self, path='/'):
@@ -80,8 +82,10 @@ def test_basic_cms_menu(self):
def test_show_menu(self):
context = self.get_context()
# test standard show_menu
+ db.reset_queries()
tpl = Template("{% load menu_tags %}{% show_menu %}")
tpl.render(context)
+ self.assertEqual(len(db.connection.queries), 1)
nodes = context['children']
self.assertEqual(len(nodes), 2)
self.assertEqual(nodes[0].selected, True)
@@ -93,7 +97,7 @@ def test_show_menu(self):
self.assertEqual(nodes[1].get_absolute_url(), self.get_page(4).get_absolute_url())
self.assertEqual(nodes[1].sibling, True)
self.assertEqual(nodes[1].selected, False)
-
+
def test_only_active_tree(self):
context = self.get_context()
# test standard show_menu
@@ -463,7 +467,7 @@ def test_build_nodes_inner_for_worst_case_menu(self):
self.assertEqual(node3.children, [node2])
self.assertEqual(node4.children, [node3])
self.assertEqual(node5.children, [node4])
-
+
def test_build_nodes_inner_for_circular_menu(self):
'''
TODO:
@@ -714,12 +718,13 @@ def test_show_submenu(self):
page = Page.objects.get(title_set__title='P6')
context = self.get_context(page.get_absolute_url())
# test standard show_menu
+ db.reset_queries()
tpl = Template("{% load menu_tags %}{% show_sub_menu %}")
tpl.render(context)
+ self.assertEqual(len(db.connection.queries), 19)
nodes = context['children']
self.assertEqual(len(nodes), 1)
self.assertEqual(nodes[0].id, 8)
-
class ShowMenuBelowIdTests(BaseMenuTest):
def test_not_in_navigation(self):
@@ -742,8 +747,10 @@ def test_not_in_navigation(self):
create_page('D', 'nav_playground.html', 'en', parent=self.reload(b),
published=True, in_navigation=False)
context = self.get_context(a.get_absolute_url())
+ db.reset_queries()
tpl = Template("{% load menu_tags %}{% show_menu_below_id 'a' 0 100 100 100 %}")
tpl.render(context)
+ self.assertEqual(len(db.connection.queries), 11) # three less queries as expected
nodes = context['children']
self.assertEqual(len(nodes), 1, nodes)
node = nodes[0]

0 comments on commit f30f623

Please sign in to comment.