From 14a7c71142cc8df8c930c79bad308ec8b8486ba6 Mon Sep 17 00:00:00 2001 From: delcroip Date: Fri, 8 Nov 2024 12:28:05 +0100 Subject: [PATCH 1/4] wait mutation --- core/schema.py | 10 +++++++++- core/services/__init__.py | 5 +++-- core/services/base.py | 13 ++++++++++++- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/core/schema.py b/core/schema.py index fe96f46..47cd525 100644 --- a/core/schema.py +++ b/core/schema.py @@ -28,7 +28,8 @@ change_user_password, reset_user_password, set_user_password, - user_authentication + user_authentication, + wait_for_mutation ) from core.tasks import openimis_mutation_async from core import filter_validity @@ -793,6 +794,7 @@ def resolve_interactive_users(self, info, **kwargs): client_mutation_id = kwargs.get("client_mutation_id", None) if client_mutation_id: + wait_for_mutation(client_mutation_id) filters.append(Q(mutations__mutation__client_mutation_id=client_mutation_id)) show_history = kwargs.get('show_history', False) @@ -848,6 +850,7 @@ def resolve_users(self, info, email=None, last_name=None, other_names=None, phon client_mutation_id = kwargs.get("client_mutation_id", None) if client_mutation_id: + wait_for_mutation(client_mutation_id) user_filters.append(Q(mutations__mutation__client_mutation_id=client_mutation_id)) if email: @@ -936,7 +939,9 @@ def resolve_role(self, info, **kwargs): filters.append(Q(name__icontains=text_search)) client_mutation_id = kwargs.get("client_mutation_id", None) + if client_mutation_id: + wait_for_mutation(client_mutation_id) filters.append(Q(mutations__mutation__client_mutation_id=client_mutation_id)) show_history = kwargs.get('show_history', False) @@ -1150,6 +1155,7 @@ def update_or_create_role(data, user): } ) for right_id in rights_id] if client_mutation_id: + wait_for_mutation(client_mutation_id) RoleMutation.object_mutated(user, role=role, client_mutation_id=client_mutation_id) return role return role @@ -1206,6 +1212,7 @@ def duplicate_role(data, user): ) for role_right in role_rights_currently_assigned] if client_mutation_id: + wait_for_mutation(client_mutation_id) RoleMutation.object_mutated(user, role=duplicated_role, client_mutation_id=client_mutation_id) return duplicated_role @@ -1581,6 +1588,7 @@ def update_or_create_user(data, user): user_uuid=user_uuid, username=username, i_user=i_user, officer=officer, claim_admin=claim_admin) if client_mutation_id: + wait_for_mutation(client_mutation_id) UserMutation.object_mutated(user, core_user=core_user, client_mutation_id=client_mutation_id) return core_user diff --git a/core/services/__init__.py b/core/services/__init__.py index 2c85290..f263e1d 100644 --- a/core/services/__init__.py +++ b/core/services/__init__.py @@ -1,5 +1,6 @@ -from core.services.base import BaseService +from core.services.base import BaseService, wait_for_mutation from core.services.userServices import create_or_update_interactive_user, create_or_update_user_roles, \ create_or_update_user_districts, create_or_update_officer_villages, create_or_update_officer, \ create_or_update_claim_admin, create_or_update_core_user, change_user_password, set_user_password, \ - reset_user_password, user_authentication + reset_user_password, user_authentication + diff --git a/core/services/base.py b/core/services/base.py index 1659807..4181622 100644 --- a/core/services/base.py +++ b/core/services/base.py @@ -3,7 +3,7 @@ from django.db import transaction -from core.models import HistoryModel +from core.models import HistoryModel, MutationLog from core.services.utils import check_authentication as check_authentication, output_exception, \ model_representation, output_result_success, build_delete_instance_payload from core.validation.base import BaseModelValidation @@ -72,3 +72,14 @@ def _adjust_update_payload(self, payload_data): def _base_payload_adjust(self, obj_data): return obj_data + + +def wait_for_mutation(client_mutation_id): + mutation = MutationLog(client_mutation_id=client_mutation_id) + if not mutation: + return + loop_count = 0 + while mutation.status == MutationLog.RECEIVED and loop_count>10: + asyncio.sleep(0.3) + loop_count += 1 + return \ No newline at end of file From 8267a27f3957fc28b7761b5b5a539995053653be Mon Sep 17 00:00:00 2001 From: delcroip Date: Fri, 8 Nov 2024 13:05:09 +0100 Subject: [PATCH 2/4] import asyncio --- core/services/base.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/core/services/base.py b/core/services/base.py index 4181622..4fe3e19 100644 --- a/core/services/base.py +++ b/core/services/base.py @@ -1,6 +1,6 @@ from abc import ABC from typing import Type - +import asyncio from django.db import transaction from core.models import HistoryModel, MutationLog @@ -53,10 +53,9 @@ def delete(self, obj_data): obj_ = self.OBJECT_TYPE.objects.filter(id=obj_data['id']).first() return self.delete_instance(obj_) except Exception as exc: - return output_exception(model_name=self.OBJECT_TYPE.__name__, method="delete", exception=exc) - + return outasyncio def save_instance(self, obj_): - obj_.save(user=self.user, username=self.user.username) + obj_.save(user=self.user, usernameasyncio=self.user.username) dict_repr = model_representation(obj_) return output_result_success(dict_representation=dict_repr) From f8359a2d38ea45d7b3dfada2e48a7a84be3a05b7 Mon Sep 17 00:00:00 2001 From: delcroip Date: Fri, 8 Nov 2024 13:48:16 +0100 Subject: [PATCH 3/4] fix w cp --- core/services/base.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/core/services/base.py b/core/services/base.py index 4fe3e19..0dd426c 100644 --- a/core/services/base.py +++ b/core/services/base.py @@ -53,9 +53,10 @@ def delete(self, obj_data): obj_ = self.OBJECT_TYPE.objects.filter(id=obj_data['id']).first() return self.delete_instance(obj_) except Exception as exc: - return outasyncio + return output_exception(model_name=self.OBJECT_TYPE.__name__, method="delete", exception=exc) + def save_instance(self, obj_): - obj_.save(user=self.user, usernameasyncio=self.user.username) + obj_.save(user=self.user, username=self.user.username) dict_repr = model_representation(obj_) return output_result_success(dict_representation=dict_repr) @@ -80,5 +81,5 @@ def wait_for_mutation(client_mutation_id): loop_count = 0 while mutation.status == MutationLog.RECEIVED and loop_count>10: asyncio.sleep(0.3) - loop_count += 1 + loop_count+= 1 return \ No newline at end of file From 3ab32bd1b74d1eebf5abc5ee5c8438484d52caa5 Mon Sep 17 00:00:00 2001 From: delcroip Date: Fri, 8 Nov 2024 14:23:04 +0100 Subject: [PATCH 4/4] security --- core/services/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/services/base.py b/core/services/base.py index 0dd426c..8886216 100644 --- a/core/services/base.py +++ b/core/services/base.py @@ -79,7 +79,7 @@ def wait_for_mutation(client_mutation_id): if not mutation: return loop_count = 0 - while mutation.status == MutationLog.RECEIVED and loop_count>10: + while mutation.status == MutationLog.RECEIVED and loop_count<10: asyncio.sleep(0.3) loop_count+= 1 return \ No newline at end of file