cms.plugin_rendering: 108 total statements, 0.0% covered

Generated: Wed 2013-03-13 10:33 CET

Source file: /media/Envs/Envs/filer-gallery/lib/python2.7/site-packages/cms/plugin_rendering.py

Stats: 0 executed, 95 missed, 13 excluded, 54 ignored

  1. # -*- coding: utf-8 -*-
  2. from cms.models.placeholdermodel import Placeholder
  3. from cms.plugin_processors import (plugin_meta_context_processor,
  4. mark_safe_plugin_processor)
  5. from cms.utils import get_language_from_request
  6. from cms.utils.django_load import iterload_objects
  7. from cms.utils.placeholder import get_placeholder_conf
  8. from django.conf import settings
  9. from django.template import Template, Context
  10. from django.template.defaultfilters import title
  11. from django.template.loader import render_to_string
  12. from django.utils.translation import ugettext_lazy as _
  13. # these are always called before all other plugin context processors
  14. DEFAULT_PLUGIN_CONTEXT_PROCESSORS = (
  15. plugin_meta_context_processor,
  16. )
  17. # these are always called after all other plugin processors
  18. DEFAULT_PLUGIN_PROCESSORS = (
  19. mark_safe_plugin_processor,
  20. )
  21. class PluginContext(Context):
  22. """
  23. This subclass of template.Context automatically populates itself using
  24. the processors defined in CMS_PLUGIN_CONTEXT_PROCESSORS.
  25. Additional processors can be specified as a list of callables
  26. using the "processors" keyword argument.
  27. """
  28. def __init__(self, dict, instance, placeholder, processors=None, current_app=None):
  29. super(PluginContext, self).__init__(dict, current_app=current_app)
  30. if not processors:
  31. processors = []
  32. for processor in DEFAULT_PLUGIN_CONTEXT_PROCESSORS:
  33. self.update(processor(instance, placeholder))
  34. for processor in iterload_objects(settings.CMS_PLUGIN_CONTEXT_PROCESSORS):
  35. self.update(processor(instance, placeholder))
  36. for processor in processors:
  37. self.update(processor(instance, placeholder))
  38. def render_plugin(context, instance, placeholder, template, processors=None,
  39. current_app=None):
  40. """
  41. Renders a single plugin and applies the post processors to it's rendered
  42. content.
  43. """
  44. if not processors:
  45. processors = []
  46. if isinstance(template, basestring):
  47. content = render_to_string(template, context)
  48. elif isinstance(template, Template):
  49. content = template.render(context)
  50. else:
  51. content = ''
  52. for processor in iterload_objects(settings.CMS_PLUGIN_PROCESSORS):
  53. content = processor(instance, placeholder, content, context)
  54. for processor in processors:
  55. content = processor(instance, placeholder, content, context)
  56. for processor in DEFAULT_PLUGIN_PROCESSORS:
  57. content = processor(instance, placeholder, content, context)
  58. return content
  59. def render_plugins(plugins, context, placeholder, processors=None):
  60. """
  61. Renders a collection of plugins with the given context, using the appropriate processors
  62. for a given placeholder name, and returns a list containing a "rendered content" string
  63. for each plugin.
  64. This is the main plugin rendering utility function, use this function rather than
  65. Plugin.render_plugin().
  66. """
  67. out = []
  68. total = len(plugins)
  69. for index, plugin in enumerate(plugins):
  70. plugin._render_meta.total = total
  71. plugin._render_meta.index = index
  72. context.push()
  73. out.append(plugin.render_plugin(context, placeholder, processors=processors))
  74. context.pop()
  75. return out
  76. def render_placeholder(placeholder, context_to_copy, name_fallback="Placeholder"):
  77. """
  78. Renders plugins for a placeholder on the given page using shallow copies of the
  79. given context, and returns a string containing the rendered output.
  80. """
  81. from cms.plugins.utils import get_plugins
  82. context = context_to_copy
  83. context.push()
  84. request = context['request']
  85. plugins = [plugin for plugin in get_plugins(request, placeholder)]
  86. page = placeholder.page if placeholder else None
  87. if page:
  88. template = page.template
  89. else:
  90. template = None
  91. # Add extra context as defined in settings, but do not overwrite existing context variables,
  92. # since settings are general and database/template are specific
  93. # TODO this should actually happen as a plugin context processor, but these currently overwrite
  94. # existing context -- maybe change this order?
  95. slot = getattr(placeholder, 'slot', None)
  96. extra_context = {}
  97. if slot:
  98. extra_context = get_placeholder_conf("extra_context", slot, template, {})
  99. for key, value in extra_context.items():
  100. if not key in context:
  101. context[key] = value
  102. content = []
  103. # Prepend frontedit toolbar output if applicable
  104. edit = False
  105. toolbar = getattr(request, 'toolbar', None)
  106. if (getattr(toolbar, 'edit_mode', False) and
  107. (not page or page.has_change_permission(request))):
  108. edit = True
  109. if edit:
  110. from cms.middleware.toolbar import toolbar_plugin_processor
  111. processors = (toolbar_plugin_processor,)
  112. else:
  113. processors = None
  114. content.extend(render_plugins(plugins, context, placeholder, processors))
  115. content = "".join(content)
  116. if edit:
  117. content = render_placeholder_toolbar(placeholder, context, content, name_fallback)
  118. context.pop()
  119. return content
  120. def render_placeholder_toolbar(placeholder, context, content, name_fallback=None):
  121. from cms.plugin_pool import plugin_pool
  122. request = context['request']
  123. page = placeholder.page if placeholder else None
  124. if not page:
  125. page = getattr(request, 'current_page', None)
  126. if page:
  127. template = page.template
  128. if name_fallback and not placeholder:
  129. placeholder = Placeholder.objects.create(slot=name_fallback)
  130. page.placeholders.add(placeholder)
  131. placeholder.page = page
  132. else:
  133. template = None
  134. if placeholder:
  135. slot = placeholder.slot
  136. else:
  137. slot = None
  138. installed_plugins = plugin_pool.get_all_plugins(slot, page)
  139. name = get_placeholder_conf("name", slot, template, title(slot))
  140. name = _(name)
  141. context.push()
  142. context['installed_plugins'] = installed_plugins
  143. context['language'] = get_language_from_request(request)
  144. context['placeholder_label'] = name
  145. context['placeholder'] = placeholder
  146. context['page'] = page
  147. toolbar = render_to_string("cms/toolbar/placeholder.html", context)
  148. context.pop()
  149. return "".join([toolbar, content])