Skip to content

Commit

Permalink
sort groups
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum committed Oct 6, 2023
1 parent f1145cb commit ea8e77d
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 7 deletions.
23 changes: 23 additions & 0 deletions dsp_permissions_scripts/utils/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from dsp_permissions_scripts.models.groups import BuiltinGroup


def dereference_prefix(
identifier: str,
context: dict[str, str],
) -> str:
prefix, actual_id = identifier.split(":")
return context[prefix] + actual_id


def sort_algorith_custom_groups(group: str) -> int:
alphabet = list("abcdefghijklmnopqrstuvwxyz")
relevant_letter = group.replace("http://www.knora.org/ontology/knora-admin#", "")[0]
return alphabet.index(relevant_letter.lower()) + 999


def sort_groups(groups_original: list[str]) -> list[str]:
"""Sorts groups by their name, except for the built-in groups, which are sorted to the beginning."""
sort_key = list(reversed([x.value for x in BuiltinGroup]))
groups = groups_original.copy()
groups.sort(key=lambda x: sort_key.index(x) if x in sort_key else sort_algorith_custom_groups(x))
return groups
8 changes: 2 additions & 6 deletions dsp_permissions_scripts/utils/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from dsp_permissions_scripts.models.oap import Oap
from dsp_permissions_scripts.utils.authentication import get_protocol
from dsp_permissions_scripts.utils.get_logger import get_logger, get_timestamp
from dsp_permissions_scripts.utils.helpers import dereference_prefix
from dsp_permissions_scripts.utils.scope_serialization import create_scope_from_string

logger = get_logger(__name__)
Expand Down Expand Up @@ -61,15 +62,10 @@ def _get_class_iris_of_onto(
all_entities = response.json()["@graph"]
context = response.json()["@context"]
class_ids = [c["@id"] for c in all_entities if c.get("knora-api:isResourceClass")]
class_iris = [_dereference_prefix(class_id, context) for class_id in class_ids]
class_iris = [dereference_prefix(class_id, context) for class_id in class_ids]
return class_iris


def _dereference_prefix(identifier: str, context: dict[str, str]) -> str:
prefix, actual_id = identifier.split(":")
return context[prefix] + actual_id


def _get_all_resource_oaps_of_resclass(
host: str,
resclass_iri: str,
Expand Down
3 changes: 2 additions & 1 deletion dsp_permissions_scripts/utils/scope_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from dsp_permissions_scripts.models.groups import BuiltinGroup
from dsp_permissions_scripts.models.scope import PermissionScope
from dsp_permissions_scripts.utils.helpers import sort_groups


def create_string_from_scope(perm_scope: PermissionScope) -> str:
Expand All @@ -10,7 +11,7 @@ def create_string_from_scope(perm_scope: PermissionScope) -> str:
for perm_letter, groups in perm_scope.model_dump().items():
if groups:
groups_as_str = [g.value if isinstance(g, BuiltinGroup) else g for g in groups]
as_dict[perm_letter] = groups_as_str
as_dict[perm_letter] = sort_groups(groups_as_str)
strs = [f"{k} {','.join(l)}" for k, l in as_dict.items()]
return "|".join(strs)

Expand Down
37 changes: 37 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import unittest

from dsp_permissions_scripts.models.groups import BuiltinGroup
from dsp_permissions_scripts.utils.helpers import sort_groups


class TestHelpers(unittest.TestCase):

def test_sort_groups(self) -> None:
groups_original = [
"http://www.knora.org/ontology/knora-admin#C_CustomGroup",
BuiltinGroup.UNKNOWN_USER.value,
BuiltinGroup.PROJECT_ADMIN.value,
BuiltinGroup.PROJECT_MEMBER.value,
BuiltinGroup.CREATOR.value,
"http://www.knora.org/ontology/knora-admin#A_CustomGroup",
"http://www.knora.org/ontology/knora-admin#B_CustomGroup",
BuiltinGroup.KNOWN_USER.value,
BuiltinGroup.SYSTEM_ADMIN.value,
]
groups_expected = [
BuiltinGroup.SYSTEM_ADMIN.value,
BuiltinGroup.CREATOR.value,
BuiltinGroup.PROJECT_ADMIN.value,
BuiltinGroup.PROJECT_MEMBER.value,
BuiltinGroup.KNOWN_USER.value,
BuiltinGroup.UNKNOWN_USER.value,
"http://www.knora.org/ontology/knora-admin#A_CustomGroup",
"http://www.knora.org/ontology/knora-admin#B_CustomGroup",
"http://www.knora.org/ontology/knora-admin#C_CustomGroup",
]
groups_returned = sort_groups(groups_original=groups_original)
self.assertEqual(groups_returned, groups_expected)


if __name__ == "__main__":
unittest.main()

0 comments on commit ea8e77d

Please sign in to comment.