123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- from asgiref.sync import async_to_sync
- from django.core import serializers
- from channels.generic.websocket import WebsocketConsumer, AsyncWebsocketConsumer, AsyncConsumer
- import json
- # TODO: REMOVE REDUNDANT CONSUMERS, UPDATE WEBSOCKETS ON FRONT END
- class IdeaConsumer(AsyncWebsocketConsumer):
- '''
- Consumer for websockets which are for modification of the Idea model
- '''
- async def connect(self):
- self.room_name = self.scope['url_route']['kwargs']['pk'] + "_idea"
- self.room_group_name = 'canvas_%s' %self.room_name
- print(self.room_group_name)
- await self.channel_layer.group_add(
- self.room_group_name,
- self.channel_name
- )
- await self.accept()
-
- async def disconnect(self, close_code):
- await self.channel_layer.group_discard(
- self.room_group_name,
- self.channel_name
- )
- async def receive(self, text_data):
- text_data_json = json.loads(text_data)
- data = text_data_json['data']
-
- await self.channel_layer.group_send(
- self.room_group_name,
- {
- 'type': 'channel_message',
- 'data': data
- }
- )
- async def channel_message(self, event):
- data = event['data']
-
- await self.send(text_data=json.dumps({
- 'data': data
- }))
- class CommentConsumer(AsyncWebsocketConsumer):
- '''
- Consumer for websockets which are for modification of the Comment model
- '''
- async def connect(self):
- self.room_name = self.scope['url_route']['kwargs']['pk'] + "_comment"
- self.room_group_name = 'canvas_%s' %self.room_name
- await self.channel_layer.group_add(
- self.room_group_name,
- self.channel_name
- )
- await self.accept()
- async def disconnect(self, close_code):
- await self.channel_layer.group_discard(
- self.room_group_name,
- self.channel_name
- )
- async def receive(self, text_data):
- canvas_pk = self.scope['url_route']['kwargs']['pk']
-
- text_data_json = json.loads(text_data)
- data = text_data_json['data']
- await self.channel_layer.group_send(
- self.room_group_name,
- {
- 'type': 'channel_message',
- 'data': data,
- }
- )
- async def channel_message(self, event):
- data = event['data']
-
- await self.send(text_data=json.dumps({
- 'data': data
- }))
- class CollabConsumer(AsyncWebsocketConsumer):
- '''
- Consumer for websockets which are for modification of the User model
- '''
- async def connect(self):
- self.room_name = self.scope['url_route']['kwargs']['pk'] + "_collab"
- self.room_group_name = 'project_%s' %self.room_name
- await self.channel_layer.group_add(
- self.room_group_name,
- self.channel_name
- )
- await self.accept()
-
- async def disconnect(self, close_code):
- await self.channel_layer.group_discard(
- self.room_group_name,
- self.channel_name
- )
- async def receive(self, text_data):
- project_pk = self.scope['url_route']['kwargs']['pk']
-
- text_data_json = json.loads(text_data)
- data = text_data_json['data']
- await self.channel_layer.group_send(
- self.room_group_name,
- {
- 'type': 'channel_message',
- 'data': data,
- }
- )
- async def channel_message(self, event):
- data = event['data']
-
- await self.send(text_data=json.dumps({
- 'data': data
- }))
- class TagConsumer(AsyncWebsocketConsumer):
- '''
- Consumer for websockets which are for modification of the Tag model
- '''
- async def connect(self):
- self.room_name = self.scope['url_route']['kwargs']['pk'] + "_tag"
- self.room_group_name = 'project_%s' %self.room_name
- await self.channel_layer.group_add(
- self.room_group_name,
- self.channel_name
- )
- await self.accept()
-
- async def disconnect(self, close_code):
- await self.channel_layer.group_discard(
- self.room_group_name,
- self.channel_name
- )
- async def receive(self, text_data):
- project_pk = self.scope['url_route']['kwargs']['pk']
- text_data_json = json.loads(text_data)
- data = text_data_json['data']
- await self.channel_layer.group_send(
- self.room_group_name,
- {
- 'type': 'channel_message',
- 'data': data,
- }
- )
- async def channel_message(self, event):
- data = event['data']
-
- await self.send(text_data=json.dumps({
- 'data': data
- }))
|