Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

base.py 5.5KB

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