2016-09-01 00:45:44 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2017-04-09 15:47:16 +02:00
|
|
|
from django.core.serializers.json import json, DjangoJSONEncoder
|
2016-09-01 00:45:44 +02:00
|
|
|
|
2016-08-14 19:59:36 +02:00
|
|
|
from channels.generic.websockets import JsonWebsocketConsumer
|
|
|
|
|
|
|
|
|
2017-04-09 15:47:16 +02:00
|
|
|
class DjangoJsonWebsocketConsumer(JsonWebsocketConsumer):
|
|
|
|
"""Custom Json Websocket Consumer.
|
2016-08-14 19:59:36 +02:00
|
|
|
|
2017-04-09 15:47:16 +02:00
|
|
|
Encode to JSON with DjangoJSONEncoder.
|
2016-08-14 19:59:36 +02:00
|
|
|
|
2017-04-09 15:47:16 +02:00
|
|
|
"""
|
2016-08-14 19:59:36 +02:00
|
|
|
|
2017-04-09 15:47:16 +02:00
|
|
|
@classmethod
|
|
|
|
def encode_json(cls, content):
|
|
|
|
return json.dumps(content, cls=DjangoJSONEncoder)
|
2016-08-14 19:59:36 +02:00
|
|
|
|
2017-04-09 15:47:16 +02:00
|
|
|
|
2017-04-09 16:10:27 +02:00
|
|
|
class PermConsumerMixin(object):
|
|
|
|
"""Add support to check permissions on Consumers.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
perms_connect (list): Required permissions to connect to this
|
|
|
|
consumer.
|
|
|
|
|
|
|
|
"""
|
|
|
|
http_user = True # Enable message.user
|
|
|
|
perms_connect = []
|
|
|
|
|
|
|
|
def connect(self, message, **kwargs):
|
|
|
|
"""Check permissions on connection."""
|
|
|
|
if message.user.has_perms(self.perms_connect):
|
|
|
|
super().connect(message, **kwargs)
|
|
|
|
else:
|
|
|
|
self.close()
|
|
|
|
|
|
|
|
|
|
|
|
class KPsul(PermConsumerMixin, DjangoJsonWebsocketConsumer):
|
2017-04-09 15:47:16 +02:00
|
|
|
groups = ['kfet.kpsul']
|
2017-04-09 16:10:27 +02:00
|
|
|
perms_connect = ['kfet.is_team']
|