[Fixed]-Django refresh session to obtain any new messages for user

1๐Ÿ‘

โœ…

I have managed to come to an acceptable solution with a couple of caveats.
Using existing message stores (either cookie or session) proved to be either impossible (cookies) or much too ridden with internal method calls & setting / deleting internal members (session).

This solution uses a new message store, in my case a database one.

settings.py

MESSAGE_STORAGE = 'your_django_app.messages_store.SessionDBStorage'

your_django_app/messages_store.py

from django.contrib.messages.storage.session import SessionStorage
from main.models import MessagesStore, models


class SessionDBStorage(SessionStorage):
    """
    Stores messages in the database based on session id
    """

    def _get(self, *args, **kwargs):
        """
        Retrieves a list of messages from the database based on session identifier.
        """
        try:
            return self.deserialize_messages(
                MessagesStore.objects.get(pk=self.request.session.session_key).messages), True
        except models.ObjectDoesNotExist:
            return [], True

    def _store(self, messages, response, *args, **kwargs):
        """
        Stores a list of messages to the database.
        """
        if messages:
            MessagesStore.objects.update_or_create(session_key=self.request.session.session_key,
                                                   defaults={'messages': self.serialize_messages(messages)})
        else:
            MessagesStore.objects.filter(pk=self.request.session.session_key).delete()
        return []

your_django_app/rest.py

def pushed_messages():
    from time import sleep, monotonic
    # standard get messages code
    ....
    now = monotonic()
    while not res.messages and monotonic() - now < 15:
        sleep(1)
        if hasattr(storage, '_loaded_data'):  # one last hack to make storage reload messages
            delattr(storage, '_loaded_data')
        res.messages = [dict(level=item.level, message=item.message) for item in storage]

Please note that internally this is still a polling solution (the loop constantly reloads the messages), but it proves the concept and ultimately โ€“ works. Any underlying storage / signal mechanism optimisations are beyond the scope of this answer.

๐Ÿ‘คvelis

Leave a comment