-
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.
move sort_groups() to group_utils.py
- Loading branch information
Showing
4 changed files
with
87 additions
and
78 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
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,32 @@ | ||
from typing import Iterable | ||
|
||
from dsp_permissions_scripts.models.group import CREATOR | ||
from dsp_permissions_scripts.models.group import KNOWN_USER | ||
from dsp_permissions_scripts.models.group import PROJECT_ADMIN | ||
from dsp_permissions_scripts.models.group import PROJECT_MEMBER | ||
from dsp_permissions_scripts.models.group import SYSTEM_ADMIN | ||
from dsp_permissions_scripts.models.group import UNKNOWN_USER | ||
from dsp_permissions_scripts.models.group import BuiltinGroup | ||
from dsp_permissions_scripts.models.group import GroupType | ||
|
||
|
||
def _get_sort_pos_of_custom_group(prefixed_iri: str) -> int: | ||
alphabet = list("abcdefghijklmnopqrstuvwxyz") | ||
relevant_letter = prefixed_iri.split(":")[-1][0] | ||
return alphabet.index(relevant_letter.lower()) + 99 # must be higher than the highest index of the builtin groups | ||
|
||
|
||
def sort_groups(groups_original: Iterable[GroupType]) -> list[GroupType]: | ||
""" | ||
Sorts groups: | ||
- First according to their power (most powerful first - only applicable for built-in groups) | ||
- Then alphabetically (custom groups) | ||
""" | ||
sort_key = [SYSTEM_ADMIN, CREATOR, PROJECT_ADMIN, PROJECT_MEMBER, KNOWN_USER, UNKNOWN_USER] | ||
groups = list(groups_original) | ||
groups.sort( | ||
key=lambda x: sort_key.index(x) | ||
if isinstance(x, BuiltinGroup) | ||
else _get_sort_pos_of_custom_group(x.prefixed_iri) | ||
) | ||
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