Simplify group_send for kpsul

This commit is contained in:
Tom Hubrecht 2022-06-29 11:10:37 +02:00
parent 9c06d46d58
commit 279a3c1828
2 changed files with 13 additions and 9 deletions

View file

@ -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)

View file

@ -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
@ -48,6 +46,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,
@ -1001,9 +1000,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)
@ -1235,9 +1233,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)
@ -1442,9 +1439,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)