Skip to content

Commit

Permalink
use model_dump(mode="json") in existing code
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum committed Oct 11, 2023
1 parent 62d7c83 commit 4d92194
Showing 1 changed file with 11 additions and 15 deletions.
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 4d92194

Please sign in to comment.