Simplify group_send for kpsul

This commit is contained in:
Tom Hubrecht 2022-06-29 11:10:37 +02:00
parent 2b712dd05d
commit 5a28aab7cb
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 from .utils import DjangoJsonWebsocketConsumer, PermConsumerMixin
@ -7,3 +10,9 @@ class KPsul(PermConsumerMixin, DjangoJsonWebsocketConsumer):
async def kpsul(self, event): async def kpsul(self, event):
await self.send_json(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 typing import List, Tuple
from urllib.parse import urlencode 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.conf import settings
from django.contrib import messages from django.contrib import messages
from django.contrib.auth.decorators import login_required, permission_required from django.contrib.auth.decorators import login_required, permission_required
@ -37,6 +35,7 @@ from kfet import KFET_DELETED_TRIGRAMME
from kfet.auth.decorators import kfet_password_auth from kfet.auth.decorators import kfet_password_auth
from kfet.autocomplete import kfet_account_only_autocomplete, kfet_autocomplete from kfet.autocomplete import kfet_account_only_autocomplete, kfet_autocomplete
from kfet.config import kfet_config from kfet.config import kfet_config
from kfet.consumers import KPsul
from kfet.decorators import teamkfet_required from kfet.decorators import teamkfet_required
from kfet.forms import ( from kfet.forms import (
AccountForm, AccountForm,
@ -991,9 +990,8 @@ def kpsul_update_addcost(request):
"type": "kpsul", "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) return JsonResponse(data)
@ -1225,9 +1223,8 @@ def kpsul_perform_operations(request):
{"id": article["id"], "stock": article["stock"]} {"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) return JsonResponse(data)
@ -1432,9 +1429,7 @@ def cancel_operations(request):
{"id": article["id"], "stock": article["stock"]} {"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)
data["canceled"] = list(opes) data["canceled"] = list(opes)
data["opegroups_to_update"] = list(opegroups) data["opegroups_to_update"] = list(opegroups)