From 965971c879b3ff86c6663d0ff8ef763c60f4a8bb Mon Sep 17 00:00:00 2001 From: Johannes Nussbaum Date: Wed, 11 Oct 2023 12:04:39 +0200 Subject: [PATCH] model_dump(mode="json") --- dsp_permissions_scripts/template.py | 14 +++------- .../utils/scope_serialization.py | 26 ++++++++----------- 2 files changed, 15 insertions(+), 25 deletions(-) diff --git a/dsp_permissions_scripts/template.py b/dsp_permissions_scripts/template.py index d93754a7..0e01553b 100644 --- a/dsp_permissions_scripts/template.py +++ b/dsp_permissions_scripts/template.py @@ -12,17 +12,15 @@ ) from dsp_permissions_scripts.utils.doap_set import apply_updated_doaps_on_server from dsp_permissions_scripts.utils.oap import apply_updated_oaps_on_server -from dsp_permissions_scripts.utils.oap_serialize import ( - deserialize_resource_oaps, - serialize_resource_oaps, -) +from dsp_permissions_scripts.utils.oap_serialize import serialize_resource_oaps from dsp_permissions_scripts.utils.project import get_all_resource_oaps_of_project def modify_oaps(oaps: list[Oap]) -> list[Oap]: """Adapt this sample to your needs.""" for oap in oaps: - oap.scope = oap.scope.add("CR", BuiltinGroup.SYSTEM_ADMIN) + if BuiltinGroup.SYSTEM_ADMIN.value not in oap.scope.CR: + oap.scope = oap.scope.add("CR", BuiltinGroup.SYSTEM_ADMIN) return oaps @@ -52,11 +50,7 @@ def update_oaps( ) resource_oaps_updated = modify_oaps(oaps=resource_oaps) serialize_resource_oaps( - resource_oaps=resource_oaps, - shortcode=shortcode, - mode="modified", - ) - resource_oaps_updated = deserialize_resource_oaps( + resource_oaps=resource_oaps_updated, shortcode=shortcode, mode="modified", ) 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