Skip to content
Browse files

Update page_url_as_tag to reflect design decision.

Signed-off-by: Martin Koistinen <mkoistinen@gmail.com>
  • Loading branch information...
1 parent 4339013 commit 6345c53eabadb7f32ee7bda75261b2ac8591a116 @mkoistinen mkoistinen committed
Showing with 72 additions and 19 deletions.
  1. +15 −0 cms/templatetags/cms_tags.py
  2. +46 −6 cms/tests/rendering.py
  3. +9 −11 docs/advanced/templatetags.rst
  4. +1 −1 setup.py
  5. +1 −1 test_requirements/requirements_base.txt
View
15 cms/templatetags/cms_tags.py
@@ -121,6 +121,21 @@ class PageUrl(AsTag):
Argument('varname', required=False, resolve=False),
)
+ def get_value_for_context(self, context, **kwargs):
+ #
+ # A design decision with several active members of the django-cms
+ # community that using this tag with the 'as' breakpoint should never
+ # return Exceptions regardless of the setting of settings.DEBUG.
+ #
+ # We wish to maintain backwards functionality where the non-as-variant
+ # of using this tag will raise DNE exceptions only when
+ # settings.DEBUG=False.
+ #
+ try:
+ return super(PageUrl, self).get_value_for_context(context, **kwargs)
+ except Page.DoesNotExist:
+ return ''
+
def get_value(self, context, page_lookup, lang, site):
from django.core.cache import cache
View
52 cms/tests/rendering.py
@@ -2,7 +2,6 @@
from __future__ import with_statement
from cms import plugin_rendering
from cms.api import create_page, add_plugin
-from cms.compat import get_user_model
from cms.models.placeholdermodel import Placeholder
from cms.models.pluginmodel import CMSPlugin
from cms.plugin_rendering import render_plugins, PluginContext, render_placeholder_toolbar
@@ -12,7 +11,6 @@
from django.core.cache import cache
from django.template import Template, RequestContext
from sekizai.context import SekizaiContext
-from cms.toolbar.toolbar import CMSToolbar
TEMPLATE_NAME = 'tests/rendering/base.html'
@@ -281,10 +279,52 @@ def test_page_url_by_page_as(self):
output = self.render(template, self.test_page, {'test_page': self.test_page2})
self.assertEqual(output, self.test_page2.get_absolute_url())
- def test_page_url_by_page_as_for_bogus_page(self):
- template = u'{% load cms_tags %}{% page_url "bogus_page" as test_url %}{{ test_url }}'
- output = self.render(template, self.test_page, {'test_page': self.test_page2})
- self.assertEqual(output, '')
+ #
+ # To ensure compatible behaviour, test that page_url swallows any
+ # Page.DoesNotExist exceptions when NOT in DEBUG mode.
+ #
+ def test_page_url_on_bogus_page(self):
+ with SettingsOverride(DEBUG=False):
+ template = u'{% load cms_tags %}{% page_url "bogus_page" %}'
+ output = self.render(template, self.test_page, {'test_page': self.test_page2})
+ self.assertEqual(output, '')
+
+ #
+ # To ensure compatible behaviour, test that page_url will raise a
+ # Page.DoesNotExist exception when the page argument does not eval to a
+ # valid page
+ #
+ def test_page_url_on_bogus_page_in_debug(self):
+ from cms.models import Page
+
+ with SettingsOverride(DEBUG=True):
+ template = u'{% load cms_tags %}{% page_url "bogus_page" %}'
+ try:
+ self.render(template, self.test_page, {'test_page': self.test_page2})
+ self.fail('No exception raised')
+ except Page.DoesNotExist:
+ pass
+ except:
+ self.fail('Wrong exception raised')
+
+ #
+ # In the 'as varname' form, ensure that the tag will always swallow
+ # Page.DoesNotExist exceptions both when DEBUG is False and...
+ #
+ def test_page_url_as_on_bogus_page(self):
+ with SettingsOverride(DEBUG=False):
+ template = u'{% load cms_tags %}{% page_url "bogus_page" as test_url %}{{ test_url }}'
+ output = self.render(template, self.test_page, {'test_page': self.test_page2})
+ self.assertEqual(output, '')
+
+ #
+ # ...when it is True.
+ #
+ def test_page_url_as_on_bogus_page_in_debug(self):
+ with SettingsOverride(DEBUG=True):
+ template = u'{% load cms_tags %}{% page_url "bogus_page" as test_url %}{{ test_url }}'
+ output = self.render(template, self.test_page, {'test_page': self.test_page2})
+ self.assertEqual(output, '')
def test_page_attribute(self):
"""
View
20 docs/advanced/templatetags.rst
@@ -216,6 +216,9 @@ Displays the URL of a page in the current language.
Arguments:
- ``page_lookup`` (see `page_lookup`_ for more information)
+- ``as var_name`` (version 3.0 or later, optional; page_url can now be used to assign the resulting
+ URL to a context variable ``var_name``)
+
Example::
@@ -234,19 +237,14 @@ addresses to inform them of the broken link.
emits nothing, but sets a variable in the context with the specified name
to the resulting value.
- When using the ``as`` argument, any PageNotFound exceptions are always
- suppressed, regardless of the setting of :setting:`django:DEBUG`. In the
- event the page is not found, the context variable is set to an
- empty string.
-
- Arguments:
+ When using the ``as`` argument PageNotFound exceptions are always
+ suppressed, regardless of the setting of :setting:`django:DEBUG` and the
+ tag will simply emit an empty string in these cases.
- - ``page_lookup`` (see `page_lookup`_ for more information)
- - ``as var_name`` page_url can be used as an As Tag
+Example::
- Example::
- {# Emit a 'canonical' tag when the page is displayed on an alternate url #}
- {% page_url request.current_page as current_url %}{% if current_url and current_url != request.get_full_path %}<link rel="canonical" href="{% page_url request.current_page %}">{% endif %}
+ {# Emit a 'canonical' tag when the page is displayed on an alternate url #}
+ {% page_url request.current_page as current_url %}{% if current_url and current_url != request.get_full_path %}<link rel="canonical" href="{% page_url request.current_page %}">{% endif %}
.. templatetag:: page_attribute
View
2 setup.py
@@ -32,7 +32,7 @@
classifiers=CLASSIFIERS,
install_requires=[
'Django>=1.4',
- 'django-classy-tags>=0.3.4.1',
+ 'django-classy-tags>=0.5',
'south>=0.7.2',
'html5lib',
'django-mptt==0.5.2,==0.6',
View
2 test_requirements/requirements_base.txt
@@ -4,7 +4,7 @@ docopt==0.6.1
unittest-xml-reporting==1.0.3
sphinx
Pillow==2.0
-django-classy-tags>=0.3.4.1
+django-classy-tags>=0.5
South>=0.7.2
html5lib>=0.90
django-mptt>=0.6

0 comments on commit 6345c53

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