Permalink
Browse files

changed logic of plugin media rendering to be done in a middleware to…

… enable non-page placeholders to register media files

added the ability to invalidate the menu cache
made CMSPluginBase.get_plugin_media context aware
  • Loading branch information...
1 parent a81390f commit e57c58abf0c7bf2b4c72f0c0dddcfd7690d2d010 ojii committed Apr 13, 2010
View
@@ -30,4 +30,5 @@
- placeholderend templatetag added: {% placeholder "content" %}There is no content here{% endplaceholder %}
- plugins can now be used in other apps :) see cms/docs/placeholders.txt
- plugins can now be grouped
-- a lot of bugfixes
+- a lot of bugfixes
+- the cms now depends on the cms.middleware.media.PlaceholderMediaMiddleware middleware
View
@@ -46,4 +46,5 @@ def post_patch_check():
if settings.CMS_PERMISSION and not 'cms.middleware.user.CurrentUserMiddleware' in settings.MIDDLEWARE_CLASSES:
raise ImproperlyConfigured('CMS Permission system requires cms.middleware.user.CurrentUserMiddleware.\n'
'Please put it into your MIDDLEWARE_CLASSES in settings file')
-
+ if 'cms.middleware.media.PlaceholderMediaMiddleware' not in settings.MIDDLEWARE_CLASSES:
+ raise ImproperlyConfigured("CMS requires cms.middleware.media.PlaceholderMediaMiddleware to be in MIDDLEWARE_CLASSES.")
View
@@ -0,0 +1,34 @@
+from django.forms.widgets import Media
+from django.utils.encoding import smart_unicode
+from django.conf import settings
+from cms.middleware.toolbar import HTML_TYPES
+
+def inster_before_tag(string, tag, insertion):
+ no_case = string.lower()
+ index = no_case.find("<%s" % tag.lower())
+ if index > -1:
+ start_tag = index
+ return string[:start_tag] + insertion + string[start_tag:]
+ else:
+ return string
+
+class PlaceholderMediaMiddleware(object):
+ def inject_media(self, request, response):
+ if request.is_ajax():
+ return False
+ if response.status_code != 200:
+ return False
+ if not response['Content-Type'].split(';')[0] in HTML_TYPES:
+ return False
+ if request.path_info.startswith(settings.MEDIA_URL):
+ return False
+ return True
+
+ def process_request(self, request):
+ request.placeholder_media = Media()
+
+ def process_response(self, request, response):
+ if self.inject_media(request, response):
+ response.content = inster_before_tag(smart_unicode(response.content),
+ u'/head', smart_unicode(request.placeholder_media.render()))
+ return response
@@ -13,7 +13,7 @@
from django.utils import simplejson
from django.utils.encoding import smart_unicode
-_HTML_TYPES = ('text/html', 'application/xhtml+xml')
+HTML_TYPES = ('text/html', 'application/xhtml+xml')
def inster_after_tag(string, tag, insertion):
no_case = string.lower()
@@ -39,7 +39,7 @@ def show_toolbar(self, request, response):
return False
if response.status_code != 200:
return False
- if not response['Content-Type'].split(';')[0] in _HTML_TYPES:
+ if not response['Content-Type'].split(';')[0] in HTML_TYPES:
return False
try:
if request.path_info.startswith(reverse("admin:index")):
@@ -1,5 +1,7 @@
from django.db import models
from django.utils.translation import ugettext_lazy as _
+from django.forms.widgets import Media
+import operator
class Placeholder(models.Model):
@@ -22,4 +24,11 @@ def render(self, context, width):
from cms.utils.plugin import render_plugins_for_context
if not 'request' in context:
return '<!-- missing request -->'
- return render_plugins_for_context(self, context, width or self.default_width)
+ return render_plugins_for_context(self, context, width or self.default_width)
+
+ def get_media(self, request, context):
+ from cms.plugins.utils import get_plugin_media
+ media_classes = [get_plugin_media(request, context, plugin) for plugin in self.cmsplugin_set.all()]
+ if media_classes:
+ return reduce(operator.add, media_classes)
+ return Media()
View
@@ -128,7 +128,7 @@ def render_change_form(self, request, context, add=False, change=False, form_url
return super(CMSPluginBase, self).render_change_form(request, context, add, change, form_url, obj)
- def get_plugin_media(self, request, plugin):
+ def get_plugin_media(self, request, context, plugin):
return self.pluginmedia
def has_add_permission(self, request, *args, **kwargs):
@@ -23,7 +23,7 @@ def render(self, context, instance, placeholder):
})
return context
- def get_plugin_media(self, request, plugin):
+ def get_plugin_media(self, request, context, plugin):
return Media(js = ('http://maps.google.com/maps?file=api&amp;v=2&amp;key=%s&amp;hl=%s' % (settings.GOOGLE_MAPS_API_KEY, request.LANGUAGE_CODE),))
plugin_pool.register_plugin(GoogleMapPlugin)
View
@@ -13,9 +13,9 @@ def get_plugins(request, placeholder, lang=None):
).order_by('placeholder', 'position').select_related())
return getattr(placeholder, '_%s_plugins_cache' % lang)
-def get_plugin_media(request, plugin):
+def get_plugin_media(request, context, plugin):
instance, plugin = plugin.get_plugin_instance()
- return plugin.get_plugin_media(request, instance)
+ return plugin.get_plugin_media(request, context, instance)
def get_plugins_for_page(request, page, lang=None):
if not page:
@@ -10,6 +10,7 @@
from django.template.defaultfilters import title
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
+from django.forms.widgets import Media
@@ -158,6 +159,7 @@ def render(self, context):
if not page or page == "dummy":
return ""
placeholder = page.placeholders.get(slot=self.name)
+ request.placeholder_media += placeholder.get_media(request, context)
content = render_plugins_for_context(placeholder, context, width)
if not content and self.nodelist_or:
return self.nodelist_or.render(context)
@@ -318,6 +320,7 @@ def show_uncached_placeholder_by_id(context, placeholder_name, reverse_id, lang=
def do_plugins_media(parser, token):
return PluginsMediaNode()
+
class PluginsMediaNode(template.Node):
"""This template node is used to output media for plugins.
@@ -332,14 +335,9 @@ def render(self, context):
return ''
from cms.plugins.utils import get_plugins_media
plugins_media = None
- try:
- plugins_media = get_plugins_media(request, request._current_page_cache) # make sure the plugin cache is filled
- except:
- import sys
- info = sys.exc_info()
- import traceback
- traceback.print_exception(*info)
- raise
+ # make sure the plugin cache is filled
+ plugins_media = get_plugins_media(request, request._current_page_cache)
+ return u''
if plugins_media:
return plugins_media.render()
else:
@@ -1,5 +1,6 @@
from django import template
from django.template.defaultfilters import safe
+from cms.plugins.utils import get_plugin_media
register = template.Library()
@@ -10,11 +11,15 @@ def __init__(self, placeholder, width):
self.width = width
def render(self, context):
+ request = context.get('request', None)
+ if not request:
+ return ''
if self.width is not None:
width = self.width.resolve(context)
else:
width = self.width
placeholder = self.placeholder.resolve(context)
+ request.placeholder_media += placeholder.get_media(request, context)
if not placeholder:
return ''
return safe(placeholder.render(context, width))
View
@@ -48,7 +48,6 @@ def render_plugins_for_context(placeholder, context_to_copy, width=None):
if ("edit" in request.GET or request.session.get("cms_edit", False)) and \
'cms.middleware.toolbar.ToolbarMiddleware' in django_settings.MIDDLEWARE_CLASSES and \
request.user.is_staff and request.user.is_authenticated() and \
- placeholder.has_change_permission(request) and \
(not page or page.has_change_permission(request)):
edit = True
if edit and settings.PLACEHOLDER_FRONTEND_EDITING:
View
@@ -82,6 +82,7 @@
'django.middleware.common.CommonMiddleware',
'django.middleware.doc.XViewMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
+ 'cms.middleware.media.PlaceholderMediaMiddleware',
'cms.middleware.user.CurrentUserMiddleware',
'cms.middleware.page.CurrentPageMiddleware',
'cms.middleware.toolbar.ToolbarMiddleware',
View
@@ -10,6 +10,7 @@ def __init__(self):
self.menus = {}
self.modifiers = []
self.discovered = False
+ self.cache_keys = set()
def discover_menus(self):
if self.discovered:
@@ -42,6 +43,7 @@ def register_modifier(self, modifier_class):
def _build_nodes(self, request, site_id):
lang = request.LANGUAGE_CODE
key = "menu_nodes_%s_%s" % (lang, site_id)
+ self.cache_keys.add(key)
cached_nodes = cache.get(key, None)
if cached_nodes:
return cached_nodes
@@ -135,5 +137,9 @@ def get_nodes_by_attribute(self, nodes, name, value):
if node.attr.get(name, None) == value:
found.append(node)
return found
+
+ def invalidate_cache(self):
+ for key in self.cache_keys:
+ cache.delete(key)
menu_pool = MenuPool()

0 comments on commit e57c58a

Please sign in to comment.