Skip to content

Commit

Permalink
use constant for knora-admin onto prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum committed Jul 18, 2024
1 parent da3f3d4 commit 30420df
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 9 deletions.
3 changes: 2 additions & 1 deletion dsp_permissions_scripts/ap/ap_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
from dsp_permissions_scripts.models.group import Group
from dsp_permissions_scripts.utils.dsp_client import DspClient
from dsp_permissions_scripts.utils.get_logger import get_logger
from dsp_permissions_scripts.utils.helpers import KNORA_ADMIN_ONTO_PREFIX
from dsp_permissions_scripts.utils.project import get_project_iri_and_onto_iris_by_shortcode

logger = get_logger(__name__)


def create_ap_from_admin_route_object(permission: dict[str, Any]) -> Ap:
"""Deserializes a AP from JSON as returned by /admin/permissions/ap/{project_iri}"""
relative_group_iri = permission["forGroup"].replace("http://www.knora.org/ontology/knora-admin#", "knora-admin:")
relative_group_iri = permission["forGroup"].replace(KNORA_ADMIN_ONTO_PREFIX, "knora-admin:")
ap = Ap(
forGroup=Group(val=relative_group_iri),
forProject=permission["forProject"],
Expand Down
5 changes: 2 additions & 3 deletions dsp_permissions_scripts/doap/doap_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from dsp_permissions_scripts.models.group import Group
from dsp_permissions_scripts.utils.dsp_client import DspClient
from dsp_permissions_scripts.utils.get_logger import get_logger
from dsp_permissions_scripts.utils.helpers import KNORA_ADMIN_ONTO_PREFIX
from dsp_permissions_scripts.utils.project import get_project_iri_and_onto_iris_by_shortcode
from dsp_permissions_scripts.utils.scope_serialization import create_scope_from_admin_route_object

Expand Down Expand Up @@ -50,9 +51,7 @@ def create_doap_from_admin_route_response(permission: dict[str, Any]) -> Doap:
"""Deserializes a DOAP from JSON as returned by /admin/permissions/doap/{project_iri}"""
scope = create_scope_from_admin_route_object(permission["hasPermissions"])
if permission.get("forGroup"):
relative_group_iri = permission["forGroup"].replace(
"http://www.knora.org/ontology/knora-admin#", "knora-admin:"
)
relative_group_iri = permission["forGroup"].replace(KNORA_ADMIN_ONTO_PREFIX, "knora-admin:")
else:
relative_group_iri = None
doap = Doap(
Expand Down
3 changes: 2 additions & 1 deletion dsp_permissions_scripts/doap/doap_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from dsp_permissions_scripts.models.scope import PermissionScope
from dsp_permissions_scripts.utils.dsp_client import DspClient
from dsp_permissions_scripts.utils.get_logger import get_logger
from dsp_permissions_scripts.utils.helpers import KNORA_ADMIN_ONTO_PREFIX
from dsp_permissions_scripts.utils.scope_serialization import create_admin_route_object_from_scope

logger = get_logger(__name__)
Expand All @@ -15,7 +16,7 @@ def _update_doap_scope_on_server(doap_iri: str, scope: PermissionScope, dsp_clie
iri = quote_plus(doap_iri, safe="")
payload = {
"hasPermissions": create_admin_route_object_from_scope(scope),
"context": {"knora-admin": "http://www.knora.org/ontology/knora-admin#"},
"context": {"knora-admin": KNORA_ADMIN_ONTO_PREFIX},
}
try:
response = dsp_client.put(f"/admin/permissions/{iri}/hasPermissions", data=payload)
Expand Down
5 changes: 3 additions & 2 deletions dsp_permissions_scripts/oap/oap_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from dsp_permissions_scripts.oap.oap_model import ValueOap
from dsp_permissions_scripts.utils.dsp_client import DspClient
from dsp_permissions_scripts.utils.get_logger import get_logger
from dsp_permissions_scripts.utils.helpers import KNORA_ADMIN_ONTO_PREFIX
from dsp_permissions_scripts.utils.scope_serialization import create_string_from_scope

logger = get_logger(__name__)
Expand Down Expand Up @@ -86,7 +87,7 @@ def _update_batch(batch: tuple[ResourceOap | ValueOap, ...], dsp_client: DspClie
resource_iri=oap.resource_iri,
lmd=resource.get("knora-api:lastModificationDate"),
resource_type=resource["@type"],
context=resource["@context"] | {"knora-admin": "http://www.knora.org/ontology/knora-admin#"},
context=resource["@context"] | {"knora-admin": KNORA_ADMIN_ONTO_PREFIX},
scope=oap.scope,
dsp_client=dsp_client,
)
Expand All @@ -99,7 +100,7 @@ def _update_batch(batch: tuple[ResourceOap | ValueOap, ...], dsp_client: DspClie
resource_iri=oap.resource_iri,
value=oap,
resource_type=resource["@type"],
context=resource["@context"] | {"knora-admin": "http://www.knora.org/ontology/knora-admin#"},
context=resource["@context"] | {"knora-admin": KNORA_ADMIN_ONTO_PREFIX},
dsp_client=dsp_client,
)
except ApiError as err:
Expand Down
1 change: 1 addition & 0 deletions dsp_permissions_scripts/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from dsp_permissions_scripts.models import group

PACKAGE_NAME = "dsp-permissions-scripts"
KNORA_ADMIN_ONTO_PREFIX = "http://www.knora.org/ontology/knora-admin#"


def dereference_prefix(
Expand Down
5 changes: 3 additions & 2 deletions dsp_permissions_scripts/utils/scope_serialization.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Any

from dsp_permissions_scripts.models.scope import PermissionScope
from dsp_permissions_scripts.utils.helpers import KNORA_ADMIN_ONTO_PREFIX
from dsp_permissions_scripts.utils.helpers import sort_groups


Expand All @@ -21,7 +22,7 @@ def create_scope_from_string(permission_string: str) -> PermissionScope:
for scope in scopes:
perm_letter, groups_as_str = scope.split(" ")
groups = groups_as_str.split(",")
kwargs[perm_letter] = [g.replace("http://www.knora.org/ontology/knora-admin#", "knora-admin:") for g in groups]
kwargs[perm_letter] = [g.replace(KNORA_ADMIN_ONTO_PREFIX, "knora-admin:") for g in groups]
return PermissionScope.from_dict(kwargs)


Expand All @@ -30,7 +31,7 @@ def create_scope_from_admin_route_object(admin_route_object: list[dict[str, Any]
kwargs: dict[str, list[str]] = {}
for obj in admin_route_object:
attr_name: str = obj["name"]
group: str = obj["additionalInformation"].replace("http://www.knora.org/ontology/knora-admin#", "knora-admin:")
group: str = obj["additionalInformation"].replace(KNORA_ADMIN_ONTO_PREFIX, "knora-admin:")
if attr_name in kwargs:
kwargs[attr_name].append(group)
else:
Expand Down

0 comments on commit 30420df

Please sign in to comment.