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
A prog2-höz tartozó friss repo anyagok itt elérhetőek:
https://git.iit.bme.hu/
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
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
3 deletions
+17
-3
recircle/status/consumers.py
+17
-3
No files found.
recircle/status/consumers.py
View file @
fbc6f0f5
...
@@ -2,6 +2,7 @@
...
@@ -2,6 +2,7 @@
from
asgiref.sync
import
async_to_sync
from
asgiref.sync
import
async_to_sync
from
channels.generic.websocket
import
WebsocketConsumer
from
channels.generic.websocket
import
WebsocketConsumer
from
instance.models
import
Instance
from
instance.models
import
Instance
from
rest_framework.authtoken.models
import
Token
import
logging
import
logging
import
json
import
json
...
@@ -9,17 +10,30 @@ logger = logging.getLogger(__name__)
...
@@ -9,17 +10,30 @@ logger = logging.getLogger(__name__)
class
StatusConsumer
(
WebsocketConsumer
):
class
StatusConsumer
(
WebsocketConsumer
):
# Accept connection and wait for login message
def
connect
(
self
):
def
connect
(
self
):
instances
=
Instance
.
objects
.
all
()
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
:
for
inst
in
instances
:
async_to_sync
(
self
.
channel_layer
.
group_add
)(
async_to_sync
(
self
.
channel_layer
.
group_add
)(
str
(
inst
.
id
),
str
(
inst
.
id
),
self
.
channel_name
self
.
channel_name
)
)
self
.
accept
()
else
:
self
.
disconnect
(
100
)
# Unsubscribe from registered groups
def
disconnect
(
self
,
close_code
):
def
disconnect
(
self
,
close_code
):
instances
=
Instance
.
objects
.
all
()
if
self
.
user
:
instances
=
Instance
.
objects
.
filter
(
created_by
=
self
.
user
)
for
inst
in
instances
:
for
inst
in
instances
:
async_to_sync
(
self
.
channel_layer
.
group_discard
)(
async_to_sync
(
self
.
channel_layer
.
group_discard
)(
str
(
inst
.
id
),
str
(
inst
.
id
),
...
...
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