56 lines
1.3 KiB
Python
56 lines
1.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
from django.core.serializers.json import json, DjangoJSONEncoder
|
|
|
|
from channels.generic.websockets import JsonWebsocketConsumer
|
|
|
|
|
|
class DjangoJsonWebsocketConsumer(JsonWebsocketConsumer):
|
|
"""Custom Json Websocket Consumer.
|
|
|
|
Encode to JSON with DjangoJSONEncoder.
|
|
|
|
"""
|
|
|
|
@classmethod
|
|
def encode_json(cls, content):
|
|
return json.dumps(content, cls=DjangoJSONEncoder)
|
|
|
|
|
|
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 KfetOpen(JsonWebsocketConsumer):
|
|
def connection_groups(self, **kwargs):
|
|
return ['kfet.is_open']
|
|
|
|
def connect(self, message, **kwargs):
|
|
pass
|
|
|
|
def receive(self, content, **kwargs):
|
|
pass
|
|
|
|
def disconnect(self, message, **kwargs):
|
|
pass
|
|
|
|
|
|
class KPsul(PermConsumerMixin, DjangoJsonWebsocketConsumer):
|
|
groups = ['kfet.kpsul']
|
|
perms_connect = ['kfet.is_team']
|