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