From 5aa4a473400867085d2a25d0da4c2cbeb894cb43 Mon Sep 17 00:00:00 2001 From: Laurent Date: Wed, 5 Feb 2025 14:56:02 +0100 Subject: [PATCH] Attempt to fix issues with missing data --- sync/signals.py | 47 +++++++++++++++++++++++++++++------------------ sync/views.py | 47 ----------------------------------------------- 2 files changed, 29 insertions(+), 65 deletions(-) diff --git a/sync/signals.py b/sync/signals.py index 1312146..d1411dc 100644 --- a/sync/signals.py +++ b/sync/signals.py @@ -117,25 +117,36 @@ def save_model_log_if_possible(instance, signal, created, device_id): print(f'>>> Model Log could not be created because no linked user could be found: {instance}, {signal}') def save_model_log(users, model_operation, model_name, model_id, store_id, device_id): + + model_log = ModelLog() + model_log.operation = model_operation + model_log.date = timezone.now() + model_log.model_name = model_name + model_log.model_id = model_id + model_log.store_id = store_id + model_log.device_id = device_id + model_log.save() + model_log.users.set(users) + # print(f'ML users = {len(users)}') - existing_log = ModelLog.objects.filter(users__in=users, model_id=model_id, operation=model_operation).first() - if existing_log: - # print(f'update existing log {existing_log.users} ') - existing_log.date = timezone.now() - existing_log.device_id = device_id - # existing_log.operation = model_operation - existing_log.save() - existing_log.users.set(users) - else: - model_log = ModelLog() - model_log.operation = model_operation - model_log.date = timezone.now() - model_log.model_name = model_name - model_log.model_id = model_id - model_log.store_id = store_id - model_log.device_id = device_id - model_log.save() - model_log.users.set(users) + # existing_log = ModelLog.objects.filter(users__in=users, model_id=model_id, operation=model_operation).first() + # if existing_log: + # # print(f'update existing log {existing_log.users} ') + # existing_log.date = timezone.now() + # existing_log.device_id = device_id + # # existing_log.operation = model_operation + # existing_log.save() + # existing_log.users.set(users) + # else: + # model_log = ModelLog() + # model_log.operation = model_operation + # model_log.date = timezone.now() + # model_log.model_name = model_name + # model_log.model_id = model_id + # model_log.store_id = store_id + # model_log.device_id = device_id + # model_log.save() + # model_log.users.set(users) def detect_foreign_key_changes(sender, instance, device_id): if not hasattr(instance, 'pk') or not instance.pk: diff --git a/sync/views.py b/sync/views.py index 347f8ad..22d2981 100644 --- a/sync/views.py +++ b/sync/views.py @@ -313,53 +313,6 @@ class UserDataAccessApi(HierarchyApiView): 'message': str(e) }, status=status.HTTP_500_INTERNAL_SERVER_ERROR) - - # # Get all GRANT_ACCESS and REVOKE_ACCESS logs for the user, ordered by date - # all_logs = ModelLog.objects.filter( - # Q(users=request.user) & - # (Q(operation='GRANT_ACCESS') | Q(operation='REVOKE_ACCESS')) - # ).order_by('date') - - # # Track latest status for each (model_name, model_id) - # active_grants = {} - - # # Process logs chronologically to determine current access state - # for log in all_logs: - # if log.operation == 'GRANT_ACCESS': - # active_grants[log.model_id] = log - # elif log.operation == 'REVOKE_ACCESS': - # if log.model_id in active_grants and active_grants[log.model_id].date < log.date: - # del active_grants[log.model_id] - - # # Convert active_grants dict to list of grant logs - # active_grants = list(active_grants.values()) - - # def _add_children_recursively(self, instance, data_dict): - # """ - # Recursively add all children of an instance to the data dictionary. - # """ - # child_models = instance.get_children_by_model() - - # for child_model_name, children in child_models.items(): - # for child in children: - # if isinstance(child, BaseModel): - # serializer = get_serializer(child, child_model_name) - # data_dict[child_model_name][child.id] = serializer.data - # self._add_children_recursively(child, data_dict) - - # def _add_parents_recursively(self, instance, data_dict): - # """ - # Recursively add all parents of an instance to the data dictionary. - # """ - # parent_models = instance.get_parents_by_model() - - # for parent_model_name, parent in parent_models.items(): - # if isinstance(parent, BaseModel): - # serializer = get_serializer(parent, parent_model_name) - # data_dict[parent_model_name][parent.id] = serializer.data - # self._add_parents_recursively(parent, data_dict) - - class DataAccessViewSet(viewsets.ModelViewSet): queryset = DataAccess.objects.all() serializer_class = DataAccessSerializer