Utils

Contact Utils

integreat_cms.cms.views.utils.contact_utils.generate_primary_contact_from_place(website: str, phone_number: str, email: str, place: Place, language: Language, region: Region, title: str) → None[source]
Parameters:
Return type:

None

integreat_cms.cms.views.utils.contact_utils.get_contact(request: HttpRequest, contact_id: int, region_slug: str | None = None) → str[source]

Retrieves the rendered HTML representation of a contact.

Parameters:
  • request (HttpRequest) – The current request

  • contact_id (int) – The ID of the contact to retrieve

  • region_slug (str | None) – The slug of the current region

Returns:

HTML representation of the requested contact

Return type:

str

integreat_cms.cms.views.utils.contact_utils.get_contact_raw(request: HttpRequest, contact_id: int, region_slug: str | None = None) → str[source]

Retrieves the short representation of a single contact, and returns it in the same format as a contact search single completion.

Parameters:
  • request (HttpRequest) – The current request

  • contact_id (int) – The ID of the contact to retrieve

  • region_slug (str | None) – The slug of the current region

Returns:

Short representation of the requested contact

Return type:

str

integreat_cms.cms.views.utils.contact_utils.search_contact_ajax(request: HttpRequest, region_slug: str | None = None) → JsonResponse[source]

Searches contacts that match the search query.

Parameters:
  • request (HttpRequest) – The current request

  • region_slug (str | None) – The slug of the current region

Raises:

PermissionDenied – If the user has no permission to the object type

Returns:

Json object containing all matching elements, of shape {title: str, url: str, type: str}

Return type:

JsonResponse

Content Edit Lock

integreat_cms.cms.views.utils.content_edit_lock.MT_SUPPORTED_LOCK_TYPES = {'Page': <class 'integreat_cms.cms.models.pages.page.Page'>}[source]

The heartbeat endpoint handles all content types, but MT progress checking is currently only supported for pages. This maps the content type string (type(self).__name__ from AbstractContentModel.edit_lock_key) to its model class, acting as an allowlist — types not listed here skip the MT check entirely.

{'Page': <class 'integreat_cms.cms.models.pages.page.Page'>}
integreat_cms.cms.views.utils.content_edit_lock.content_edit_lock_heartbeat(request: HttpRequest, region_slug: str | None = None) → JsonResponse[source]

This function handles heartbeat requests. When a heartbeat is received, this function tries to extend the lock for a user who is editing some content.

Parameters:
  • request (HttpRequest) – The current request

  • region_slug (str | None)

Returns:

Json object containing success: true if the lock could be acquired

Return type:

JsonResponse

integreat_cms.cms.views.utils.content_edit_lock.content_edit_lock_release(request: HttpRequest, region_slug: str | None = None) → JsonResponse[source]

This function handles unlock requests

Parameters:
  • request (HttpRequest) – The current request

  • region_slug (str | None)

Returns:

Json object containing success: true if the content object could be unlocked

Return type:

JsonResponse

Hix

This file contains functionality to communicate with the Textlab api to get the hix-value for a given text.

exception integreat_cms.cms.views.utils.hix.CacheMeIfYouCan[source]

Bases: Exception

Helper exception used for stopping @lru_cache from caching

__init__(*args, **kwargs)[source]
add_note(object, /)[source]

Exception.add_note(note) – add a note to the exception

args[source]
with_traceback(object, /)[source]

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

integreat_cms.cms.views.utils.hix.get_hix_score(request: HttpRequest, **kwargs: Any) → JsonResponse[source]

Calculates the hix score for the param ‘text’ in the request body and returns it.

Parameters:
  • request (HttpRequest) – The request

  • kwargs (Any)

Returns:

A json response, of {“score”: value} in case of success

Return type:

JsonResponse

integreat_cms.cms.views.utils.hix.get_translation_over_hix_threshold(region: Region) → QuerySet[source]

Filter page translations which are over the hix threshold

Parameters:

region (Region) – The region for which to get all of the translations

Return type:

QuerySet

integreat_cms.cms.views.utils.hix.get_translation_under_hix_threshold(region: Region) → QuerySet[source]

Filter page translations which are under the hix threshold

Parameters:

region (Region) – The region for which to get all of the translations

Return type:

QuerySet

integreat_cms.cms.views.utils.hix.get_translations_relevant_to_hix(region: Region) → QuerySet[source]

Get page translations for a region sorted by hix score.

Parameters:

region (Region) – The region for which to get all of the translations

Return type:

QuerySet

integreat_cms.cms.views.utils.hix.lookup_hix_score(text: str) → TextlabResult | None[source]

This function returns the hix score for the given text. It either performs an api request or returns the value from cache.

Parameters:

text (str) – The text to calculate the hix score for

Returns:

The score for the given text

Return type:

TextlabResult | None

integreat_cms.cms.views.utils.hix.lookup_hix_score_helper(text: str) → TextlabResult[source]

This function returns the hix score for the given text. It either performs an api request or returns the value from cache, unless it is None, in which case an exception is raised to prevent caching.

Parameters:

text (str) – The text to calculate the hix score for

Returns:

The score for the given text

Return type:

TextlabResult

Machine Translation Progress

This module contains the API call handler for polling the progress of an asynchronous machine translation Celery task.

integreat_cms.cms.views.utils.machine_translation_progress.get_machine_translation_task_progress(request: HttpRequest, region_slug: str, model_type: Literal['page', 'event', 'place', 'pushnotification'], task_id: str) → JsonResponse[source]

Report the current progress of an asynchronous machine translation Celery task, identified directly by its task id. Used by callers (e.g. the content list views) that already know which task governs a whole batch of objects, because the task id was resolved once at render time and grouped client-side. Every object in that batch shares the same task, so checking the task once is equivalent to - and cheaper than - checking each object.

Parameters:
  • request (HttpRequest) – The current request

  • region_slug (str) – The slug of the current region

  • model_type (Literal['page', 'event', 'place', 'pushnotification']) – The content type being translated (e.g. “page”)

  • task_id (str) – The id of the Celery task to check

Returns:

A JSON object describing the current translation progress

Return type:

JsonResponse

Machine Translation Report

This module contains the AJAX call for reading the machine translation report(s) queued for the current user, region, and content type.

integreat_cms.cms.views.utils.machine_translation_report.get_machine_translation_report(request: HttpRequest, region_slug: str, language_slug: str, model_type: str) → JsonResponse[source]

Report the machine translation report(s) queued for the current user, region, and content type, if any - a destructive read, so each report is only ever returned once, to whoever triggered the translation.

Parameters:
  • request (HttpRequest) – The current request

  • region_slug (str) – The slug of the current region

  • language_slug (str) – The slug of the current language (unused, kept for URL consistency with the other machine translation AJAX endpoints)

  • model_type (str) – The content type the reports concern (e.g. “page”)

Returns:

A JSON object containing the queued reports, if any

Return type:

JsonResponse

Machine Translations

This module contains the generalized function to build the AJAX call for the machine translation popup

integreat_cms.cms.views.utils.machine_translations.build_json_for_machine_translation(request: HttpRequest, region_slug: str, language_slug: str, model_type: Literal['page', 'event', 'place']) → JsonResponse[source]

This function collects the hix score and the amount of words per content entry from the source

Parameters:
  • request (HttpRequest) – The current request

  • language_slug (str) – The slug of the current language

  • model_type (Literal['page', 'event', 'place']) – The according model to the different content types

  • region_slug (str)

Returns:

A dictionary that contains the data for the machine translation popup (page id, title, amount of words and optional hix value)

Return type:

JsonResponse

Opening Hour

integreat_cms.cms.views.utils.opening_hour.get_open_hour_config_data(related_class: type[PlaceForm] | type[Contact], can_change_place: bool) → dict[source]
Parameters:
Return type:

dict

Publication Status

This file contains the helper function to change the publication status of translations

integreat_cms.cms.views.utils.publication_status.change_publication_status(request: HttpRequest, selected_content: QuerySet, language_slug: str, desired_status: str) → None[source]

Helper function to change the publication status

Parameters:
  • request (HttpRequest) – The current request

  • selected_content (QuerySet) – The current queryset

  • language_slug (str) – The language slug of the current language

  • desired_status (str) – The desired status

Return type:

None

Search Content Ajax

integreat_cms.cms.views.utils.search_content_ajax.search_content_ajax(request: HttpRequest, region_slug: str | None = None, language_slug: str | None = None) → JsonResponse[source]

Searches all places, events and pages for the current region and returns all that match the search query. Results which match the query in the title or slug get ranked higher than results which only match through their text content.

Parameters:
  • request (HttpRequest) – The current request

  • language_slug (str) – language slug

  • region_slug (str | None)

Raises:
  • PermissionDenied – If the user has no permission to the object type

  • AttributeError – If the request contains an object type which is unknown or if the user has no permission for it

Returns:

Json object containing all matching elements, of shape {title: str, url: str, type: str}

Return type:

JsonResponse

Slugify Ajax

integreat_cms.cms.views.utils.slugify_ajax.slugify_ajax(request: HttpRequest, region_slug: str, language_slug: str, model_type: Literal['page', 'event', 'place']) → JsonResponse[source]

checks the current user input for title and generates unique slug for permalink

Parameters:
  • request (HttpRequest) – The current request

  • language_slug (str) – language slug

  • model_type (Literal['page', 'event', 'place']) – The type of model to generate a unique slug for, one of event|page|place

  • region_slug (str)

Returns:

unique translation slug

Raises:
  • PermissionDenied – If the user does not have the permission to access this function

  • Http404 – If the given model_id is invalid or does not exist in the current region

Return type:

JsonResponse

Translation Coverage

integreat_cms.cms.views.utils.translation_coverage.get_translation_and_word_count(region: Region) → tuple[dict[Language, Counter], dict[Language, Counter]][source]

This function counts the translations and words of a region

Parameters:

region (Region) – The region for which we want to count the translation and word count

Returns:

The translation and word count in a tuple

Return type:

tuple[dict[Language, Counter], dict[Language, Counter]]

User

integreat_cms.cms.views.utils.user.get_pages_observer_has_access_to(user: User) → list[source]

This function checks which pages a user of a region has access to.

Parameters:

user (User) – The user for which the access rights are checked

Return type:

list