Permalink
Browse files

Merge branch 'master'

  • Loading branch information...
2 parents ce7eeea + 2cfaca1 commit ceaf6a52b9809f534bfa4b6e0ec3dc0dd8c54a4f @jedie committed Jan 4, 2012
@@ -111,6 +111,7 @@ def pylucid(request):
del(request.session["design_switch_pk"])
template_name = pagetree.design.template
+ # Used e.g.: in plugin templates: {% extends template_name %}
context["template_name"] = template_name
# Add the dbtemplates entry.
@@ -4,20 +4,15 @@
PyLucid plugin tools
~~~~~~~~~~~~~~~~~~~~
- :copyleft: 2009-2011 by the PyLucid team, see AUTHORS for more details.
+ :copyleft: 2009-2012 by the PyLucid team, see AUTHORS for more details.
:license: GNU GPL v3 or above, see LICENSE for more details.p
"""
-import re
-
-from django import http
-from django.conf import settings
from django.contrib import messages
from django.core import urlresolvers
from django.http import HttpResponse
-from django.utils.encoding import smart_str
from django.views.decorators.csrf import csrf_protect
from dbpreferences.forms import DBPreferencesBaseForm
@@ -122,111 +117,3 @@ def call_plugin(request, url_lang_code, prefix_url, rest_url):
return response
-
-#______________________________________________________________________________
-# ContextMiddleware functions
-
-# TODO: Should we use TemplateResponse?
-# https://docs.djangoproject.com/en/dev/ref/template-response/
-
-TAG_RE = re.compile("<!-- ContextMiddleware (.*?) -->", re.UNICODE)
-from django.utils.importlib import import_module
-from django.utils.functional import memoize
-
-_middleware_class_cache = {}
-
-def _get_middleware_class(plugin_name):
- plugin_name = plugin_name.encode('ascii') # check non-ASCII strings
-
- mod_name = "pylucid_plugins.%s.context_middleware" % plugin_name
- module = import_module(mod_name)
- middleware_class = getattr(module, "ContextMiddleware")
- return middleware_class
-_get_middleware_class = memoize(_get_middleware_class, _middleware_class_cache, 1)
-
-
-def context_middleware_request(request):
- """
- get from the template all context middleware plugins and call the request method.
- """
- context = request.PYLUCID.context
- context["context_middlewares"] = {}
-
- page_template = request.PYLUCID.page_template # page template content
-
- # FIXME:
- # We render the page here completely, only to get the ContextMiddlewares
- # This seems to be not the best way.
- #
- request._dont_call_lucid_tags = True # Don't render any lucidTags here
- content = page_template.render(context)
- request._dont_call_lucid_tags = False
-
- plugin_names = TAG_RE.findall(content)
-
-# messages.debug(request, "Found ContextMiddlewares in content via RE: %r" % plugin_names)
-
- for plugin_name in plugin_names:
- # Get the middleware class from the plugin
- try:
- middleware_class = _get_middleware_class(plugin_name)
- except ImportError, err:
- messages.error(request, "Can't import context middleware '%s': %s" % (plugin_name, err))
- continue
-
- # make a instance
- instance = middleware_class(request, context)
- # Add it to the context
- context["context_middlewares"][plugin_name] = instance
-# messages.debug(request, "Init ContextMiddleware %r" % plugin_name)
-
-
-def context_middleware_response(request, response):
- """
- replace the context middleware tags in the response, with the plugin render output
- """
- context = request.PYLUCID.context
- context_middlewares = context["context_middlewares"]
- def replace(match):
- plugin_name = match.group(1)
- try:
- middleware_class_instance = context_middlewares[plugin_name]
- except KeyError, err:
- return "[Error: context middleware %r doesn't exist! Existing middlewares are: %r]" % (
- plugin_name, context_middlewares.keys()
- )
-
- # Add info for pylucid_project.apps.pylucid.context_processors.pylucid
- request.plugin_name = plugin_name
- request.method_name = "ContextMiddleware"
-
- middleware_response = middleware_class_instance.render()
-
- request.plugin_name = None
- request.method_name = None
-
- if middleware_response == None:
- return ""
- elif isinstance(middleware_response, unicode):
- return smart_str(middleware_response, encoding=settings.DEFAULT_CHARSET)
- elif isinstance(middleware_response, str):
- return middleware_response
- elif isinstance(middleware_response, http.HttpResponse):
- return middleware_response.content
- else:
- raise RuntimeError(
- "plugin context middleware render() must return"
- " http.HttpResponse instance or a basestring or None!"
- )
-
- # FIXME: A HttpResponse allways convert unicode into string. So we need to do that here:
- # Or we say, context render should not return a HttpResponse?
-# from django.utils.encoding import smart_str
-# complete_page = smart_str(complete_page)
-
- source_content = response.content
-
- new_content = TAG_RE.sub(replace, source_content)
- response.content = new_content
- return response
-
@@ -4,7 +4,7 @@
PyLucid
~~~~~~~
- :copyleft: 2009-2011 by the PyLucid team, see AUTHORS for more details.
+ :copyleft: 2009-2012 by the PyLucid team, see AUTHORS for more details.
:license: GNU GPL v3 or above, see LICENSE for more details.
"""
@@ -61,17 +61,6 @@ def _get_page_content(request):
return pagecontent
-
-
-def _apply_context_middleware(request, response):
- """
- Before we "send" the response back to the client, we replace all existing
- context_middleware tags.
- """
- response = pylucid_plugin.context_middleware_response(request, response)
- return response
-
-
def _render_page(request, pagetree, url_lang_code, prefix_url=None, rest_url=None):
""" render a cms page """
request.PYLUCID.pagetree = pagetree
@@ -122,24 +111,14 @@ def _render_page(request, pagetree, url_lang_code, prefix_url=None, rest_url=Non
# Create initial context object
request.PYLUCID.context = context = RequestContext(request)
- # Add the page template content to the pylucid objects
- # Used to find context middleware plugins and in _render_template()
- template_name = context["template_name"] # Added in pylucid.context_processors
- page_template, origin = loader.find_template(template_name)
- request.PYLUCID.page_template = page_template
-
- # Get all plugin context middlewares from the template and add them to the context
- pylucid_plugin.context_middleware_request(request)
-
- context["page_content"] = None # it will be filled either by plugin or by PageContent
-
+ # it will be filled either by plugin or by PageContent:
+ context["page_content"] = None
# call a pylucid plugin "html get view", if exist
get_view_response = PYLUCID_PLUGINS.call_get_views(request)
if isinstance(get_view_response, http.HttpResponse):
# Plugin would be build the complete html page
- response = _apply_context_middleware(request, get_view_response)
- return response
+ return get_view_response
elif isinstance(get_view_response, basestring):
# Plugin replace the page content
context["page_content"] = get_view_response
@@ -159,8 +138,7 @@ def _render_page(request, pagetree, url_lang_code, prefix_url=None, rest_url=Non
page_plugin_response = pylucid_plugin.call_plugin(request, url_lang_code, prefix_url, rest_url)
if isinstance(page_plugin_response, http.HttpResponse):
# Plugin would be build the complete html page
- response = _apply_context_middleware(request, page_plugin_response)
- return response
+ return page_plugin_response
elif isinstance(page_plugin_response, basestring):
# Plugin replace the page content
context["page_content"] = page_plugin_response
@@ -185,6 +163,8 @@ def _render_page(request, pagetree, url_lang_code, prefix_url=None, rest_url=Non
# Render django tags in PageContent with the global context
context["page_content"] = render.render_string_template(context["page_content"], context)
+ template_name = context["template_name"] # Added in pylucid.context_processors
+ page_template, origin = loader.find_template(template_name)
pre_render_global_template.send(sender=None, request=request, page_template=page_template)
# Render django tags in global template with global context
@@ -194,8 +174,6 @@ def _render_page(request, pagetree, url_lang_code, prefix_url=None, rest_url=Non
response = http.HttpResponse(complete_page, mimetype="text/html")
response["content-language"] = context["page_language"]
- # replace/render pylucid plugin context middlewares
- response = _apply_context_middleware(request, response)
return response
@@ -254,7 +232,7 @@ def send_head_file(request, filepath):
def _prepage_request(request, lang_entry):
"""
shared function for serval views.
- *
+ *
"""
# setup i18n language settings
i18n.setup_language(request, lang_entry)
@@ -307,7 +285,7 @@ def _lang_code_is_pagetree(request, url_lang_code):
# It's a valid language code
return False
- # Check if url language code is a pagetree slug
+ # Check if url language code is a pagetree slug
exist = PageTree.on_site.filter(slug=url_lang_code).count()
if exist > 0:
return True
Oops, something went wrong.

0 comments on commit ceaf6a5

Please sign in to comment.