root: fix messages showing for all sessions of a user

This commit is contained in:
Jens Langhammer 2020-12-15 15:19:15 +01:00
parent 736dbdca33
commit 4b1e73251a
2 changed files with 6 additions and 3 deletions

View File

@ -7,13 +7,16 @@ class MessageConsumer(JsonWebsocketConsumer):
"""Consumer which sends django.contrib.messages Messages over WS.
channel_name is saved into cache with user_id, and when a add_message is called"""
session_key: str
def connect(self):
self.accept()
cache.set(f"user_{self.scope['user'].pk}_messages_{self.channel_name}", True)
self.session_key = self.scope["session"].session_key
cache.set(f"user_{self.session_key}_messages_{self.channel_name}", True, None)
# pylint: disable=unused-argument
def disconnect(self, close_code):
cache.delete(f"user_{self.scope['user'].pk}_messages_{self.channel_name}")
cache.delete(f"user_{self.session_key}_messages_{self.channel_name}")
def event_update(self, event: dict):
"""Event handler which is called by Messages Storage backend"""

View File

@ -16,7 +16,7 @@ class ChannelsStorage(FallbackStorage):
self.channel = get_channel_layer()
def _store(self, messages: list[Message], response, *args, **kwargs):
prefix = f"user_{self.request.user.pk}_messages_"
prefix = f"user_{self.request.session.session_key}_messages_"
keys = cache.keys(f"{prefix}*")
if len(keys) < 1:
return super()._store(messages, response, *args, **kwargs)