Skip to content

Commit

Permalink
Merge branch 'main' into wip/rdu-11-create-AP-DOAP
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum authored Aug 9, 2024
2 parents 3691fe5 + c7af42f commit e13c1c0
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 25 deletions.
12 changes: 6 additions & 6 deletions dsp_permissions_scripts/ap/ap_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,28 @@
logger = get_logger(__name__)


def _update_ap_on_server(ap: Ap, dsp_client: DspClient) -> Ap:
def _update_ap_scope_on_server(ap: Ap, dsp_client: DspClient) -> Ap:
iri = quote_plus(ap.iri, safe="")
payload = {"hasPermissions": create_admin_route_object_from_ap(ap)["hasPermissions"]}
try:
response = dsp_client.put(f"/admin/permissions/{iri}/hasPermissions", data=payload)
except ApiError as err:
err.message = f"Could not update Administrative Permission {ap.iri}"
err.message = f"Could not update scope of Administrative Permission {ap.iri}"
raise err from None
ap_updated: dict[str, Any] = response["administrative_permission"]
ap_object_updated = create_ap_from_admin_route_object(ap_updated)
return ap_object_updated


def apply_updated_aps_on_server(aps: list[Ap], host: str, dsp_client: DspClient) -> None:
def apply_updated_scopes_of_aps_on_server(aps: list[Ap], host: str, dsp_client: DspClient) -> None:
if not aps:
logger.warning(f"There are no APs to update on {host}")
return
logger.info(f"****** Updating {len(aps)} Administrative Permissions on {host}... ******")
logger.info(f"****** Updating scopes of {len(aps)} Administrative Permissions on {host}... ******")
for ap in aps:
try:
_ = _update_ap_on_server(ap, dsp_client)
_ = _update_ap_scope_on_server(ap, dsp_client)
logger.info(f"Successfully updated AP {ap.iri}")
except ApiError as err:
logger.error(err)
logger.info(f"Finished updating {len(aps)} Administrative Permissions on {host}")
logger.info(f"Finished updating scopes of {len(aps)} Administrative Permissions on {host}")
2 changes: 1 addition & 1 deletion dsp_permissions_scripts/doap/doap_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def create_doap_from_admin_route_response(permission: dict[str, Any]) -> Doap:
relative_group_iri = None
doap = Doap(
target=DoapTarget(
project=permission["forProject"],
project_iri=permission["forProject"],
group=Group(val=relative_group_iri) if relative_group_iri else None,
resource_class=permission.get("forResourceClass"),
property=permission.get("forProperty"),
Expand Down
2 changes: 1 addition & 1 deletion dsp_permissions_scripts/doap/doap_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Doap(BaseModel):


class DoapTarget(BaseModel):
project: str
project_iri: str
group: Group | None = None
resource_class: str | None = None
property: str | None = None
Expand Down
6 changes: 3 additions & 3 deletions dsp_permissions_scripts/doap/doap_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ def _update_doap_scope_on_server(doap_iri: str, scope: PermissionScope, dsp_clie
return new_doap


def apply_updated_doaps_on_server(doaps: list[Doap], host: str, dsp_client: DspClient) -> None:
def apply_updated_scopes_of_doaps_on_server(doaps: list[Doap], host: str, dsp_client: DspClient) -> None:
if not doaps:
logger.warning(f"There are no DOAPs to update on {host}")
return
logger.info(f"****** Updating {len(doaps)} DOAPs on {host}... ******")
logger.info(f"****** Updating scopes of {len(doaps)} DOAPs on {host}... ******")
for d in doaps:
try:
_ = _update_doap_scope_on_server(d.doap_iri, d.scope, dsp_client)
logger.info(f"Successfully updated DOAP {d.doap_iri}")
except ApiError as err:
logger.error(err)
logger.info(f"Finished updating {len(doaps)} DOAPs on {host}")
logger.info(f"Finished updating scopes of {len(doaps)} DOAPs on {host}")
24 changes: 12 additions & 12 deletions dsp_permissions_scripts/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
from dsp_permissions_scripts.ap.ap_model import Ap
from dsp_permissions_scripts.ap.ap_model import ApValue
from dsp_permissions_scripts.ap.ap_serialize import serialize_aps_of_project
from dsp_permissions_scripts.ap.ap_set import apply_updated_aps_on_server
from dsp_permissions_scripts.ap.ap_set import apply_updated_scopes_of_aps_on_server
from dsp_permissions_scripts.doap.doap_get import get_doaps_of_project
from dsp_permissions_scripts.doap.doap_model import Doap
from dsp_permissions_scripts.doap.doap_serialize import serialize_doaps_of_project
from dsp_permissions_scripts.doap.doap_set import apply_updated_doaps_on_server
from dsp_permissions_scripts.doap.doap_set import apply_updated_scopes_of_doaps_on_server
from dsp_permissions_scripts.models import group
from dsp_permissions_scripts.models.host import Hosts
from dsp_permissions_scripts.models.scope import PUBLIC, PermissionScope
Expand All @@ -32,9 +32,9 @@ def modify_aps(aps: list[Ap]) -> list[Ap]:
"""Adapt this sample to your needs."""
modified_aps = []
for ap in copy.deepcopy(aps):
if ap.forGroup == group.UNKNOWN_USER:
if ApValue.ProjectAdminGroupAllPermission not in ap.hasPermissions:
ap.add_permission(ApValue.ProjectAdminGroupAllPermission)
if ap.forGroup == group.PROJECT_ADMIN:
if ApValue.ProjectAdminRightsAllPermission not in ap.hasPermissions:
ap.add_permission(ApValue.ProjectAdminRightsAllPermission)
modified_aps.append(ap)
return modified_aps

Expand All @@ -43,7 +43,7 @@ def modify_doaps(doaps: list[Doap]) -> list[Doap]:
"""Adapt this sample to your needs."""
modified_doaps = []
for doap in copy.deepcopy(doaps):
if doap.target.group == group.UNKNOWN_USER:
if doap.target.group == group.PROJECT_ADMIN:
doap.scope = PUBLIC
modified_doaps.append(doap)
return modified_doaps
Expand Down Expand Up @@ -75,7 +75,7 @@ def update_aps(host: str, shortcode: str, dsp_client: DspClient) -> None:
remaining_aps = delete_ap_of_group_on_server(
host=host,
existing_aps=project_aps,
forGroup=group.UNKNOWN_USER,
forGroup=group.PROJECT_MEMBER,
dsp_client=dsp_client,
)
_ = create_new_ap_on_server( # noqa: F821
Expand All @@ -88,7 +88,7 @@ def update_aps(host: str, shortcode: str, dsp_client: DspClient) -> None:
if not modified_aps:
logger.info("There are no APs to update.")
return
apply_updated_aps_on_server(modified_aps, host, dsp_client)
apply_updated_scopes_of_aps_on_server(modified_aps, host, dsp_client)
project_aps_updated = get_aps_of_project(shortcode, dsp_client)
serialize_aps_of_project(
project_aps=project_aps_updated,
Expand Down Expand Up @@ -117,7 +117,7 @@ def update_doaps(host: str, shortcode: str, dsp_client: DspClient) -> None:
if not project_doaps_modified:
logger.info("There are no DOAPs to update.")
return
apply_updated_doaps_on_server(project_doaps_modified, host, dsp_client)
apply_updated_scopes_of_doaps_on_server(project_doaps_modified, host, dsp_client)
project_doaps_updated = get_doaps_of_project(shortcode, dsp_client)
serialize_doaps_of_project(
project_doaps=project_doaps_updated,
Expand Down Expand Up @@ -154,14 +154,14 @@ def main() -> None:
and one to update the Object Access Permissions of a project.
All must first be adapted to your needs.
"""
host = Hosts.get_host("test")
shortcode = "F18E"
host = Hosts.get_host("localhost")
shortcode = "4123"
log_start_of_script(host, shortcode)
dsp_client = login(host)

oap_config = OapRetrieveConfig(
retrieve_resources="specified_res_classes",
specified_res_classes=["my-data-model:ImageThing"],
specified_res_classes=["testonto:ImageThing"],
retrieve_values="specified_props",
specified_props=["knora-api:hasStillImageFileValue"],
)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_doap_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def _setup_teardown(self) -> Iterator[None]:
def test_doap_serialization(self) -> None:
doap1 = Doap(
target=DoapTarget(
project="http://rdfh.ch/projects/MsOaiQkcQ7-QPxsYBKckfQ",
project_iri="http://rdfh.ch/projects/MsOaiQkcQ7-QPxsYBKckfQ",
group=group.PROJECT_ADMIN,
),
scope=PermissionScope.create(
Expand All @@ -37,7 +37,7 @@ def test_doap_serialization(self) -> None:
)
doap2 = Doap(
target=DoapTarget(
project="http://rdfh.ch/projects/MsOaiQkcQ7-QPxsYBKckfQ",
project_iri="http://rdfh.ch/projects/MsOaiQkcQ7-QPxsYBKckfQ",
group=group.PROJECT_MEMBER,
),
scope=PermissionScope.create(
Expand Down

0 comments on commit e13c1c0

Please sign in to comment.