Utils

This package contains utilities shared among all Django apps

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: ABC

A 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:
  • region – The current region

  • form_class (ModelFormMetaclass) – The CustomContentModelForm subclass of the current content type

  • request (HttpRequest) –

Return type:

None

check_usage(region: Region, source_translation: EventTranslation | PageTranslation | POITranslation) tuple[bool, int][source]

This function checks if the attempted translation would exceed the region’s word limit

Parameters:
Returns:

translation would exceed limit, region budget, attempted translation word count

Return type:

tuple[bool, int]

form_class: ModelFormMetaclass[source]

The CustomContentModelForm

region: Region[source]

The current region

request: HttpRequest[source]

The current request

translate_object(obj: Event | Page | POI, language_slug: str) None[source]

This function translates one content object

Parameters:
  • obj (Event | Page | POI) – The content object

  • language_slug (str) – The target language slug

Return type:

None

abstract translate_queryset(queryset: QuerySet[Event | Page | POI], language_slug: str) None[source]

Translate a given queryset into one specific language. Needs to be implemented by subclasses of MachineTranslationApiClient.

Parameters:
  • queryset (QuerySet[Event | Page | POI]) – The QuerySet of content objects to translate

  • language_slug (str) – The target language slug to translate into

Return type:

None

Machine Translation Provider

This module contains utilities for machine translations

class integreat_cms.core.utils.machine_translation_provider.MachineTranslationProvider[source]

Bases: object

A base class for machine translation providers. It should be used as static class, without instantiating it.

api_client: type | None = None[source]

The API client class for this provider

bulk_only_for_staff: bool = False[source]

Whether to require the staff permission for bulk actions

enabled: bool = True[source]

Whether the provider is globally enabled

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.

Parameters:
  • region (Region) – The given region

  • language (Language) – The given language

Returns:

Wether this provider is enabled for the given region and language

Return type:

bool

is_needed(queryset: QuerySet[Event | Page | POI], target_language: Language) list[Event | Page | POI][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

Parameters:
  • queryset (QuerySet[Event | Page | POI]) – The content model which should be translated

  • target_language (Language) – The target language

Returns:

translations which need to be translated and updated

Return type:

list[Event | Page | POI]

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.

Parameters:
  • region (Region) – The current region

  • user (SimpleLazyObject) – The current user

  • content_type (ModelBase) – The content model which should be translated

Returns:

Whether the translation is permitted

Return type:

bool

name: str = ''[source]

The readable name for this provider

region_enabled_attr: str | None = None[source]

The name of the region attribute which denotes whether the provider is enabled in a region

supported_source_languages: list[str] = [][source]

The supported source languages

supported_target_languages: list[str] = [][source]

The supported target languages

class integreat_cms.core.utils.machine_translation_provider.MachineTranslationProviderType[source]

Bases: type

A meta class for machine translation providers

Strtobool

General utility functions that are used by multiple modules within our project.

integreat_cms.core.utils.strtobool.strtobool(val: str) bool[source]

Convert a string representation of truth to true (1) or false (0). True values are ‘y’, ‘yes’, ‘t’, ‘true’, ‘on’, and ‘1’; false values are ‘n’, ‘no’, ‘f’, ‘false’, ‘off’, and ‘0’. Raises ValueError if ‘val’ is anything else.

Parameters:

val (str) –

Return type:

bool