Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
RECIRCLE
/
portal
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
11
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
fbc6f0f5
authored
Apr 27, 2020
by
Chif Gergő
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Create auth in websocket
parent
74faf9bc
Pipeline
#1090
passed with stage
in 2 minutes 24 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
12 deletions
+26
-12
recircle/status/consumers.py
+26
-12
No files found.
recircle/status/consumers.py
View file @
fbc6f0f5
...
...
@@ -2,6 +2,7 @@
from
asgiref.sync
import
async_to_sync
from
channels.generic.websocket
import
WebsocketConsumer
from
instance.models
import
Instance
from
rest_framework.authtoken.models
import
Token
import
logging
import
json
...
...
@@ -9,22 +10,35 @@ logger = logging.getLogger(__name__)
class
StatusConsumer
(
WebsocketConsumer
):
# Accept connection and wait for login message
def
connect
(
self
):
instances
=
Instance
.
objects
.
all
()
for
inst
in
instances
:
async_to_sync
(
self
.
channel_layer
.
group_add
)(
str
(
inst
.
id
),
self
.
channel_name
)
self
.
accept
()
# If login message arrives get the auth token from it
# Then subscribe to the channels of the vm's that belongs to the user
def
login
(
self
,
event
):
user_token
=
event
[
"auth_token"
]
token
=
Token
.
objects
.
get
(
key
=
user_token
)
if
token
:
self
.
user
=
token
.
user
instances
=
Instance
.
objects
.
filter
(
created_by
=
self
.
user
)
for
inst
in
instances
:
async_to_sync
(
self
.
channel_layer
.
group_add
)(
str
(
inst
.
id
),
self
.
channel_name
)
else
:
self
.
disconnect
(
100
)
# Unsubscribe from registered groups
def
disconnect
(
self
,
close_code
):
instances
=
Instance
.
objects
.
all
()
for
inst
in
instances
:
async_to_sync
(
self
.
channel_layer
.
group_discard
)(
str
(
inst
.
id
),
self
.
channel_name
)
if
self
.
user
:
instances
=
Instance
.
objects
.
filter
(
created_by
=
self
.
user
)
for
inst
in
instances
:
async_to_sync
(
self
.
channel_layer
.
group_discard
)(
str
(
inst
.
id
),
self
.
channel_name
)
def
receive
(
self
,
text_message
):
pass
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment