Utils
This package contains utilities shared among all Django apps
Decorators
This file contains decorators that are used in core.
Env
Helpers for reading typed settings from environment variables.
Machine Translation API Client
This module contains utilities for machine translation API clients
- class integreat_cms.core.utils.machine_translation_api_client.MachineTranslationApiClient(request: HttpRequest, form_class: ModelFormMetaclass)[source]
Bases:
ABCA base class for API clients interacting with machine translation APIs
- Parameters:
request (HttpRequest)
form_class (ModelFormMetaclass)
- __init__(request: HttpRequest, form_class: ModelFormMetaclass) None[source]
Constructor initializes the class variables
- Parameters:
request (HttpRequest) – The current request
form_class (ModelFormMetaclass) – The
CustomContentModelFormsubclass of the current content type
- Return type:
None
- failed_because_exceeds_limit: list[str] = [][source]
Content objects untranslatable due to insufficient translation budget
- failed_because_insufficient_hix_score: list[str] = [][source]
Content objects untranslatable due to too-low hix score
- failed_because_no_changes_made: list[str] = [][source]
Content objects untranslatable since no actual changes were made
- failed_because_no_source_translation: list[str] = [][source]
Content objects untranslatable due to missing source translations
- failed_because_too_long_text: list[str] = [][source]
Content objects untranslatable due to too long text (only applies for push notification translation)
- filter_exceeds_limit(context: list[TranslationContext]) list[TranslationContext][source]
This method filters out entries from the context list if translating them would exceed the translation budget.
The removed entries are stored in order to show users batched error messages after all objects have been handled.
- Parameters:
context (list[TranslationContext]) – The list of translation contexts to filter
- Returns:
The filtered list of translation contexts
- Return type:
- filter_insufficient_hix_score(context: list[TranslationContext]) list[TranslationContext][source]
This method filters out entries from the context list if they do not have the required HIX score.
The removed entries are stored in order to show users batched error messages after all objects have been handled.
- Parameters:
context (list[TranslationContext]) – The list of translation contexts to filter
- Returns:
The filtered list of translation contexts
- Return type:
- filter_no_source_translation(context: list[TranslationContext]) list[TranslationContext][source]
This method filters out entries from the context list if they do not have the required source translation.
The removed entries are stored in order to show users batched error messages after all objects have been handled.
- Parameters:
context (list[TranslationContext]) – The list of translation contexts to filter
- Returns:
The filtered list of translation contexts
- Return type:
- filter_unchanged_translations(context: list[TranslationContext]) list[TranslationContext][source]
This method filters out entries from the context list if there have been no changes made to the source_translation.
If there are no changes but the target translation is outdated, a new translation version is created with
minor_edit=Trueto refresh its status without flagging it as a content change, instead of being recorded as a plain “no changes made” failure.Push notifications are excluded from the refresh case because they do not use
AbstractContentTranslationand therefore have no outdated status to refresh.The removed entries are stored in order to show users batched messages after all objects have been handled.
- Parameters:
context (list[TranslationContext]) – The list of translation contexts to filter
- Returns:
The filtered list of translation contexts
- Return type:
- form_class: ModelFormMetaclass[source]
- abstractmethod static get_target_language_key(target_language: Language) str[source]
This function decides the correct target language key Needs to be implemented by subclasses of MachineTranslationApiClient.
- abstractmethod invoke_translation_api(context: list[TranslationContext]) None[source]
Translate all ctx.instance stored in context. Needs to be implemented by subclasses of MachineTranslationApiClient.
- Parameters:
context (list[TranslationContext])
- Return type:
None
- mark_successful(ctx: TranslationContext) None[source]
Mark a content object as having been translated successfully
- Parameters:
ctx (TranslationContext)
- Return type:
None
- mark_too_long(ctx: TranslationContext, errors: ErrorDict | Exception) None[source]
Mark a translation as too long
- Parameters:
ctx (TranslationContext)
errors (ErrorDict | Exception)
- Return type:
None
- mark_unsuccessful(ctx: TranslationContext, errors: ErrorDict | Exception) None[source]
Mark a translation as unsuccessful (usually due to API errors)
- Parameters:
ctx (TranslationContext)
errors (ErrorDict | Exception)
- Return type:
None
- prepare_content_objects() list[TranslationContext][source]
Wrap each content object in a
TranslationContextand populate it with pre-computed translation metadata (source/target translations, translatable attributes, word count).- Returns:
A list of
TranslationContextinstances ready for filtering- Return type:
- request: HttpRequest[source]
The current request
- reset() None[source]
A single instance of a MachineTranslationApiClient may be used multiple times in succession, requiring a reset of the stored results and failures.
Forgetting to call this function will not result in additional machine translation budget use, but will produce nonsense messages shown to the user.
- Return type:
None
- save_translation(ctx: TranslationContext, translation_data: dict) None[source]
Create a translation form based on the extracted content object data, save it, and validate the translation.
- Parameters:
ctx (TranslationContext)
translation_data (dict)
- Return type:
None
- successful_translations: list[TranslationContext] = [][source]
Successful translations
- translate_object(obj: AbstractContentModel, language_slug: str) None[source]
This function translates one content object
- Parameters:
obj (AbstractContentModel) – The content object
language_slug (str) – The target language slug
- Return type:
None
- translate_queryset(queryset: list[AbstractContentModel], language_slug: str) None[source]
This function translates a content queryset via the configured machine translation provider.
Content objects are wrapped into
TranslationContextinstances, filtered for translatability, and then passed to the provider-specific translation API.- Parameters:
queryset (list[AbstractContentModel]) – The content QuerySet
language_slug (str) – The target language slug
- Return type:
None
- class integreat_cms.core.utils.machine_translation_api_client.TranslationContext(instance: 'AbstractContentModel', source_translation: 'AbstractContentTranslation | None' = None, existing_target_translation: 'AbstractContentTranslation | None' = None, translatable_attributes: 'list[tuple[str, str]]'=<factory>, word_count: 'int' = 0)[source]
Bases:
object- Parameters:
instance (AbstractContentModel)
source_translation (AbstractContentTranslation | None)
existing_target_translation (AbstractContentTranslation | None)
word_count (int)
- __init__(instance: AbstractContentModel, source_translation: AbstractContentTranslation | None = None, existing_target_translation: AbstractContentTranslation | None = None, translatable_attributes: list[tuple[str, str]]=<factory>, word_count: int = 0) None[source]
- Parameters:
instance (AbstractContentModel)
source_translation (AbstractContentTranslation | None)
existing_target_translation (AbstractContentTranslation | None)
word_count (int)
- Return type:
None
Machine Translation Celery Task
- integreat_cms.core.utils.machine_translation_celery_task.get_mt_task_ids(content_type: str, object_ids: list[int], language_slugs: list[str]) dict[ObjectIdAndLanguageSlug, str | None][source]
Batch-resolve the machine translation task IDs for every (object_id, language_slug) pair in one cache round trip. The returned dict contains an entry for every requested pair:
A value of None means no MT task is currently running for that pair, a string value is the Celery task id.
- integreat_cms.core.utils.machine_translation_celery_task.start_machine_translations(request: HttpRequest, content_objects: list[AbstractContentModel], language_slugs: list[str]) None[source]
Machine translate the given content objects into the given languages. Content types in
CONTENT_TYPESare queued as Celery task, all others are translated synchronously.- Parameters:
request (HttpRequest) – The current request
content_objects (list[AbstractContentModel]) – The content objects to translate
language_slugs (list[str]) – The slugs of the target languages
- Return type:
None
Machine Translation Provider
This module contains utilities for machine translations
- class integreat_cms.core.utils.machine_translation_provider.MachineTranslationProvider[source]
Bases:
objectA base class for machine translation providers. It should be used as static class, without instantiating it.
- classmethod is_enabled(region: Region, language: Language) bool[source]
Whether this provider is enabled for a given region and language. Call this from the parent class.
- is_needed(queryset: QuerySet[Event | Page | Place], target_language: Language) list[Event | Page | Place][source]
Checks if a machine translation is needed, thus checking if the translation status is UP_TO_DATE or MACHINE_TRANSLATED and then returns a lit of translations which are to be updated
- static is_permitted(region: Region, user: SimpleLazyObject, content_type: ModelBase) bool[source]
Checks if a machine translation is permitted, i.e. if for the given region, MT of the given content type is allowed and MT into the target language is enabled for the requesting user.
Strtobool
General utility functions that are used by multiple modules within our project.