Skip to content

Commit

Permalink
Merge pull request #1195 from piquadrat/feature/pagefield-tree-order
Browse files Browse the repository at this point in the history
PageField tree order fix

Thanks to @ojii and @chrisglass for the review!
  • Loading branch information
beniwohli committed Jun 7, 2012
2 parents 7a7b806 + fced8e9 commit b8ebb25
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
11 changes: 7 additions & 4 deletions cms/forms/utils.py
Expand Up @@ -8,6 +8,7 @@
from django.core.cache import cache
from django.db.models.signals import post_save, post_delete
from django.utils import translation
from django.utils.datastructures import SortedDict
from django.utils.safestring import mark_safe


Expand All @@ -19,11 +20,13 @@ def update_site_and_page_choices(lang=None):
title_queryset = Title.objects.filter(page__publisher_is_draft=False)
else:
title_queryset = Title.objects.filter(page__publisher_is_draft=True)
title_queryset = title_queryset.select_related('page', 'page__site')
pages = defaultdict(lambda: defaultdict(dict))
title_queryset = title_queryset.select_related('page', 'page__site').order_by('page__tree_id', 'page__lft', 'page__rght')
pages = defaultdict(SortedDict)
sites = {}
for title in title_queryset:
pages[title.page.site.pk][title.page.pk][title.language] = title
page = pages[title.page.site.pk].get(title.page.pk, {})
page[title.language] = title
pages[title.page.site.pk][title.page.pk] = page
sites[title.page.site.pk] = title.page.site.name

site_choices = []
Expand Down Expand Up @@ -93,4 +96,4 @@ def clean_page_choices_cache(sender, **kwargs):
post_save.connect(clean_page_choices_cache, sender=Page)
post_save.connect(clean_site_choices_cache, sender=Site)
post_delete.connect(clean_page_choices_cache, sender=Page)
post_delete.connect(clean_site_choices_cache, sender=Site)
post_delete.connect(clean_site_choices_cache, sender=Site)
3 changes: 3 additions & 0 deletions cms/tests/forms.py
Expand Up @@ -93,12 +93,15 @@ def test_update_site_and_page_choices(self):
site = Site.objects.create(domain='http://www.django-cms.org', name='Django CMS')
page1 = create_page('Page 1', 'nav_playground.html', 'en', site=site)
page2 = create_page('Page 2', 'nav_playground.html', 'de', site=site)
page3 = create_page('Page 3', 'nav_playground.html', 'en',
site=site, parent=page1)
# enfore the choices to be casted to a list
site_choices, page_choices = [list(bit) for bit in update_site_and_page_choices('en')]
self.assertEqual(page_choices, [
('', '----'),
(site.name, [
(page1.pk, 'Page 1'),
(page3.pk, '  Page 3'),
(page2.pk, 'Page 2'),
])
])
Expand Down

0 comments on commit b8ebb25

Please sign in to comment.