backend/pitch_deck_generator/utils/channels.py

25 lines
722 B
Python
Raw Normal View History

2023-08-26 01:17:47 +03:00
import functools
from channels.generic.websocket import AsyncJsonWebsocketConsumer, JsonWebsocketConsumer
def login_required(func):
@functools.wraps(func)
def wrapper(self, *args, **kwargs):
if not self.scope.get("user", False) or not self.scope["user"].is_authenticated:
self.send_error("Login is required")
else:
return func(self, *args, **kwargs)
return wrapper
class BaseConsumer(AsyncJsonWebsocketConsumer):
async def send_error(self, msg):
await self.send_json({"type": "error", "data": {"msg": msg}})
class SyncBaseConsumer(JsonWebsocketConsumer):
def send_error(self, msg):
self.send_json({"type": "error", "data": {"msg": msg}})