diff --git a/kfet/open/tests.py b/kfet/open/tests.py index b4481994..4e652cb6 100644 --- a/kfet/open/tests.py +++ b/kfet/open/tests.py @@ -1,5 +1,5 @@ import json -import threading +import random from datetime import timedelta from unittest import mock @@ -18,7 +18,7 @@ class OpenKfetTest(ChannelTestCase): def setUp(self): self.kfet_open = OpenKfet( - cache_prefix="test_kfetopen_%s" % threading.get_ident() + cache_prefix="test_kfetopen_%s" % random.randrange(2 ** 20) ) self.addCleanup(self.kfet_open.clear_cache) @@ -138,7 +138,7 @@ class OpenKfetViewsTest(ChannelTestCase): self.c_a.login(username="admin", password="admin") self.kfet_open = OpenKfet( - cache_prefix="test_kfetopen_%s" % threading.get_ident() + cache_prefix="test_kfetopen_%s" % random.randrange(2 ** 20) ) self.addCleanup(self.kfet_open.clear_cache) @@ -244,7 +244,7 @@ class OpenKfetScenarioTest(ChannelTestCase): self.r_c_ws.force_login(self.r) self.kfet_open = OpenKfet( - cache_prefix="test_kfetopen_%s" % threading.get_ident() + cache_prefix="test_kfetopen_%s" % random.randrange(2 ** 20) ) self.addCleanup(self.kfet_open.clear_cache) diff --git a/kfet/views.py b/kfet/views.py index a2a69930..51bfc1a4 100644 --- a/kfet/views.py +++ b/kfet/views.py @@ -1301,13 +1301,27 @@ def kpsul_cancel_operations(request): stock=F("stock") + to_articles_stocks[article] ) + # Need refresh from db cause we used update on querysets. + # Sort objects by pk to get deterministic responses. + opegroups_pk = [opegroup.pk for opegroup in to_groups_amounts] + opegroups = ( + OperationGroup.objects.values("id", "amount", "is_cof") + .filter(pk__in=opegroups_pk) + .order_by("pk") + ) + opes = sorted(opes) + checkouts_pk = [checkout.pk for checkout in to_checkouts_balances] + checkouts = ( + Checkout.objects.values("id", "balance") + .filter(pk__in=checkouts_pk) + .order_by("pk") + ) + articles_pk = [article.pk for articles in to_articles_stocks] + articles = Article.objects.values("id", "stock").filter(pk__in=articles_pk) + # Websocket data websocket_data = {"opegroups": [], "opes": [], "checkouts": [], "articles": []} - # Need refresh from db cause we used update on querysets - opegroups_pk = [opegroup.pk for opegroup in to_groups_amounts] - opegroups = OperationGroup.objects.values("id", "amount", "is_cof").filter( - pk__in=opegroups_pk - ) + for opegroup in opegroups: websocket_data["opegroups"].append( { @@ -1327,16 +1341,10 @@ def kpsul_cancel_operations(request): "canceled_at": canceled_at, } ) - # Need refresh from db cause we used update on querysets - checkouts_pk = [checkout.pk for checkout in to_checkouts_balances] - checkouts = Checkout.objects.values("id", "balance").filter(pk__in=checkouts_pk) for checkout in checkouts: websocket_data["checkouts"].append( {"id": checkout["id"], "balance": checkout["balance"]} ) - # Need refresh from db cause we used update on querysets - articles_pk = [article.pk for articles in to_articles_stocks] - articles = Article.objects.values("id", "stock").filter(pk__in=articles_pk) for article in articles: websocket_data["articles"].append( {"id": article["id"], "stock": article["stock"]}