Represent an actual message that can be stored in any of the supported storage classes (typically session- or cookie-based) and rendered in a view or template.
| 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: |
no outgoing calls