Attempt to fix issues with missing data

sync
Laurent 9 months ago
parent c8d629924a
commit 5aa4a47340
  1. 31
      sync/signals.py
  2. 47
      sync/views.py

@ -117,16 +117,7 @@ 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):
# 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()
@ -137,6 +128,26 @@ def save_model_log(users, model_operation, model_name, model_id, store_id, devic
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)
def detect_foreign_key_changes(sender, instance, device_id):
if not hasattr(instance, 'pk') or not instance.pk:
return

@ -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

Loading…
Cancel
Save