generated from MinBZK/python-project-template
-
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
26 changed files
with
933 additions
and
492 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
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,43 @@ | ||
from collections.abc import Callable | ||
from enum import Enum | ||
from typing import TypeVar | ||
|
||
from fastapi import Request | ||
|
||
from amt.core.internationalization import get_requested_language, get_supported_translation, get_translation | ||
from amt.schema.localized_value_item import LocalizedValueItem | ||
|
||
T = TypeVar("T", bound="LocalizableEnum", covariant=True) | ||
|
||
|
||
class LocalizableEnum(Enum): | ||
@property | ||
def index(self) -> int: | ||
return list(self.__class__).index(self) | ||
|
||
def localize(self, language: str) -> LocalizedValueItem: | ||
translations = get_translation(get_supported_translation(language)) | ||
_ = translations.gettext | ||
display_values = self.get_display_values(_) | ||
return LocalizedValueItem(value=self.name, display_value=display_values[self]) | ||
|
||
@classmethod | ||
def get_display_values(cls: type[T], _: Callable[[str], str]) -> dict[T, str]: | ||
raise NotImplementedError("Subclasses must implement this method") | ||
|
||
|
||
def get_localized_enum(key: LocalizableEnum | None, request: Request) -> LocalizedValueItem | None: | ||
""" | ||
Given the key and translation, returns the translated text. | ||
:param key: the key | ||
:param request: request to get the current language | ||
:return: a LocalizedValueItem with the correct translation | ||
""" | ||
if key is None: | ||
return None | ||
|
||
return key.localize(get_requested_language(request)) | ||
|
||
|
||
def get_localized_enums(enum_class: type[T], request: Request) -> list[LocalizedValueItem | None]: | ||
return [get_localized_enum(enum_value, request) for enum_value in enum_class] |
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 |
---|---|---|
@@ -1,47 +1,38 @@ | ||
import logging | ||
from enum import Enum | ||
from collections.abc import Callable | ||
|
||
from fastapi import Request | ||
|
||
from amt.core.internationalization import get_current_translation | ||
from amt.schema.publication_category import PublicationCategory | ||
from ..schema.localized_value_item import LocalizedValueItem | ||
from .localizable import LocalizableEnum, get_localized_enum, get_localized_enums | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class PublicationCategories(Enum): | ||
class PublicationCategories(LocalizableEnum): | ||
IMPACTVOL_ALGORITME = "impactvol algoritme" | ||
NIET_IMPACTVOL_ALGORITME = "niet-impactvol algoritme" | ||
HOOG_RISICO_AI = "hoog-risico AI" | ||
GEEN_HOOG_RISICO_AI = "geen hoog-risico AI" | ||
VERBODEN_AI = "verboden AI" | ||
UITZONDERING_VAN_TOEPASSING = "uitzondering van toepassing" | ||
|
||
@classmethod | ||
def get_display_values( | ||
cls: type["PublicationCategories"], _: Callable[[str], str] | ||
) -> dict["PublicationCategories", str]: | ||
return { | ||
cls.IMPACTVOL_ALGORITME: _("Impactful algorithm"), | ||
cls.NIET_IMPACTVOL_ALGORITME: _("Non-impactful algorithm"), | ||
cls.HOOG_RISICO_AI: _("High-risk AI"), | ||
cls.GEEN_HOOG_RISICO_AI: _("No high-risk AI"), | ||
cls.VERBODEN_AI: _("Forbidden AI"), | ||
cls.UITZONDERING_VAN_TOEPASSING: _("Exception of application"), | ||
} | ||
|
||
|
||
def get_localized_publication_category( | ||
key: PublicationCategories | None, request: Request | ||
) -> LocalizedValueItem | None: | ||
return get_localized_enum(key, request) | ||
|
||
|
||
def get_publication_category(key: PublicationCategories | None, request: Request) -> PublicationCategory | None: | ||
""" | ||
Given the key and translation, returns the translated text. | ||
:param key: the key | ||
:param request: request to get the current language | ||
:return: a Publication Category model with the correct translation | ||
""" | ||
|
||
if key is None: | ||
return None | ||
|
||
translations = get_current_translation(request) | ||
_ = translations.gettext | ||
# translations are determined at runtime, which is why we use the dictionary below | ||
keys = { | ||
PublicationCategories.IMPACTVOL_ALGORITME: _("Impactful algorithm"), | ||
PublicationCategories.NIET_IMPACTVOL_ALGORITME: _("Non-impactful algorithm"), | ||
PublicationCategories.HOOG_RISICO_AI: _("High-risk AI"), | ||
PublicationCategories.GEEN_HOOG_RISICO_AI: _("No high-risk AI"), | ||
PublicationCategories.VERBODEN_AI: _("Forbidden AI"), | ||
PublicationCategories.UITZONDERING_VAN_TOEPASSING: _("Exception of application"), | ||
} | ||
return PublicationCategory(id=key.name, name=keys[key]) | ||
|
||
|
||
def get_publication_categories(request: Request) -> list[PublicationCategory | None]: | ||
return [get_publication_category(p, request) for p in PublicationCategories] | ||
def get_localized_publication_categories(request: Request) -> list[LocalizedValueItem | None]: | ||
return get_localized_enums(PublicationCategories, request) |
Oops, something went wrong.