root: fix messages showing for all sessions of a user
This commit is contained in:
parent
736dbdca33
commit
4b1e73251a
|
@ -7,13 +7,16 @@ class MessageConsumer(JsonWebsocketConsumer):
|
||||||
"""Consumer which sends django.contrib.messages Messages over WS.
|
"""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"""
|
channel_name is saved into cache with user_id, and when a add_message is called"""
|
||||||
|
|
||||||
|
session_key: str
|
||||||
|
|
||||||
def connect(self):
|
def connect(self):
|
||||||
self.accept()
|
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
|
# pylint: disable=unused-argument
|
||||||
def disconnect(self, close_code):
|
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):
|
def event_update(self, event: dict):
|
||||||
"""Event handler which is called by Messages Storage backend"""
|
"""Event handler which is called by Messages Storage backend"""
|
||||||
|
|
|
@ -16,7 +16,7 @@ class ChannelsStorage(FallbackStorage):
|
||||||
self.channel = get_channel_layer()
|
self.channel = get_channel_layer()
|
||||||
|
|
||||||
def _store(self, messages: list[Message], response, *args, **kwargs):
|
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}*")
|
keys = cache.keys(f"{prefix}*")
|
||||||
if len(keys) < 1:
|
if len(keys) < 1:
|
||||||
return super()._store(messages, response, *args, **kwargs)
|
return super()._store(messages, response, *args, **kwargs)
|
||||||
|
|
Reference in a new issue