Storage Backends¶
With version 1.0, the concept of Storage Backend was introduced to let developers choose how messages are persisted. Django Stored Messages provides a pool of backends out of the box and developers can extend the app providing their own implementation of a Storage Backend.
STORAGE_BACKEND settings parameter contains a string representing the backend class to use. If not specified, it defaults to the default backend.
Here follows a list of supported backends.
Default backend: Django ORM¶
'STORAGE_BACKEND': 'stored_messages.backends.DefaultBackend'
This is the default backend, it stores messages on the configured database using plain old Django models; it doesn’t need any additional configuration.
Redis backend¶
'STORAGE_BACKEND': 'stored_messages.backends.redis'
Users’ inbox and archives are persisted on a Redis instance. Keys are in the form user:<userid>:notifications user:<userid>:archive and values are lists. This backend needs the REDIS_URL settings to be specified, for example:
STORED_MESSAGES={
'REDIS_URL': 'redis://username:password@localhost:6379/0',
}
Implementing your own backend¶
Custom backends should derive from StoredMessagesBackend class and implement all the methods:
-
class
stored_messages.backends.base.
StoredMessagesBackend
[source]¶ -
archive_list
(user)[source]¶ Retrieve all the messages in user‘s archive.
- Params:
- user: Django User instance
- Return:
- An iterable containing Message instances
-
archive_store
(users, msg_instance)[source]¶ Store a Message instance in the archive for a list of users.
- Params:
- users: a list or iterable containing Django User instances msg_instance: Message instance to persist in archive
- Return:
- None
- Raise:
- MessageTypeNotSupported if msg_instance cannot be managed by current backend
-
can_handle
(msg_instance)[source]¶ Determine if this backend can handle messages of the same type of msg_instance.
- Params:
- msg_instance: Message instance
- Return:
- True if type is correct, False otherwise
-
create_message
(level, msg_text, extra_tags, date=None)[source]¶ Create and return a Message instance. Instance types depend on backends implementation.
- Params:
- level: message level (see django.contrib.messages) msg_text: what you think it is extra_tags: see django.contrib.messages date: a DateTime (optional)
- Return:
- Message instance
-
expired_messages_cleanup
()[source]¶ Remove messages that have been expired.
- Params:
- None
- Return:
- None
-
inbox_delete
(user, msg_id)[source]¶ Remove a Message instance from user‘s inbox.
- Params:
- user: Django User instance msg_id: Message identifier
- Return:
- None
- Raise:
- MessageDoesNotExist if msg_id was not found
-
inbox_get
(user, msg_id)[source]¶ Retrieve a Message instance from user‘s inbox.
- Params:
- user: Django User instance msg_id: Message identifier
- Return:
- A Message instance
- Raise:
- MessageDoesNotExist if msg_id was not found
-
inbox_list
(user)[source]¶ Retrieve all the messages in user‘s Inbox.
- Params:
- user: Django User instance
- Return:
- An iterable containing Message instances
-
inbox_purge
(user)[source]¶ Delete all the messages in user‘s Inbox.
- Params:
- user: Django User instance
- Return:
- None
-
inbox_store
(users, msg_instance)[source]¶ Store a Message instance in the inbox for a list of users.
- Params:
- users: a list or iterable containing Django User instances msg_instance: Message instance to persist in inbox
- Return:
- None
- Raise:
- MessageTypeNotSupported if msg_instance cannot be managed by current backend
-