Skip to content

Commit

Permalink
model_dump(mode="json")
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum committed Oct 11, 2023
1 parent dabb53a commit 965971c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 25 deletions.
14 changes: 4 additions & 10 deletions dsp_permissions_scripts/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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",
)
Expand Down
26 changes: 11 additions & 15 deletions dsp_permissions_scripts/utils/scope_serialization.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
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


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)

Expand Down Expand Up @@ -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

0 comments on commit 965971c

Please sign in to comment.