Skip to content

Commit

Permalink
update cleanup script
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum committed Oct 23, 2023
1 parent c6019c5 commit d1414c1
Showing 1 changed file with 5 additions and 14 deletions.
19 changes: 5 additions & 14 deletions dsp_permissions_scripts/scenario_tanner/cleanup_tanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,11 @@ def _get_affected_resources(host: str) -> list[AffectedResource]:
return affected_resources


def _get_valueHasUUIDs(
def _assert_contested_value_exists(
affected_resources: list[AffectedResource],
host: str,
token: str,
) -> list[str]:
valueHasUUIDs: list[str] = []
) -> None:
for aff_res in affected_resources:
headers = {"Authorization": f"Bearer {token}"}
url = f"https://{host}/v2/resources/{quote_plus(aff_res.res_iri)}"
Expand All @@ -99,11 +98,7 @@ def _get_valueHasUUIDs(
assert aff_res.val_iri == response_as_json[prop_short]["@id"]
else:
assert aff_res.val_iri.endswith(response_as_json[prop_short]["knora-api:valueHasUUID"])
_uuid = response_as_json[prop_short]["knora-api:valueHasUUID"]
valueHasUUIDs.append(_uuid)

return valueHasUUIDs


def _get_new_scope() -> PermissionScope:
scope = PermissionScope.create(
Expand Down Expand Up @@ -133,15 +128,14 @@ def _get_params_for_update(

def _update_permissions_for_affected_value(
affected_resource: AffectedResource,
new_val_iri: str,
host: str,
token: str,
) -> None:
resource_type, value_type, context = _get_params_for_update(affected_resource, host, token)
scope = _get_new_scope()
_update_permissions_for_value(
resource_iri=affected_resource.res_iri,
value=ValueUpdate(affected_resource.prop_iri, new_val_iri, value_type),
value=ValueUpdate(affected_resource.prop_iri, affected_resource.val_iri, value_type),
resource_type=resource_type,
context=context,
scope=scope,
Expand All @@ -162,17 +156,14 @@ def cleanup_tanner() -> None:
token = login(host)

affected_resources = _get_affected_resources(host)
valueHasUUIDs = _get_valueHasUUIDs(
_assert_contested_value_exists(
affected_resources=affected_resources,
host=host,
token=token,
)
for aff_res, _uuid in zip(affected_resources, valueHasUUIDs):
new_val_iri = re.sub(r"(?<=/values/).+", _uuid, aff_res.val_iri)
logger.info(f"Resource {aff_res.res_iri}: try value IRI {new_val_iri} instead of {aff_res.val_iri}")
for aff_res in affected_resources:
_update_permissions_for_affected_value(
affected_resource=aff_res,
new_val_iri=new_val_iri,
host=host,
token=token,
)
Expand Down

0 comments on commit d1414c1

Please sign in to comment.