django.contrib.messages.storage.base: 81 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/django/contrib/messages/storage/base.py

Stats: 0 executed, 76 missed, 5 excluded, 100 ignored

  1. from django.conf import settings
  2. from django.utils.encoding import force_unicode, StrAndUnicode
  3. from django.contrib.messages import constants, utils
  4. LEVEL_TAGS = utils.get_level_tags()
  5. class Message(StrAndUnicode):
  6. """
  7. Represents an actual message that can be stored in any of the supported
  8. storage classes (typically session- or cookie-based) and rendered in a view
  9. or template.
  10. """
  11. def __init__(self, level, message, extra_tags=None):
  12. self.level = int(level)
  13. self.message = message
  14. self.extra_tags = extra_tags
  15. def _prepare(self):
  16. """
  17. Prepares the message for serialization by forcing the ``message``
  18. and ``extra_tags`` to unicode in case they are lazy translations.
  19. Known "safe" types (None, int, etc.) are not converted (see Django's
  20. ``force_unicode`` implementation for details).
  21. """
  22. self.message = force_unicode(self.message, strings_only=True)
  23. self.extra_tags = force_unicode(self.extra_tags, strings_only=True)
  24. def __eq__(self, other):
  25. return isinstance(other, Message) and self.level == other.level and \
  26. self.message == other.message
  27. def __unicode__(self):
  28. return force_unicode(self.message)
  29. def _get_tags(self):
  30. label_tag = force_unicode(LEVEL_TAGS.get(self.level, ''),
  31. strings_only=True)
  32. extra_tags = force_unicode(self.extra_tags, strings_only=True)
  33. if extra_tags and label_tag:
  34. return u' '.join([extra_tags, label_tag])
  35. elif extra_tags:
  36. return extra_tags
  37. elif label_tag:
  38. return label_tag
  39. return ''
  40. tags = property(_get_tags)
  41. class BaseStorage(object):
  42. """
  43. This is the base backend for temporary message storage.
  44. This is not a complete class; to be a usable storage backend, it must be
  45. subclassed and the two methods ``_get`` and ``_store`` overridden.
  46. """
  47. def __init__(self, request, *args, **kwargs):
  48. self.request = request
  49. self._queued_messages = []
  50. self.used = False
  51. self.added_new = False
  52. super(BaseStorage, self).__init__(*args, **kwargs)
  53. def __len__(self):
  54. return len(self._loaded_messages) + len(self._queued_messages)
  55. def __iter__(self):
  56. self.used = True
  57. if self._queued_messages:
  58. self._loaded_messages.extend(self._queued_messages)
  59. self._queued_messages = []
  60. return iter(self._loaded_messages)
  61. def __contains__(self, item):
  62. return item in self._loaded_messages or item in self._queued_messages
  63. @property
  64. def _loaded_messages(self):
  65. """
  66. Returns a list of loaded messages, retrieving them first if they have
  67. not been loaded yet.
  68. """
  69. if not hasattr(self, '_loaded_data'):
  70. messages, all_retrieved = self._get()
  71. self._loaded_data = messages or []
  72. return self._loaded_data
  73. def _get(self, *args, **kwargs):
  74. """
  75. Retrieves a list of stored messages. Returns a tuple of the messages
  76. and a flag indicating whether or not all the messages originally
  77. intended to be stored in this storage were, in fact, stored and
  78. retrieved; e.g., ``(messages, all_retrieved)``.
  79. **This method must be implemented by a subclass.**
  80. If it is possible to tell if the backend was not used (as opposed to
  81. just containing no messages) then ``None`` should be returned in
  82. place of ``messages``.
  83. """
  84. raise NotImplementedError()
  85. def _store(self, messages, response, *args, **kwargs):
  86. """
  87. Stores a list of messages, returning a list of any messages which could
  88. not be stored.
  89. One type of object must be able to be stored, ``Message``.
  90. **This method must be implemented by a subclass.**
  91. """
  92. raise NotImplementedError()
  93. def _prepare_messages(self, messages):
  94. """
  95. Prepares a list of messages for storage.
  96. """
  97. for message in messages:
  98. message._prepare()
  99. def update(self, response):
  100. """
  101. Stores all unread messages.
  102. If the backend has yet to be iterated, previously stored messages will
  103. be stored again. Otherwise, only messages added after the last
  104. iteration will be stored.
  105. """
  106. self._prepare_messages(self._queued_messages)
  107. if self.used:
  108. return self._store(self._queued_messages, response)
  109. elif self.added_new:
  110. messages = self._loaded_messages + self._queued_messages
  111. return self._store(messages, response)
  112. def add(self, level, message, extra_tags=''):
  113. """
  114. Queues a message to be stored.
  115. The message is only queued if it contained something and its level is
  116. not less than the recording level (``self.level``).
  117. """
  118. if not message:
  119. return
  120. # Check that the message level is not less than the recording level.
  121. level = int(level)
  122. if level < self.level:
  123. return
  124. # Add the message.
  125. self.added_new = True
  126. message = Message(level, message, extra_tags=extra_tags)
  127. self._queued_messages.append(message)
  128. def _get_level(self):
  129. """
  130. Returns the minimum recorded level.
  131. The default level is the ``MESSAGE_LEVEL`` setting. If this is
  132. not found, the ``INFO`` level is used.
  133. """
  134. if not hasattr(self, '_level'):
  135. self._level = getattr(settings, 'MESSAGE_LEVEL', constants.INFO)
  136. return self._level
  137. def _set_level(self, value=None):
  138. """
  139. Sets a custom minimum recorded level.
  140. If set to ``None``, the default level will be used (see the
  141. ``_get_level`` method).
  142. """
  143. if value is None and hasattr(self, '_level'):
  144. del self._level
  145. else:
  146. self._level = int(value)
  147. level = property(_get_level, _set_level, _set_level)