From 3aa928e8f0b24bd99c36359b9ea7e60665165c60 Mon Sep 17 00:00:00 2001 From: Tom Hubrecht Date: Wed, 29 Jun 2022 11:10:37 +0200 Subject: [PATCH] Simplify group_send for kpsul --- kfet/consumers.py | 9 +++++++++ kfet/views.py | 13 ++++--------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/kfet/consumers.py b/kfet/consumers.py index e8334421..1765836c 100644 --- a/kfet/consumers.py +++ b/kfet/consumers.py @@ -1,3 +1,6 @@ +from asgiref.sync import async_to_sync +from channels.layers import get_channel_layer + from .utils import DjangoJsonWebsocketConsumer, PermConsumerMixin @@ -7,3 +10,9 @@ class KPsul(PermConsumerMixin, DjangoJsonWebsocketConsumer): async def kpsul(self, event): await self.send_json(event) + + @classmethod + @async_to_sync + async def group_send(cls, group, data): + channel_layer = get_channel_layer() + await channel_layer.group_send(group, data) diff --git a/kfet/views.py b/kfet/views.py index d60a3a07..83d1a9e2 100644 --- a/kfet/views.py +++ b/kfet/views.py @@ -6,8 +6,6 @@ from decimal import Decimal from typing import List, Tuple from urllib.parse import urlencode -from asgiref.sync import async_to_sync -from channels.layers import get_channel_layer from django.conf import settings from django.contrib import messages from django.contrib.auth.decorators import login_required, permission_required @@ -50,6 +48,7 @@ from kfet import KFET_DELETED_TRIGRAMME from kfet.auth.decorators import kfet_password_auth from kfet.autocomplete import kfet_account_only_autocomplete, kfet_autocomplete from kfet.config import kfet_config +from kfet.consumers import KPsul from kfet.decorators import teamkfet_required from kfet.forms import ( AccountForm, @@ -1061,9 +1060,8 @@ def kpsul_update_addcost(request): "type": "kpsul", } - channel_layer = get_channel_layer() + KPsul.group_send("kfet.kpsul", data) - async_to_sync(channel_layer.group_send)("kfet.kpsul", data) return JsonResponse(data) @@ -1295,9 +1293,8 @@ def kpsul_perform_operations(request): {"id": article["id"], "stock": article["stock"]} ) - channel_layer = get_channel_layer() + KPsul.group_send("kfet.kpsul", websocket_data) - async_to_sync(channel_layer.group_send)("kfet.kpsul", websocket_data) return JsonResponse(data) @@ -1502,9 +1499,7 @@ def cancel_operations(request): {"id": article["id"], "stock": article["stock"]} ) - channel_layer = get_channel_layer() - - async_to_sync(channel_layer.group_send)("kfet.kpsul", websocket_data) + KPsul.group_send("kfet.kpsul", websocket_data) data["canceled"] = list(opes) data["opegroups_to_update"] = list(opegroups)