From 4d921940dae36cb1587f085f4e449e374300e82c Mon Sep 17 00:00:00 2001 From: Johannes Nussbaum Date: Wed, 11 Oct 2023 11:51:32 +0200 Subject: [PATCH] use model_dump(mode="json") in existing code --- .../utils/scope_serialization.py | 26 ++++++++----------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/dsp_permissions_scripts/utils/scope_serialization.py b/dsp_permissions_scripts/utils/scope_serialization.py index 503e7975..f5216631 100644 --- a/dsp_permissions_scripts/utils/scope_serialization.py +++ b/dsp_permissions_scripts/utils/scope_serialization.py @@ -1,6 +1,5 @@ from typing import Any -from dsp_permissions_scripts.models.groups import BuiltinGroup from dsp_permissions_scripts.models.scope import PermissionScope from dsp_permissions_scripts.utils.helpers import sort_groups @@ -8,10 +7,9 @@ def create_string_from_scope(perm_scope: PermissionScope) -> str: """Serializes a permission scope to a permissions string as used by /v2 routes.""" as_dict = {} - for perm_letter, groups in perm_scope.model_dump().items(): + for perm_letter, groups in perm_scope.model_dump(mode="json").items(): if groups: - groups_as_str = [g.value if isinstance(g, BuiltinGroup) else g for g in groups] - as_dict[perm_letter] = sort_groups(groups_as_str) + as_dict[perm_letter] = sort_groups(groups) strs = [f"{k} {','.join(l)}" for k, l in as_dict.items()] return "|".join(strs) @@ -43,15 +41,13 @@ def create_scope_from_admin_route_object(admin_route_object: list[dict[str, Any] def create_admin_route_object_from_scope(perm_scope: PermissionScope) -> list[dict[str, str | None]]: """Serializes a permission scope to a shape that can be used for requests to /admin/permissions routes.""" scope_elements: list[dict[str, str | None]] = [] - for perm_letter, groups in perm_scope.model_dump().items(): - if groups: - groups_as_str = [g.value if isinstance(g, BuiltinGroup) else g for g in groups] - for group in groups_as_str: - scope_elements.append( - { - "additionalInformation": group, - "name": perm_letter, - "permissionCode": None, - } - ) + for perm_letter, groups in perm_scope.model_dump(mode="json").items(): + for group in groups: + scope_elements.append( + { + "additionalInformation": group, + "name": perm_letter, + "permissionCode": None, + } + ) return scope_elements