-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
64 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |