Skip to content

Commit

Permalink
reorganize modules
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum committed Oct 12, 2023
1 parent c0fbd69 commit 499fd04
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 46 deletions.
7 changes: 5 additions & 2 deletions dsp_permissions_scripts/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
from dsp_permissions_scripts.models.host import Hosts
from dsp_permissions_scripts.models.oap import Oap
from dsp_permissions_scripts.models.scope import PUBLIC
from dsp_permissions_scripts.utils.ap.ap_get import delete_ap, get_aps_of_project
from dsp_permissions_scripts.utils.ap.ap_get import get_aps_of_project
from dsp_permissions_scripts.utils.ap.ap_serialize import serialize_aps_of_project
from dsp_permissions_scripts.utils.ap.ap_set import apply_updated_aps_on_server
from dsp_permissions_scripts.utils.ap.ap_set import (
apply_updated_aps_on_server,
delete_ap,
)
from dsp_permissions_scripts.utils.authentication import login
from dsp_permissions_scripts.utils.doap_get import get_doaps_of_project
from dsp_permissions_scripts.utils.doap_serialize import serialize_doaps_of_project
Expand Down
44 changes: 0 additions & 44 deletions dsp_permissions_scripts/utils/ap/ap_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,50 +55,6 @@ def _get_all_aps_of_project(
return ap_objects


def _filter_aps_by_group(
aps: list[Ap],
forGroup: str,
) -> Ap:
aps = [ap for ap in aps if ap.forGroup == forGroup]
assert len(aps) == 1
return aps[0]


def _delete_single_ap(
ap: Ap,
host: str,
token: str,
) -> None:
headers = {"Authorization": f"Bearer {token}"}
ap_iri = quote_plus(ap.iri, safe="")
protocol = get_protocol(host)
url = f"{protocol}://{host}/admin/permissions/{ap_iri}"
response = requests.delete(url, headers=headers, timeout=5)
assert response.status_code == 200
logger.info(f"Deleted Administrative Permission {ap.iri} on host {host}")


def delete_ap(
host: str,
token: str,
existing_aps: list[Ap],
forGroup: str,
) -> list[Ap]:
"""Deletes the Administrative Permission of a group."""
logger.info(f"Deleting the Administrative Permission for group {forGroup} on server {host}")
ap_to_delete = _filter_aps_by_group(
aps=existing_aps,
forGroup=forGroup,
)
_delete_single_ap(
ap=ap_to_delete,
host=host,
token=token,
)
existing_aps.remove(ap_to_delete)
return existing_aps


def get_aps_of_project(
host: str,
shortcode: str,
Expand Down
45 changes: 45 additions & 0 deletions dsp_permissions_scripts/utils/ap/ap_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,30 @@

logger = get_logger(__name__)


def _filter_aps_by_group(
aps: list[Ap],
forGroup: str,
) -> Ap:
aps = [ap for ap in aps if ap.forGroup == forGroup]
assert len(aps) == 1
return aps[0]


def _delete_single_ap(
ap: Ap,
host: str,
token: str,
) -> None:
headers = {"Authorization": f"Bearer {token}"}
ap_iri = quote_plus(ap.iri, safe="")
protocol = get_protocol(host)
url = f"{protocol}://{host}/admin/permissions/{ap_iri}"
response = requests.delete(url, headers=headers, timeout=5)
assert response.status_code == 200
logger.info(f"Deleted Administrative Permission {ap.iri} on host {host}")


def _update_ap(
ap: Ap,
host: str,
Expand Down Expand Up @@ -77,3 +101,24 @@ def apply_updated_aps_on_server(
warnings.warn(f"ERROR while updating Administrative Permission {ap.iri}")

print(f"{get_timestamp()}: All APs have been updated.")


def delete_ap(
host: str,
token: str,
existing_aps: list[Ap],
forGroup: str,
) -> list[Ap]:
"""Deletes the Administrative Permission of a group."""
logger.info(f"Deleting the Administrative Permission for group {forGroup} on server {host}")
ap_to_delete = _filter_aps_by_group(
aps=existing_aps,
forGroup=forGroup,
)
_delete_single_ap(
ap=ap_to_delete,
host=host,
token=token,
)
existing_aps.remove(ap_to_delete)
return existing_aps

0 comments on commit 499fd04

Please sign in to comment.