Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

set current page in apphooked urls

  • Loading branch information...
commit b8148b5d5a2fdeec890c8c452385dbd4961c0ed5 1 parent 9b47e0d
@fivethreeo fivethreeo authored
View
5 cms/appresolver.py
@@ -11,6 +11,9 @@
APP_RESOLVERS = []
+def clear_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.
@@ -25,7 +28,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
100 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,77 @@ 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')
+
+ page.publish()
+ child_page = self.reload(child_page)
+ child_page.publish()
+ child_child_page = self.reload(child_child_page)
+ child_child_page.publish()
+
+ en_title = child_child_page.get_title('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:-1]) # strip trailing slash
+ self.assertEquals(attached_to_page.get_title('en'), en_title)
+
+ 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)
+
+ de_title = child_child_page.get_title('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:-1]) # strip trailing slash and language prefix
+ self.assertEquals(attached_to_page.get_title('de'), de_title)
+
+ response = self.client.get(path)
+ self.assertEquals(response.status_code, 200)
+ self.assertTemplateUsed(response, 'sampleapp/home.html')
+ self.assertContains(response, de_title)
+
+ apphook_pool.clear()
View
12 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 cms.appresolver import applications_page_check
from django.conf import settings
from django.contrib.sites.models import Site
from django.core.urlresolvers import reverse
@@ -113,6 +114,13 @@ def get_page_from_request(request, use_path=None):
try:
page = pages.filter(q).distinct().get()
except Page.DoesNotExist:
- return None
- request._current_page_cache = page
+ page = None
+
+ if not page:
+ # if this is in a apphook
+ # find the page the apphook is attached to
+ page = applications_page_check(request, path=path)
+
+ if page:
+ 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>
Please sign in to comment.
Something went wrong with that request. Please try again.