cms.toolbar.items: 101 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/toolbar/items.py

Stats: 0 executed, 94 missed, 7 excluded, 141 ignored

  1. # -*- coding: utf-8 -*-
  2. from cms.toolbar.base import BaseItem, Serializable
  3. from django.conf import settings
  4. from django.core.exceptions import ImproperlyConfigured
  5. from django.middleware.csrf import get_token
  6. from django.template.context import RequestContext, Context
  7. from django.template.loader import render_to_string
  8. from django.utils.html import strip_spaces_between_tags
  9. class Switcher(BaseItem):
  10. """
  11. A 'switcher' button, state is defined using GET (and optionally a session
  12. entry).
  13. """
  14. item_type = 'switcher'
  15. extra_attributes = [
  16. ('add_parameter', 'addParameter'),
  17. ('remove_parameter', 'removeParameter'),
  18. ('title', 'title'),
  19. ]
  20. def __init__(self, alignment, css_class_suffix, add_parameter,
  21. remove_parameter, title, session_key=None):
  22. """
  23. add_parameter: parameter which indicates the True state
  24. remove_parameter: parameter which indicates the False state
  25. title: name of the switcher
  26. session_key: key in the session which has a boolean value to indicate
  27. the state of this switcher.
  28. """
  29. super(Switcher, self).__init__(alignment, css_class_suffix)
  30. self.add_parameter = add_parameter
  31. self.remove_parameter = remove_parameter
  32. self.title = title
  33. self.session_key = session_key
  34. def get_state(self, request):
  35. state = self.add_parameter in request.GET
  36. if self.session_key and request.session.get(self.session_key, False):
  37. return True
  38. return state
  39. def get_extra_data(self, context, toolbar, **kwargs):
  40. return {
  41. 'state': self.get_state(toolbar.request)
  42. }
  43. class Anchor(BaseItem):
  44. """
  45. A link.
  46. """
  47. item_type = 'anchor'
  48. extra_attributes = [
  49. ('url', 'url'),
  50. ('title', 'title'),
  51. ]
  52. def __init__(self, alignment, css_class_suffix, title, url):
  53. """
  54. title: Name of the link
  55. url: Target of the link
  56. """
  57. super(Anchor, self).__init__(alignment, css_class_suffix)
  58. self.title = title
  59. if callable(url):
  60. self.serialize_url = url
  61. else:
  62. self.url = url
  63. class HTML(BaseItem):
  64. """
  65. HTML item, can do whatever it want
  66. """
  67. item_type = 'html'
  68. extra_attributes = [
  69. ('html', 'html'),
  70. ]
  71. def __init__(self, alignment, css_class_suffix, html):
  72. """
  73. html: The HTML to render.
  74. """
  75. super(HTML, self).__init__(alignment, css_class_suffix)
  76. self.html = html
  77. class TemplateHTML(BaseItem):
  78. """
  79. Same as HTML, but renders a template to generate the HTML.
  80. """
  81. item_type = 'html'
  82. def __init__(self, alignment, css_class_suffix, template):
  83. """
  84. template: the template to render
  85. """
  86. super(TemplateHTML, self).__init__(alignment, css_class_suffix)
  87. self.template = template
  88. def get_extra_data(self, context, toolbar, **kwargs):
  89. new_context = RequestContext(toolbar.request)
  90. rendered = render_to_string(self.template, new_context)
  91. stripped = strip_spaces_between_tags(rendered.strip())
  92. return {
  93. 'html': stripped,
  94. }
  95. class GetButton(BaseItem):
  96. """
  97. A button which triggers a GET request
  98. """
  99. item_type = 'button'
  100. extra_attributes = [
  101. ('title', 'title'),
  102. ('icon', 'icon'),
  103. ('url', 'redirect'),
  104. ]
  105. def __init__(self, alignment, css_class_suffix, title, url, icon=None):
  106. """
  107. title: name of the button
  108. icon: icon of the button, relative to STATIC_URL
  109. url: target of the GET request
  110. """
  111. super(GetButton, self).__init__(alignment, css_class_suffix)
  112. self.icon = icon
  113. self.title = title
  114. if callable(url):
  115. self.serialize_url = url
  116. else:
  117. self.url = url
  118. class PostButton(BaseItem):
  119. """
  120. A button which triggers a POST request
  121. """
  122. item_type = 'button'
  123. extra_attributes = [
  124. ('title', 'title'),
  125. ('icon', 'icon'),
  126. ('action', 'action'),
  127. ]
  128. def __init__(self, alignment, css_class_suffix, title, icon, action, *args, **kwargs):
  129. """
  130. title: name of the button
  131. icon: icon of the button, relative to STATIC_URL
  132. action: target of the request
  133. *args, **kwargs: data to POST
  134. A csrfmiddlewaretoken is always injected into the request.
  135. """
  136. super(PostButton, self).__init__(alignment, css_class_suffix)
  137. self.title = title
  138. self.icon = icon
  139. self.action = action
  140. self.args = args
  141. self.kwargs = kwargs
  142. def get_extra_data(self, context, toolbar, **kwargs):
  143. double = self.kwargs.copy()
  144. double['csrfmiddlewaretoken'] = get_token(toolbar.request)
  145. hidden = render_to_string('cms/toolbar/items/_post_button_hidden.html',
  146. Context({'single': self.args,
  147. 'double': double}))
  148. return {
  149. 'hidden': hidden,
  150. }
  151. class ListItem(Serializable):
  152. """
  153. A item in a dropdown list (List).
  154. """
  155. base_attributes = [
  156. ('css_class', 'cls'),
  157. ('title', 'title'),
  158. ('url', 'url'),
  159. ('icon', 'icon'),
  160. ('method', 'method'),
  161. ]
  162. extra_attributes = []
  163. def __init__(self, css_class_suffix, title, url, method='GET', icon=None):
  164. """
  165. title: name of the list
  166. url: target of the item
  167. icon: icon of the item, relative to STATIC_URL
  168. """
  169. self.css_class_suffix = css_class_suffix
  170. self.css_class = 'cms_toolbar-item_%s' % self.css_class_suffix
  171. self.title = title
  172. self.method = method
  173. self.icon = icon
  174. if callable(url):
  175. self.serialize_url = url
  176. else:
  177. self.url = url
  178. class List(BaseItem):
  179. """
  180. A dropdown list
  181. """
  182. item_type = 'list'
  183. extra_attributes = [
  184. ('title', 'title'),
  185. ('icon', 'icon'),
  186. ]
  187. def __init__(self, alignment, css_class_suffix, title, icon, items):
  188. """
  189. title: name of the item
  190. icon: icon of the item, relative to STATIC_URL
  191. items: an iterable of ListItem instances.
  192. """
  193. super(List, self).__init__(alignment, css_class_suffix)
  194. self.title = title
  195. self.icon = icon
  196. self.validate_items(items)
  197. self.raw_items = items
  198. def validate_items(self, items):
  199. for item in items:
  200. if not isinstance(item, ListItem):
  201. raise ImproperlyConfigured(
  202. 'Only ListItem instances are allowed to be used inside of '
  203. 'List instances'
  204. )
  205. def get_extra_data(self, context, **kwargs):
  206. items = [item.serialize(context, **kwargs)
  207. for item in self.raw_items]
  208. return {
  209. 'items': items
  210. }