Skip to content

consumers

GLOBAL_TASKS_GROUP = 'TASKS_GLOBAL' module-attribute

logger = get_logger() module-attribute

TasksConsumer

Bases: WebsocketConsumer

Source code in tasks/consumers.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class TasksConsumer(WebsocketConsumer):
    def connect(self):
        self.user = self.scope["user"]

        async_to_sync(self.channel_layer.group_add)(
            str(GLOBAL_TASKS_GROUP), self.channel_name
        )
        self.accept()

    def disconnect(self, close_code):
        async_to_sync(self.channel_layer.group_discard)(
            str(GLOBAL_TASKS_GROUP), self.channel_name
        )

    def receive(self, text_data):
        packet = Packet.loads_json(text_data)
        logger.info(f'Received {packet.type} packet')

    def send_packet(self, type: str, data: Any):
        self.send(text_data=Packet(type, data).dumps())

    def task_updated(self, event):
        task: Task = Task.objects.get(task_id=event['message'])
        if task.can_view(self.user):
            self.send_packet('TASK_UPDATED', TaskSerializer(task).data)

connect()

Source code in tasks/consumers.py
17
18
19
20
21
22
23
def connect(self):
    self.user = self.scope["user"]

    async_to_sync(self.channel_layer.group_add)(
        str(GLOBAL_TASKS_GROUP), self.channel_name
    )
    self.accept()

disconnect(close_code)

Source code in tasks/consumers.py
25
26
27
28
def disconnect(self, close_code):
    async_to_sync(self.channel_layer.group_discard)(
        str(GLOBAL_TASKS_GROUP), self.channel_name
    )

receive(text_data)

Source code in tasks/consumers.py
30
31
32
def receive(self, text_data):
    packet = Packet.loads_json(text_data)
    logger.info(f'Received {packet.type} packet')

send_packet(type, data)

Source code in tasks/consumers.py
34
35
def send_packet(self, type: str, data: Any):
    self.send(text_data=Packet(type, data).dumps())

task_updated(event)

Source code in tasks/consumers.py
37
38
39
40
def task_updated(self, event):
    task: Task = Task.objects.get(task_id=event['message'])
    if task.can_view(self.user):
        self.send_packet('TASK_UPDATED', TaskSerializer(task).data)