Simplify group_send for kpsul

This commit is contained in:
Tom Hubrecht 2022-06-29 11:10:37 +02:00 committed by thubrecht
parent 5e2e68960b
commit 3aa928e8f0
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
@ -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)