From 00c44b4eba1cb82a97576fbbba170aaea14483a1 Mon Sep 17 00:00:00 2001 From: Laurent Date: Thu, 12 Dec 2024 15:36:29 +0100 Subject: [PATCH] Manage to provide an order for the revocation hierarchy --- sync/utils.py | 46 ++++++++++++++++++++++++++++ sync/views.py | 84 +++++++++++++++++++++++++++------------------------ 2 files changed, 90 insertions(+), 40 deletions(-) diff --git a/sync/utils.py b/sync/utils.py index ec1ab77..b6c0f32 100644 --- a/sync/utils.py +++ b/sync/utils.py @@ -1,6 +1,7 @@ import importlib from django.apps import apps from .registry import sync_registry +from collections import defaultdict def build_serializer_class(model_name): @@ -40,3 +41,48 @@ def get_serialized_data(model_name, model_id): serializer_class = build_serializer_class(model_name) serializer = serializer_class(instance) return serializer.data + +class HierarchyOrganizer: + def __init__(self): + self.levels = [] # List of dictionaries, each representing a level + self.item_levels = {} # Keep track of items and their levels: (model_name, id) -> level + + def add_item(self, model_name, item_data, level): + """ + Add an item to a specific level, ensuring it's at the highest level if already exists + """ + item_key = (model_name, item_data['model_id']) + + # If item exists at a lower level, remove it + existing_level = self.item_levels.get(item_key) + if existing_level is not None: + if existing_level < level: + # Keep the lower level (deeper in hierarchy) + return + # Remove from existing level + self.levels[existing_level][model_name] = [ + item for item in self.levels[existing_level].get(model_name, []) + if item['model_id'] != item_data['model_id'] + ] + + # Ensure we have enough levels + while len(self.levels) <= level: + self.levels.append(defaultdict(list)) + + # Add item to the appropriate level + if model_name not in self.levels[level]: + self.levels[level][model_name] = [] + self.levels[level][model_name].append(item_data) + self.item_levels[item_key] = level + + def get_organized_data(self): + """ + Return the organized levels, cleaning up empty models/levels + """ + # Clean up empty models and levels + cleaned_levels = [] + for level_dict in self.levels: + cleaned_dict = {k: v for k, v in level_dict.items() if v} + if cleaned_dict: + cleaned_levels.append(cleaned_dict) + return cleaned_levels diff --git a/sync/views.py b/sync/views.py index bb35a49..ceb9870 100644 --- a/sync/views.py +++ b/sync/views.py @@ -15,7 +15,7 @@ from django.core.exceptions import ObjectDoesNotExist from collections import defaultdict from urllib.parse import unquote -from .utils import get_serializer, build_serializer_class, get_data, get_serialized_data +from .utils import get_serializer, build_serializer_class, get_data, get_serialized_data, HierarchyOrganizer from .models import ModelLog, BaseModel, SideStoreModel, DataAccess @@ -24,31 +24,41 @@ from .registry import sync_registry class HierarchyApiView(APIView): def add_children_recursively(self, instance, updates): - """ - Recursively add all children of an instance to the updates dictionary. - """ - # print(f"Instance class: {instance.__class__}") - 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) - # serializer_class = build_serializer_class(child_model_name) - # serializer = serializer_class(child) - updates[child_model_name][child.id] = serializer.data - self.add_children_recursively(child, updates) - - # def add_parents_recursively(self, instance, updates): - # parent_models = instance.get_parents_by_model() - - # for parent_model_name, parent in parent_models.items(): - # # print(f'parent = {parent_model_name}') - # if isinstance(parent, BaseModel): - # serializer_class = build_serializer_class(parent_model_name) - # serializer = serializer_class(parent) - # updates[parent_model_name][parent.id] = serializer.data - # self.add_parents_recursively(parent, updates) + """ + Recursively add all children of an instance to the updates 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) + updates[child_model_name][child.id] = serializer.data + self.add_children_recursively(child, updates) + + def add_parents_with_hierarchy_organizer(self, instance, hierarchy_organizer, current_level=0): + """ + Recursively add all parents of an instance to the hierarchy organizer. + Parents are added at a higher level than their children. + """ + parent_models = instance.get_parents_by_model() + + for parent_model_name, parent in parent_models.items(): + if isinstance(parent, BaseModel): + store_id = None + if isinstance(parent, SideStoreModel): + store_id = parent.store_id + + parent_data = { + 'model_id': parent.id, + 'store_id': store_id + } + + # Add parent at the next level + hierarchy_organizer.add_item(parent_model_name, parent_data, current_level + 1) + + # Recursively process parent's parents + self.add_parents_with_hierarchy_organizer(parent, hierarchy_organizer, current_level + 1) def add_parents_recursively(self, instance, dictionary, minimal=False): """ @@ -69,9 +79,6 @@ class HierarchyApiView(APIView): } else: serializer = get_serializer(parent, parent_model_name) - # Add full serialized data - # serializer_class = build_serializer_class(parent_model_name) - # serializer = serializer_class(parent) dictionary[parent_model_name][parent.id] = serializer.data self.add_parents_recursively(parent, dictionary, minimal) @@ -155,7 +162,9 @@ class SynchronizationApi(HierarchyApiView): deletions = defaultdict(list) grants = defaultdict(dict) revocations = defaultdict(list) # New dictionary for revocations - revocation_parents = defaultdict(dict) + revocations_parents_organizer = HierarchyOrganizer() + + # revocated_parents = defaultdict(dict) last_log_date = None for log in logs: @@ -170,15 +179,10 @@ class SynchronizationApi(HierarchyApiView): elif log.operation == 'GRANT_ACCESS': model = sync_registry.get_model(log.model_name) - - # model = apps.get_model('tournaments', model_name=log.model_name) instance = model.objects.get(id=log.model_id) - # serializer_class = build_serializer_class(log.model_name) - # serializer = serializer_class(instance) serializer = get_serializer(instance, log.model_name) grants[log.model_name][log.model_id] = serializer.data - # instance = model.objects.get(id=log.model_id) self.add_children_recursively(instance, grants) self.add_parents_recursively(instance, grants) elif log.operation == 'REVOKE_ACCESS': @@ -188,11 +192,11 @@ class SynchronizationApi(HierarchyApiView): 'store_id': log.store_id }) - # Get the model instance and add its parents to revocation_parents + # Get the model instance and add its parents to hierarchy model = sync_registry.get_model(log.model_name) try: instance = model.objects.get(id=log.model_id) - self.add_parents_recursively(instance, revocation_parents, minimal=True) + self.add_parents_with_hierarchy_organizer(instance, revocations_parents_organizer) except model.DoesNotExist: pass except ObjectDoesNotExist: @@ -209,15 +213,15 @@ class SynchronizationApi(HierarchyApiView): for model_name in grants: grants[model_name] = list(grants[model_name].values()) - for model_name in revocation_parents: - revocation_parents[model_name] = list(revocation_parents[model_name].values()) + # for model_name in revocation_parents: + # revocation_parents[model_name] = list(revocation_parents[model_name].values()) response_data = { "updates": dict(updates), "deletions": dict(deletions), "grants": dict(grants), "revocations": dict(revocations), - "revocation_parents": dict(revocation_parents), + "revocation_parents": revocations_parents_organizer.get_organized_data(), "date": last_log_date }