cms.utils.plugins: 100 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/utils/plugins.py

Stats: 0 executed, 89 missed, 11 excluded, 44 ignored

  1. # -*- coding: utf-8 -*-
  2. from cms.exceptions import DuplicatePlaceholderWarning
  3. from cms.models import Page
  4. from cms.templatetags.cms_tags import Placeholder
  5. from cms.utils.placeholder import validate_placeholder_name
  6. from django.contrib.sites.models import Site
  7. from django.shortcuts import get_object_or_404
  8. from django.template import (NodeList, TextNode, VariableNode,
  9. TemplateSyntaxError)
  10. from django.template.loader import get_template
  11. from django.template.loader_tags import (ConstantIncludeNode, ExtendsNode,
  12. BlockNode)
  13. import warnings
  14. from sekizai.helpers import is_variable_extend_node
  15. def get_page_from_plugin_or_404(cms_plugin):
  16. return get_object_or_404(Page, placeholders=cms_plugin.placeholder)
  17. def _extend_blocks(extend_node, blocks):
  18. """
  19. Extends the dictionary `blocks` with *new* blocks in the parent node (recursive)
  20. """
  21. # we don't support variable extensions
  22. if is_variable_extend_node(extend_node):
  23. return
  24. parent = extend_node.get_parent(None)
  25. # Search for new blocks
  26. for node in parent.nodelist.get_nodes_by_type(BlockNode):
  27. if not node.name in blocks:
  28. blocks[node.name] = node
  29. else:
  30. # set this node as the super node (for {{ block.super }})
  31. block = blocks[node.name]
  32. seen_supers = []
  33. while hasattr(block.super, 'nodelist') and block.super not in seen_supers:
  34. seen_supers.append(block.super)
  35. block = block.super
  36. block.super = node
  37. # search for further ExtendsNodes
  38. for node in parent.nodelist.get_nodes_by_type(ExtendsNode):
  39. _extend_blocks(node, blocks)
  40. break
  41. def _find_topmost_template(extend_node):
  42. parent_template = extend_node.get_parent({})
  43. for node in parent_template.nodelist.get_nodes_by_type(ExtendsNode):
  44. # Their can only be one extend block in a template, otherwise django raises an exception
  45. return _find_topmost_template(node)
  46. # No ExtendsNode
  47. return extend_node.get_parent({})
  48. def _extend_nodelist(extend_node):
  49. """
  50. Returns a list of placeholders found in the parent template(s) of this
  51. ExtendsNode
  52. """
  53. # we don't support variable extensions
  54. if is_variable_extend_node(extend_node):
  55. return []
  56. blocks = extend_node.blocks
  57. _extend_blocks(extend_node, blocks)
  58. placeholders = []
  59. for block in blocks.values():
  60. placeholders += _scan_placeholders(block.nodelist, block, blocks.keys())
  61. # Scan topmost template for placeholder outside of blocks
  62. parent_template = _find_topmost_template(extend_node)
  63. placeholders += _scan_placeholders(parent_template.nodelist, None, blocks.keys())
  64. return placeholders
  65. def _scan_placeholders(nodelist, current_block=None, ignore_blocks=None):
  66. placeholders = []
  67. if ignore_blocks is None:
  68. ignore_blocks = []
  69. for node in nodelist:
  70. # check if this is a placeholder first
  71. if isinstance(node, Placeholder):
  72. placeholders.append(node.get_name())
  73. # if it's a Constant Include Node ({% include "template_name.html" %})
  74. # scan the child template
  75. elif isinstance(node, ConstantIncludeNode):
  76. # if there's an error in the to-be-included template, node.template becomes None
  77. if node.template:
  78. placeholders += _scan_placeholders(node.template.nodelist, current_block)
  79. # handle {% extends ... %} tags
  80. elif isinstance(node, ExtendsNode):
  81. placeholders += _extend_nodelist(node)
  82. # in block nodes we have to scan for super blocks
  83. elif isinstance(node, VariableNode) and current_block:
  84. if node.filter_expression.token == 'block.super':
  85. if not hasattr(current_block.super, 'nodelist'):
  86. raise TemplateSyntaxError("Cannot render block.super for blocks without a parent.")
  87. placeholders += _scan_placeholders(current_block.super.nodelist, current_block.super)
  88. # ignore nested blocks which are already handled
  89. elif isinstance(node, BlockNode) and node.name in ignore_blocks:
  90. continue
  91. # if the node has the newly introduced 'child_nodelists' attribute, scan
  92. # those attributes for nodelists and recurse them
  93. elif hasattr(node, 'child_nodelists'):
  94. for nodelist_name in node.child_nodelists:
  95. if hasattr(node, nodelist_name):
  96. subnodelist = getattr(node, nodelist_name)
  97. if isinstance(subnodelist, NodeList):
  98. if isinstance(node, BlockNode):
  99. current_block = node
  100. placeholders += _scan_placeholders(subnodelist, current_block)
  101. # else just scan the node for nodelist instance attributes
  102. else:
  103. for attr in dir(node):
  104. obj = getattr(node, attr)
  105. if isinstance(obj, NodeList):
  106. if isinstance(node, BlockNode):
  107. current_block = node
  108. placeholders += _scan_placeholders(obj, current_block)
  109. return placeholders
  110. def get_placeholders(template):
  111. compiled_template = get_template(template)
  112. placeholders = _scan_placeholders(compiled_template.nodelist)
  113. clean_placeholders = []
  114. for placeholder in placeholders:
  115. if placeholder in clean_placeholders:
  116. warnings.warn("Duplicate placeholder found: `%s`" % placeholder, DuplicatePlaceholderWarning)
  117. else:
  118. validate_placeholder_name(placeholder)
  119. clean_placeholders.append(placeholder)
  120. return clean_placeholders
  121. SITE_VAR = "site__exact"
  122. def current_site(request):
  123. if SITE_VAR in request.REQUEST:
  124. return Site.objects.get(pk=request.REQUEST[SITE_VAR])
  125. else:
  126. site_pk = request.session.get('cms_admin_site', None)
  127. if site_pk:
  128. try:
  129. return Site.objects.get(pk=site_pk)
  130. except Site.DoesNotExist:
  131. return None
  132. else:
  133. return Site.objects.get_current()