cms.toolbar.base: 60 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/base.py

Stats: 0 executed, 54 missed, 6 excluded, 64 ignored

  1. # -*- coding: utf-8 -*-
  2. from cms.toolbar.constants import ALIGNMENTS
  3. from django.conf import settings
  4. from django.core.exceptions import ImproperlyConfigured
  5. from django.utils import simplejson
  6. from django.utils.encoding import force_unicode
  7. from django.utils.functional import Promise
  8. class Serializable(object):
  9. """
  10. Base class for objects used in the toolbar. Abstracts the serialization and
  11. conversion to JSON.
  12. """
  13. # attributes that this type and all subclasses of this type should serialize
  14. base_attributes = []
  15. # additional attributes to serialize only on this type
  16. extra_attributes = []
  17. def as_json(self, context, **kwargs):
  18. """
  19. Converts the (serialized) data to JSON
  20. """
  21. data = self.serialize(context, **kwargs)
  22. return simplejson.dumps(data)
  23. def serialize(self, context, **kwargs):
  24. """
  25. Serializes it's data. Uses self.base_attributes, self.extra_attributes
  26. and self.get_extra_data to
  27. """
  28. data = {}
  29. for python, javascript in self.base_attributes:
  30. self._populate(data, python, javascript, context, **kwargs)
  31. for python, javascript in self.extra_attributes:
  32. self._populate(data, python, javascript, context, **kwargs)
  33. data.update(self.get_extra_data(context, **kwargs))
  34. return data
  35. def _populate(self, container, python, javascript, context, **kwargs):
  36. """
  37. Populates the *container* using the key *javascript* by accessing the
  38. attribute *python* on *self* (or serialize_*python* if that's a callable
  39. on *self*).
  40. """
  41. if hasattr(self, 'serialize_%s' % python):
  42. meth = getattr(self, 'serialize_%s' % python)
  43. value = meth(context, **kwargs)
  44. else:
  45. value = getattr(self, python)
  46. if isinstance(value, Promise):
  47. value = force_unicode(value)
  48. container[javascript] = value
  49. def get_extra_data(self, context, **kwargs):
  50. """
  51. Hook for subclasses to add more data.
  52. """
  53. return {}
  54. class Toolbar(Serializable):
  55. """
  56. A base toolbar, implements the request_hook API and the get_items API.
  57. """
  58. def __init__(self, request):
  59. self.request = request
  60. def get_items(self, context, **kwargs):
  61. return []
  62. def get_extra_data(self, context, **kwargs):
  63. raw_items = self.get_items(context, **kwargs)
  64. items = []
  65. for item in raw_items:
  66. items.append(item.serialize(context, toolbar=self, **kwargs))
  67. return {
  68. 'debug': settings.TEMPLATE_DEBUG,
  69. 'items': items,
  70. }
  71. def request_hook(self):
  72. """
  73. May return a HttpResponse instance
  74. """
  75. return None
  76. class BaseItem(Serializable):
  77. """
  78. Base class for toolbar items, has default attributes common to all items.
  79. """
  80. base_attributes = [
  81. ('order', 'order'), # automatically set
  82. ('alignment', 'dir'),
  83. ('item_type', 'type'),
  84. ('css_class', 'cls'),
  85. ]
  86. extra_attributes = []
  87. alignment = 'left'
  88. def __init__(self, alignment, css_class_suffix):
  89. """
  90. alignment: either cms.toolbar.constants.LEFT or
  91. cms.toolbar.constants.RIGHT
  92. css_class_suffix: suffix for the cms class to put on this item, prefix
  93. is always 'cms_toolbar-item'
  94. """
  95. if alignment not in ALIGNMENTS:
  96. raise ImproperlyConfigured("Item alignment %r is not valid, must "
  97. "either cms.toolbar.base.LEFT or "
  98. "cms.toolbar.base.RIGHT" % alignment)
  99. self.alignment = alignment
  100. self.css_class_suffix = css_class_suffix
  101. self.css_class = 'cms_toolbar-item_%s' % self.css_class_suffix
  102. def serialize(self, context, toolbar, **kwargs):
  103. counter_attr = 'counter_%s' % self.alignment
  104. current = getattr(toolbar, counter_attr, 0)
  105. this = current + 1
  106. self.order = this * 10
  107. setattr(toolbar, counter_attr, this)
  108. return super(BaseItem, self).serialize(context, toolbar=toolbar, **kwargs)