Skip to content
Browse files

Merge branch 'current_page_apphooks'

  • Loading branch information...
2 parents 51e0b8d + 6bbe4fe commit c647c23b51d805e3bc5db872abde39088f9485cb @fivethreeo fivethreeo committed Jan 26, 2011
View
9 cms/appresolver.py
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
from cms.apphook_pool import apphook_pool
from cms.exceptions import NoHomeFound
+from cms.utils.moderator import get_page_queryset
+
from django.conf import settings
from django.conf.urls.defaults import patterns
from django.contrib.sites.models import Site
@@ -11,11 +13,14 @@
APP_RESOLVERS = []
+def clear_app_resolvers():
+ global APP_RESOLVERS
+ APP_RESOLVERS = []
+
def applications_page_check(request, current_page=None, path=None):
"""Tries to find if given path was resolved over application.
Applications have higher priority than other cms pages.
"""
- from cms.utils.moderator import get_page_queryset
if current_page:
return current_page
if path is None:
@@ -25,7 +30,7 @@ def applications_page_check(request, current_page=None, path=None):
# check if application resolver can resolve this
for resolver in APP_RESOLVERS:
try:
- page_id = resolver.resolve_page_id(path+"/")
+ page_id = resolver.resolve_page_id(path)
# yes, it is application page
page = get_page_queryset(request).get(id=page_id)
# If current page was matched, then we have some override for content
View
6 cms/middleware/page.py
@@ -1,9 +1,15 @@
# -*- coding: utf-8 -*-
+from cms.appresolver import applications_page_check
+
class LazyPage(object):
def __get__(self, request, obj_type=None):
from cms.utils.page_resolver import get_page_from_request
if not hasattr(request, '_current_page_cache'):
request._current_page_cache = get_page_from_request(request)
+ if not request._current_page_cache:
+ # if this is in a apphook
+ # find the page the apphook is attached to
+ request._current_page_cache = applications_page_check(request)
return request._current_page_cache
class CurrentPageMiddleware(object):
View
98 cms/tests/apphooks.py
@@ -1,11 +1,13 @@
# -*- coding: utf-8 -*-
from __future__ import with_statement
from cms.apphook_pool import apphook_pool
+from cms.appresolver import applications_page_check, clear_app_resolvers
+
from cms.models.titlemodels import Title
from cms.test.testcases import CMSTestCase
from cms.test.util.context_managers import SettingsOverride
from django.contrib.auth.models import User
-from django.core.urlresolvers import clear_url_caches
+from django.core.urlresolvers import clear_url_caches, reverse
import sys
@@ -14,9 +16,9 @@
class ApphooksTestCase(CMSTestCase):
- urls = 'testapp.urls_for_apphook_tests'
-
+
def setUp(self):
+ clear_app_resolvers()
clear_url_caches()
def test_01_explicit_apphooks(self):
@@ -45,7 +47,7 @@ def test_02_implicit_apphooks(self):
del sys.modules[APP_MODULE]
apps = ['testapp.sampleapp']
- with SettingsOverride(INSTALLED_APPS=apps):
+ with SettingsOverride(INSTALLED_APPS=apps, ROOT_URLCONF='testapp.urls_for_apphook_tests'):
apphook_pool.clear()
hooks = apphook_pool.get_apphooks()
app_names = [hook[0] for hook in hooks]
@@ -58,21 +60,75 @@ def test_03_apphook_on_root(self):
if APP_MODULE in sys.modules:
del sys.modules[APP_MODULE]
- apphook_pool.clear()
- superuser = User.objects.create_superuser('admin', 'admin@admin.com', 'admin')
- page = self.create_page(user=superuser, published=True)
- english_title = page.title_set.all()[0]
- self.assertEquals(english_title.language, 'en')
- Title.objects.create(
- language='de',
- title='%s DE' % english_title.title,
- slug=english_title.slug,
- path=english_title.path,
- page=page,
- )
- page.title_set.all().update(application_urls='SampleApp')
- self.assertTrue(page.publish())
+ with SettingsOverride(ROOT_URLCONF='testapp.urls_for_apphook_tests'):
+ apphook_pool.clear()
+ superuser = User.objects.create_superuser('admin', 'admin@admin.com', 'admin')
+ page = self.create_page(user=superuser, published=True)
+ english_title = page.title_set.all()[0]
+ self.assertEquals(english_title.language, 'en')
+ Title.objects.create(
+ language='de',
+ title='%s DE' % english_title.title,
+ slug=english_title.slug,
+ path=english_title.path,
+ page=page,
+ )
+ page.title_set.all().update(application_urls='SampleApp')
+ self.assertTrue(page.publish())
+
+ response = self.client.get(self.get_pages_root())
+ self.assertTemplateUsed(response, 'sampleapp/home.html')
+ apphook_pool.clear()
+
+ def test_04_get_page_for_apphook(self):
+
+ if APP_MODULE in sys.modules:
+ del sys.modules[APP_MODULE]
+
+ with SettingsOverride(ROOT_URLCONF='testapp.second_urls_for_apphook_tests'):
+
+ apphook_pool.clear()
+ superuser = User.objects.create_superuser('admin', 'admin@admin.com', 'admin')
+ page = self.create_page(user=superuser, published=True)
+ self.create_title(page.get_title(), page.get_slug(), 'de', page)
+ child_page = self.create_page(page, user=superuser, published=True)
+ self.create_title(child_page.get_title(), child_page.get_slug(), 'de', child_page)
+ child_child_page = self.create_page(child_page, user=superuser, published=True)
+ self.create_title(child_child_page.get_title(), child_child_page.get_slug(), 'de', child_child_page)
+ child_child_page.title_set.all().update(application_urls='SampleApp')
+
+ child_child_page.publish()
+ # publisher_public is set to draft on publish, issue with onetoone reverse
+ child_child_page = self.reload(child_child_page)
+
+ en_title = child_child_page.publisher_public.get_title_obj('en')
+
+ path = reverse('en:sample-settings')
+
+ request = self.get_request(path)
+ request.LANGUAGE_CODE = 'en'
+
+ attached_to_page = applications_page_check(request, path=path[1:]) # strip leading slash
+ self.assertEquals(attached_to_page.pk, en_title.page.pk)
+
+ response = self.client.get(path)
+ self.assertEquals(response.status_code, 200)
- response = self.client.get(self.get_pages_root())
- self.assertTemplateUsed(response, 'sampleapp/home.html')
- apphook_pool.clear()
+ self.assertTemplateUsed(response, 'sampleapp/home.html')
+ self.assertContains(response, en_title.title)
+
+ de_title = child_child_page.publisher_public.get_title_obj('de')
+ path = reverse('de:sample-settings')
+
+ request = self.get_request(path)
+ request.LANGUAGE_CODE = 'de'
+
+ attached_to_page = applications_page_check(request, path=path[4:]) # strip leading slash and language prefix
+ self.assertEquals(attached_to_page.pk, de_title.page.pk)
+
+ response = self.client.get(path)
+ self.assertEquals(response.status_code, 200)
+ self.assertTemplateUsed(response, 'sampleapp/home.html')
+ self.assertContains(response, de_title.title)
+
+ apphook_pool.clear()
View
2 cms/utils/page_resolver.py
@@ -2,6 +2,7 @@
from cms.exceptions import NoHomeFound
from cms.models.pagemodel import Page
from cms.utils.moderator import get_page_queryset
+
from django.conf import settings
from django.contrib.sites.models import Site
from django.core.urlresolvers import reverse
@@ -114,5 +115,6 @@ def get_page_from_request(request, use_path=None):
page = pages.filter(q).distinct().get()
except Page.DoesNotExist:
return None
+
request._current_page_cache = page
return page
View
2 tests/testapp/sampleapp/templates/sampleapp/home.html
@@ -2,7 +2,7 @@
{% load cms_tags %}
{% block content %}
- <h2>Sample application home page</h2>
+ <h2>Sample application home page - on page {% page_attribute page_title %}</h2>
<h3>{{ message }}</h3>
{{ block.super }}
<ul>
View
25 tests/testapp/second_cms_urls_for_apphook_tests.py
@@ -0,0 +1,25 @@
+from cms.apphook_pool import apphook_pool
+from cms.views import details
+from django.conf import settings
+from django.conf.urls.defaults import url, patterns
+
+if settings.APPEND_SLASH:
+ reg = url(r'^(?P<slug>[0-9A-Za-z-_.//]+)/$', details, name='pages-details-by-slug')
+else:
+ reg = url(r'^(?P<slug>[0-9A-Za-z-_.//]+)$', details, name='pages-details-by-slug')
+
+urlpatterns = [
+ # Public pages
+ url(r'^$', details, {'slug':''}, name='pages-root'),
+ reg,
+]
+
+if apphook_pool.get_apphooks():
+ """If there are some application urls, add special resolver, so we will
+ have standard reverse support.
+ """
+ from cms.appresolver import get_app_patterns
+ urlpatterns = get_app_patterns() + urlpatterns
+ #urlpatterns = (dynamic_app_regex_url_resolver, ) + urlpatterns
+
+urlpatterns = patterns('', *urlpatterns)
View
16 tests/testapp/second_urls_for_apphook_tests.py
@@ -0,0 +1,16 @@
+from django.conf import settings
+from django.conf.urls.defaults import handler500, handler404, patterns, include, \
+ url
+from django.contrib import admin
+
+admin.autodiscover()
+
+urlpatterns = patterns('',
+ url(r'^admin/', include(admin.site.urls)),
+ url(r'^jsi18n/(?P<packages>\S+?)/$', 'django.views.i18n.javascript_catalog'),
+ url(r'^media/cms/(?P<path>.*)$', 'django.views.static.serve',
+ {'document_root': settings.CMS_MEDIA_ROOT, 'show_indexes': True}),
+ url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
+ {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
+ url(r'^', include('testapp.second_cms_urls_for_apphook_tests')),
+)

0 comments on commit c647c23

Please sign in to comment.
Something went wrong with that request. Please try again.