From 1aa68d319089d2cefa29669cfc25e8299604d04e Mon Sep 17 00:00:00 2001 From: Nassim Tabchiche Date: Tue, 7 May 2024 20:05:15 +0200 Subject: [PATCH] Rewrite flatten_dict without pandas --- backend/core/helpers.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/backend/core/helpers.py b/backend/core/helpers.py index 9cbdafa88..c71476bb1 100644 --- a/backend/core/helpers.py +++ b/backend/core/helpers.py @@ -1,7 +1,6 @@ from collections.abc import MutableMapping from datetime import date, timedelta -import pandas as pd from django.db.models import Count from django.shortcuts import get_object_or_404 from iam.models import Folder, Permission, RoleAssignment, User @@ -12,9 +11,17 @@ from .utils import camel_case -def flatten_dict(d: MutableMapping, sep: str = ".") -> MutableMapping: - [flat_dict] = pd.json_normalize(d, sep=sep).to_dict(orient="records") - return flat_dict +def flatten_dict( + d: MutableMapping, parent_key: str = "", sep: str = "." +) -> MutableMapping: + items = [] + for k, v in d.items(): + new_key = parent_key + sep + k if parent_key else k + if isinstance(v, MutableMapping): + items.extend(flatten_dict(v, new_key, sep=sep).items()) + else: + items.append((new_key, v)) + return dict(items) STATUS_COLOR_MAP = { # TODO: Move these kinds of color maps to frontend