consumers.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. from asgiref.sync import async_to_sync
  2. from django.core import serializers
  3. from channels.generic.websocket import WebsocketConsumer, AsyncWebsocketConsumer, AsyncConsumer
  4. import json
  5. # TODO: REMOVE REDUNDANT CONSUMERS, UPDATE WEBSOCKETS ON FRONT END
  6. class IdeaConsumer(AsyncWebsocketConsumer):
  7. '''
  8. Consumer for websockets which are for modification of the Idea model
  9. '''
  10. async def connect(self):
  11. self.room_name = self.scope['url_route']['kwargs']['pk'] + "_idea"
  12. self.room_group_name = 'canvas_%s' %self.room_name
  13. print(self.room_group_name)
  14. await self.channel_layer.group_add(
  15. self.room_group_name,
  16. self.channel_name
  17. )
  18. await self.accept()
  19. async def disconnect(self, close_code):
  20. await self.channel_layer.group_discard(
  21. self.room_group_name,
  22. self.channel_name
  23. )
  24. async def receive(self, text_data):
  25. text_data_json = json.loads(text_data)
  26. data = text_data_json['data']
  27. await self.channel_layer.group_send(
  28. self.room_group_name,
  29. {
  30. 'type': 'channel_message',
  31. 'data': data
  32. }
  33. )
  34. async def channel_message(self, event):
  35. data = event['data']
  36. await self.send(text_data=json.dumps({
  37. 'data': data
  38. }))
  39. class CommentConsumer(AsyncWebsocketConsumer):
  40. '''
  41. Consumer for websockets which are for modification of the Comment model
  42. '''
  43. async def connect(self):
  44. self.room_name = self.scope['url_route']['kwargs']['pk'] + "_comment"
  45. self.room_group_name = 'canvas_%s' %self.room_name
  46. await self.channel_layer.group_add(
  47. self.room_group_name,
  48. self.channel_name
  49. )
  50. await self.accept()
  51. async def disconnect(self, close_code):
  52. await self.channel_layer.group_discard(
  53. self.room_group_name,
  54. self.channel_name
  55. )
  56. async def receive(self, text_data):
  57. canvas_pk = self.scope['url_route']['kwargs']['pk']
  58. text_data_json = json.loads(text_data)
  59. data = text_data_json['data']
  60. await self.channel_layer.group_send(
  61. self.room_group_name,
  62. {
  63. 'type': 'channel_message',
  64. 'data': data,
  65. }
  66. )
  67. async def channel_message(self, event):
  68. data = event['data']
  69. await self.send(text_data=json.dumps({
  70. 'data': data
  71. }))
  72. class CollabConsumer(AsyncWebsocketConsumer):
  73. '''
  74. Consumer for websockets which are for modification of the User model
  75. '''
  76. async def connect(self):
  77. self.room_name = self.scope['url_route']['kwargs']['pk'] + "_collab"
  78. self.room_group_name = 'project_%s' %self.room_name
  79. await self.channel_layer.group_add(
  80. self.room_group_name,
  81. self.channel_name
  82. )
  83. await self.accept()
  84. async def disconnect(self, close_code):
  85. await self.channel_layer.group_discard(
  86. self.room_group_name,
  87. self.channel_name
  88. )
  89. async def receive(self, text_data):
  90. project_pk = self.scope['url_route']['kwargs']['pk']
  91. text_data_json = json.loads(text_data)
  92. data = text_data_json['data']
  93. await self.channel_layer.group_send(
  94. self.room_group_name,
  95. {
  96. 'type': 'channel_message',
  97. 'data': data,
  98. }
  99. )
  100. async def channel_message(self, event):
  101. data = event['data']
  102. await self.send(text_data=json.dumps({
  103. 'data': data
  104. }))
  105. class TagConsumer(AsyncWebsocketConsumer):
  106. '''
  107. Consumer for websockets which are for modification of the Tag model
  108. '''
  109. async def connect(self):
  110. self.room_name = self.scope['url_route']['kwargs']['pk'] + "_tag"
  111. self.room_group_name = 'project_%s' %self.room_name
  112. await self.channel_layer.group_add(
  113. self.room_group_name,
  114. self.channel_name
  115. )
  116. await self.accept()
  117. async def disconnect(self, close_code):
  118. await self.channel_layer.group_discard(
  119. self.room_group_name,
  120. self.channel_name
  121. )
  122. async def receive(self, text_data):
  123. project_pk = self.scope['url_route']['kwargs']['pk']
  124. text_data_json = json.loads(text_data)
  125. data = text_data_json['data']
  126. await self.channel_layer.group_send(
  127. self.room_group_name,
  128. {
  129. 'type': 'channel_message',
  130. 'data': data,
  131. }
  132. )
  133. async def channel_message(self, event):
  134. data = event['data']
  135. await self.send(text_data=json.dumps({
  136. 'data': data
  137. }))