From 2e5826418b7b5e9dc4f05d0546bb007d87aed337 Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Tue, 12 Jul 2016 14:26:01 -0700 Subject: [PATCH] Fixed #221: WebSocket class based consumer now has http user support --- channels/generic/websockets.py | 13 +++++++++++++ docs/generics.rst | 4 ++++ 2 files changed, 17 insertions(+) diff --git a/channels/generic/websockets.py b/channels/generic/websockets.py index 096dee1..f07ee25 100644 --- a/channels/generic/websockets.py +++ b/channels/generic/websockets.py @@ -1,6 +1,7 @@ import json from ..channel import Group +from ..auth import channel_session_user_from_http from ..sessions import enforce_ordering from .base import BaseConsumer @@ -18,6 +19,10 @@ class WebsocketConsumer(BaseConsumer): "websocket.disconnect": "raw_disconnect", } + # Turning this on passes the user over from the HTTP session on connect, + # implies channel_session_user + http_user = False + # Set one to True if you want the class to enforce ordering for you slight_ordering = False strict_ordering = False @@ -27,8 +32,16 @@ class WebsocketConsumer(BaseConsumer): Pulls out the path onto an instance variable, and optionally adds the ordering decorator. """ + # HTTP user implies channel session user + if self.http_user: + self.channel_session_user = True + # Get super-handler self.path = message['path'] handler = super(WebsocketConsumer, self).get_handler(message, **kwargs) + # Optionally apply HTTP transfer + if self.http_user: + handler = channel_session_user_from_http(handler) + # Ordering decorators if self.strict_ordering: return enforce_ordering(handler, slight=False) elif self.slight_ordering: diff --git a/docs/generics.rst b/docs/generics.rst index 7edda19..1272f22 100644 --- a/docs/generics.rst +++ b/docs/generics.rst @@ -84,6 +84,10 @@ The basic WebSocket generic consumer is used like this:: class MyConsumer(WebsocketConsumer): + # Set to True to automatically port users from HTTP cookies + # (you don't need channel_session_user, this implies it) + http_user = True + # Set to True if you want them, else leave out strict_ordering = False slight_ordering = False