Checking if self.groups is None in AsyncWebsocketConsumer causes ValueError #1647
Unanswered
utsdanielosborne
asked this question in
Q&A
Replies: 2 comments
-
Hello @utsdanielosborne class Test (AsyncWebsocketConsumer):
async def connect(self):
# reject anonymous connections if the
# scope's user shouldn't be anonymous
await self.accept()
self.groups = [a_fn(self.scope['user']] This way, you're guaranteed that the request's user would be set. |
Beta Was this translation helpful? Give feedback.
0 replies
-
As far as I know, the groups attribute is only for setting "predefined" group names, which don't reference any attributes of the scope itself. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
With reference to:
channels/channels/generic/websocket.py
Line 160 in ece488b
The check of self.groups during _init_, to change it from None to an empty list 'if not set', causes a ValueError to be thrown by:
channels/channels/auth.py
Line 146 in ece488b
if self.groups is a property which tries to access scope['user'].
i.e.
Because scope['user'] is only set during _call_
I've had to work around this by catching ValueError in groups for when it's called from _init_
It seems to me that the _init_ check is only intended to ensure that self.groups returns a list, but does not need to access that list itself as it currently does.
Therefore this problem could be avoided perhaps by e.g defining a default groups property on AsyncWebsocketConsumer that can be overridden, and removing the two lines from _init_.
i.e.
Beta Was this translation helpful? Give feedback.
All reactions