diff --git a/dsp_permissions_scripts/ap/ap_get.py b/dsp_permissions_scripts/ap/ap_get.py index 80e9e19f..60cabb7d 100644 --- a/dsp_permissions_scripts/ap/ap_get.py +++ b/dsp_permissions_scripts/ap/ap_get.py @@ -5,7 +5,9 @@ from dsp_permissions_scripts.models.errors import ApiError from dsp_permissions_scripts.utils.dsp_client import DspClient from dsp_permissions_scripts.utils.get_logger import get_logger -from dsp_permissions_scripts.utils.project import get_project_iri_by_shortcode +from dsp_permissions_scripts.utils.project import ( + get_project_iri_and_onto_iris_by_shortcode, +) logger = get_logger(__name__) @@ -50,7 +52,7 @@ def _get_all_aps_of_project(project_iri: str, dsp_client: DspClient) -> list[Ap] def get_aps_of_project(shortcode: str, dsp_client: DspClient) -> list[Ap]: """Returns the Administrative Permissions for a project.""" logger.info("****** Retrieving all Administrative Permissions... ******") - project_iri = get_project_iri_by_shortcode(shortcode, dsp_client) + project_iri, _ = get_project_iri_and_onto_iris_by_shortcode(shortcode, dsp_client) aps = _get_all_aps_of_project(project_iri, dsp_client) logger.info(f"Retrieved {len(aps)} Administrative Permissions") return aps diff --git a/dsp_permissions_scripts/doap/doap_get.py b/dsp_permissions_scripts/doap/doap_get.py index 54f946d6..76949354 100644 --- a/dsp_permissions_scripts/doap/doap_get.py +++ b/dsp_permissions_scripts/doap/doap_get.py @@ -5,7 +5,9 @@ from dsp_permissions_scripts.models.errors import ApiError from dsp_permissions_scripts.utils.dsp_client import DspClient from dsp_permissions_scripts.utils.get_logger import get_logger -from dsp_permissions_scripts.utils.project import get_project_iri_by_shortcode +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, ) @@ -72,7 +74,7 @@ def get_doaps_of_project( By default, all DOAPs are returned, regardless of their target (target=all). """ logger.info("****** Retrieving all DOAPs... ******") - project_iri = get_project_iri_by_shortcode(shortcode, dsp_client) + project_iri, _ = get_project_iri_and_onto_iris_by_shortcode(shortcode, dsp_client) doaps = _get_all_doaps_of_project(project_iri, dsp_client) filtered_doaps = _filter_doaps_by_target( doaps=doaps, diff --git a/dsp_permissions_scripts/oap/oap_get.py b/dsp_permissions_scripts/oap/oap_get.py index bb5820f6..3523d4f6 100644 --- a/dsp_permissions_scripts/oap/oap_get.py +++ b/dsp_permissions_scripts/oap/oap_get.py @@ -7,7 +7,7 @@ from dsp_permissions_scripts.utils.get_logger import get_logger from dsp_permissions_scripts.utils.project import ( get_all_resource_class_iris_of_project, - get_project_iri_by_shortcode, + get_project_iri_and_onto_iris_by_shortcode, ) from dsp_permissions_scripts.utils.scope_serialization import create_scope_from_string @@ -99,9 +99,9 @@ def get_all_resource_oaps_of_project( excluded_class_iris: Iterable[str] = (), ) -> list[Oap]: logger.info("******* Retrieving all resource OAPs... *******") - project_iri = get_project_iri_by_shortcode(shortcode, dsp_client) + project_iri, onto_iris = get_project_iri_and_onto_iris_by_shortcode(shortcode, dsp_client) all_resource_oaps = [] - resclass_iris = get_all_resource_class_iris_of_project(project_iri, dsp_client) + resclass_iris = get_all_resource_class_iris_of_project(onto_iris, dsp_client) resclass_iris = [x for x in resclass_iris if x not in excluded_class_iris] for resclass_iri in resclass_iris: resource_oaps = _get_all_resource_oaps_of_resclass(resclass_iri, project_iri, dsp_client) diff --git a/dsp_permissions_scripts/utils/dsp_client.py b/dsp_permissions_scripts/utils/dsp_client.py index 442d0586..8619787a 100644 --- a/dsp_permissions_scripts/utils/dsp_client.py +++ b/dsp_permissions_scripts/utils/dsp_client.py @@ -2,7 +2,6 @@ import re import time from dataclasses import dataclass, field -from datetime import datetime from functools import partial from importlib.metadata import version from typing import Any, Literal, Optional, cast diff --git a/dsp_permissions_scripts/utils/project.py b/dsp_permissions_scripts/utils/project.py index dca3eccb..8c53e68c 100644 --- a/dsp_permissions_scripts/utils/project.py +++ b/dsp_permissions_scripts/utils/project.py @@ -8,17 +8,6 @@ logger = get_logger(__name__) -def _get_onto_iris_of_project(project_iri: str, dsp_client: DspClient) -> list[str]: - try: - response = dsp_client.get("/v2/ontologies/metadata") - except ApiError as err: - err.message = f"Could not get onto IRIs of project {project_iri}" - raise err from None - all_ontologies = response["@graph"] - project_onto_iris = [o["@id"] for o in all_ontologies if o["knora-api:attachedToProject"]["@id"] == project_iri] - return project_onto_iris - - def _get_class_iris_of_onto(onto_iri: str, dsp_client: DspClient) -> list[str]: try: response = dsp_client.get(f"/v2/ontologies/allentities/{quote_plus(onto_iri)}") @@ -32,21 +21,21 @@ def _get_class_iris_of_onto(onto_iri: str, dsp_client: DspClient) -> list[str]: return class_iris -def get_all_resource_class_iris_of_project(project_iri: str, dsp_client: DspClient) -> list[str]: - project_onto_iris = _get_onto_iris_of_project(project_iri, dsp_client) +def get_all_resource_class_iris_of_project(onto_iris: list[str], dsp_client: DspClient) -> list[str]: all_class_iris = [] - for onto_iri in project_onto_iris: + for onto_iri in onto_iris: class_iris = _get_class_iris_of_onto(onto_iri, dsp_client) all_class_iris.extend(class_iris) logger.info(f"Found {len(class_iris)} resource classes in onto {onto_iri}.") return all_class_iris -def get_project_iri_by_shortcode(shortcode: str, dsp_client: DspClient) -> str: +def get_project_iri_and_onto_iris_by_shortcode(shortcode: str, dsp_client: DspClient) -> tuple[str, list[str]]: try: response = dsp_client.get(f"/admin/projects/shortcode/{shortcode}") except ApiError as err: err.message = f"Could not get project IRI by shortcode {shortcode}" raise err from None - iri: str = response["project"]["id"] - return iri + project_iri: str = response["project"]["id"] + onto_iris: list[str] = response["project"]["ontologies"] + return project_iri, onto_iris