Skip to content

Commit

Permalink
changed logic of plugin media rendering to be done in a middleware to…
Browse files Browse the repository at this point in the history
… 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
ojii committed Apr 13, 2010
1 parent a81390f commit e57c58a
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 18 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion cms/conf/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
34 changes: 34 additions & 0 deletions cms/middleware/media.py
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions cms/middleware/toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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")):
Expand Down
11 changes: 10 additions & 1 deletion cms/models/placeholdermodel.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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()
2 changes: 1 addition & 1 deletion cms/plugin_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion cms/plugins/googlemap/cms_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 2 additions & 2 deletions cms/plugins/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 6 additions & 8 deletions cms/templatetags/cms_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -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



Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand All @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions cms/templatetags/placeholder_tags.py
Original file line number Diff line number Diff line change
@@ -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()

Expand All @@ -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))
Expand Down
1 change: 0 additions & 1 deletion cms/utils/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions example/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
6 changes: 6 additions & 0 deletions menus/menu_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.