Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #1596 from ojii/fix-show-placeholder-sekizai

Fix show placeholder sekizai
  • Loading branch information...
commit 5a03060c2eb8b6284bbb66b49f18828c130959f8 2 parents e1bd3ee + ecd464d
@digi604 digi604 authored
View
46 cms/templatetags/cms_tags.py
@@ -19,6 +19,7 @@
from django.utils.translation import ugettext_lazy as _, get_language
from itertools import chain
import re
+from sekizai.helpers import Watcher, get_varname
register = template.Library()
@@ -368,6 +369,15 @@ def get_context(self, context, cl, spec):
return {'title': spec.title(), 'choices': unique_choices}
+def _restore_sekizai(context, changes):
+ varname = get_varname()
+ sekizai_container = context[varname]
+ for key, values in changes.items():
+ sekizai_namespace = sekizai_container[key]
+ for value in values:
+ sekizai_namespace.append(value)
+
+
def _show_placeholder_for_page(context, placeholder_name, page_lookup, lang=None,
site=None, cache_result=True):
"""
@@ -389,26 +399,30 @@ def _show_placeholder_for_page(context, placeholder_name, page_lookup, lang=None
if lang is None:
lang = get_language_from_request(request)
- content = None
-
if cache_result:
base_key = _get_cache_key('_show_placeholder_for_page', page_lookup, lang, site_id)
cache_key = _clean_key('%s_placeholder:%s' % (base_key, placeholder_name))
- content = cache.get(cache_key)
-
- if not content:
- page = _get_page_by_untyped_arg(page_lookup, request, site_id)
- if not page:
- return {'content': ''}
- try:
- placeholder = page.placeholders.get(slot=placeholder_name)
- except PlaceholderModel.DoesNotExist:
- if settings.DEBUG:
- raise
- return {'content': ''}
- content = render_placeholder(placeholder, context, placeholder_name)
+ cached_value = cache.get(cache_key)
+ if isinstance(cached_value, dict): # new style
+ _restore_sekizai(context, cached_value['sekizai'])
+ return {'content': cached_value['content']}
+ elif isinstance(cached_value, basestring): # old style
+ return {'content': cached_value}
+
+ page = _get_page_by_untyped_arg(page_lookup, request, site_id)
+ if not page:
+ return {'content': ''}
+ try:
+ placeholder = page.placeholders.get(slot=placeholder_name)
+ except PlaceholderModel.DoesNotExist:
+ if settings.DEBUG:
+ raise
+ return {'content': ''}
+ watcher = Watcher(context)
+ content = render_placeholder(placeholder, context, placeholder_name)
+ changes = watcher.get_changes()
if cache_result:
- cache.set(cache_key, content, get_cms_setting('CACHE_DURATIONS')['content'])
+ cache.set(cache_key, {'content': content, 'sekizai': changes}, get_cms_setting('CACHE_DURATIONS')['content'])
if content:
return {'content': mark_safe(content)}
View
1  cms/test_utils/project/templates/alt_plugin_templates/show_placeholder/cms/plugins/text.html
@@ -0,0 +1 @@
+{% load sekizai_tags %}{% addtoblock "js" %}JAVASCRIPT{% endaddtoblock %}
View
58 cms/tests/templatetags.py
@@ -1,7 +1,10 @@
from __future__ import with_statement
import copy
-from cms.api import create_page, create_title
+from django.test import RequestFactory, TestCase
+import os
+from cms.api import create_page, create_title, add_plugin
from cms.models.pagemodel import Page, Placeholder
+from cms.plugins.text.cms_plugins import TextPlugin
from cms.templatetags.cms_tags import (get_site_id, _get_page_by_untyped_arg,
_show_placeholder_for_page)
from cms.test_utils.fixtures.templatetags import TwoPagesFixture
@@ -9,13 +12,11 @@
from cms.test_utils.util.context_managers import SettingsOverride
from cms.utils import get_cms_setting
from cms.utils.plugins import get_placeholders
-from django.conf import settings
from django.contrib.sites.models import Site
from django.core import mail
from django.core.exceptions import ImproperlyConfigured
from django.http import HttpRequest
from django.template import RequestContext, Context
-from unittest import TestCase
from django.template.base import Template
from django.utils.html import escape
@@ -60,13 +61,6 @@ class FakeRequest(object):
class TemplatetagDatabaseTests(TwoPagesFixture, SettingsOverrideTestCase):
-
- def setUp(self):
- self._prev_DEBUG = settings.DEBUG
-
- def tearDown(self):
- settings.DEBUG = self._prev_DEBUG
-
def _getfirst(self):
return Page.objects.public().get(title_set__title='first')
@@ -129,19 +123,19 @@ def test_show_placeholder_for_page_placeholder_does_not_exist(self):
Verify ``show_placeholder`` correctly handles being given an
invalid identifier.
"""
- settings.DEBUG = True # So we can see the real exception raised
- request = HttpRequest()
- request.REQUEST = {}
- request.session = {}
- self.assertRaises(Placeholder.DoesNotExist,
- _show_placeholder_for_page,
- RequestContext(request),
- 'does_not_exist',
- 'myreverseid')
- settings.DEBUG = False # Now test the non-debug output
- content = _show_placeholder_for_page(RequestContext(request),
- 'does_not_exist', 'myreverseid')
- self.assertEqual(content['content'], '')
+ with SettingsOverride(DEBUG=True):
+ request = HttpRequest()
+ request.REQUEST = {}
+ request.session = {}
+ self.assertRaises(Placeholder.DoesNotExist,
+ _show_placeholder_for_page,
+ RequestContext(request),
+ 'does_not_exist',
+ 'myreverseid')
+ with SettingsOverride(DEBUG=False):
+ content = _show_placeholder_for_page(RequestContext(request),
+ 'does_not_exist', 'myreverseid')
+ self.assertEqual(content['content'], '')
def test_untranslated_language_url(self):
""" Tests page_language_url templatetag behavior when used on a page
@@ -192,3 +186,21 @@ def test_untranslated_language_url(self):
context['request'].current_page = page_3.publisher_public
res = tpl.render(context)
self.assertEqual(res,"/de/")
+
+
+class NoFixtureDatabaseTemplateTagTests(TestCase):
+ def test_cached_show_placeholder_sekizai(self):
+ from cms.test_utils import project
+ template_dir = os.path.join(os.path.dirname(project.__file__), 'templates', 'alt_plugin_templates', 'show_placeholder')
+ page = create_page('Test', 'col_two.html', 'en')
+ placeholder = page.placeholders.all()[0]
+ add_plugin(placeholder, TextPlugin, 'en', body='HIDDEN')
+ request = RequestFactory().get('/')
+ with SettingsOverride(TEMPLATE_DIRS=[template_dir]):
+ template = Template("{% load cms_tags sekizai_tags %}{% show_placeholder slot page 'en' 1 %}{% render_block 'js' %}")
+ context = RequestContext(request, {'page': page, 'slot': placeholder.slot})
+ output = template.render(context)
+ self.assertIn('JAVASCRIPT', output)
+ context = RequestContext(request, {'page': page, 'slot': placeholder.slot})
+ output = template.render(context)
+ self.assertIn('JAVASCRIPT', output)
View
2  setup.py
@@ -33,7 +33,7 @@
'south>=0.7.2',
'html5lib',
'django-mptt>=0.5.1,<0.5.3',
- 'django-sekizai>=0.6.1',
+ 'django-sekizai>=0.7',
],
tests_require=[
'django-reversion>=1.6',
View
2  test_requirements/requirements_base.txt
@@ -6,7 +6,7 @@ django-classy-tags>=0.3.4.1
South>=0.7.2
html5lib>=0.90
django-mptt==0.5.2
-django-sekizai>=0.6.1
+django-sekizai>=0.7
argparse
dj-database-url
transifex-client
Please sign in to comment.
Something went wrong with that request. Please try again.