Textlab API

Apps

class integreat_cms.textlab_api.apps.TextlabApiConfig(app_name, app_module)[source]

Bases: AppConfig

Textlab api config inheriting the django AppConfig

__init__(app_name, app_module)[source]
classmethod create(entry)[source]

Factory that creates an app config from an entry in INSTALLED_APPS.

default_auto_field[source]
get_model(model_name, require_ready=True)[source]

Return the model with the given case-insensitive model_name.

Raise LookupError if no model exists with this name.

get_models(include_auto_created=False, include_swapped=False)[source]

Return an iterable of models.

By default, the following models aren’t included:

  • auto-created models for many-to-many relations without an explicit intermediate table,

  • models that have been swapped out.

Set the corresponding keyword argument to True to include such models. Keyword arguments aren’t documented; they’re a private API.

import_models()[source]
name: Final[str] = 'integreat_cms.textlab_api'[source]

Full Python path to the application

ready() None[source]

Checking if api is available

Return type:

None

verbose_name: Final[Promise] = 'TextLab API'[source]

Human-readable name for the application

Textlab API Client

class integreat_cms.textlab_api.textlab_api_client.TextlabClient(username: str, password: str)[source]

Bases: object

Client for the textlab api. Supports login and hix-score retrieval.

A detailed API documentation can be found at https://comlab-ulm.github.io/swagger-V8/

Parameters:
  • username (str)

  • password (str)

__init__(username: str, password: str) None[source]
Parameters:
  • username (str)

  • password (str)

Return type:

None

benchmark(text: str, text_type: int = 420) TextlabResult[source]

Retrieves the hix score of the given text.

Parameters:
  • text (str) – The text to calculate the score for

  • text_type (int) – The id of the text type (“Textsorte”) or, in terms of the api, benchmark to query. A benchmark is a pre-defined set of modules producing various metrics. They can have threshold/target values, depending on the type of text the benchmark is trying to represent. The available text types activated for the logged in account can be queried by sending a simple GET request to the /benchmark endpoint, complete with all metrics that get included for it. You can find the not so helpful API “documentation” here: https://comlab-ulm.github.io/swagger-V8/ But since for now we are only interested in the HIX score anyway, we just use the benchmark “Letter Demo Integreat” with ID 420 by default.

Returns:

The textlab result including score and feedback, or None if an error occurred

Return type:

TextlabResult

login() None[source]

Authorizes for the textlab api. On success, sets the token attribute.

Raises:

urllib.error.HTTPError – If the login was not successful

Return type:

None

static post_request(path: str, data: dict[str, str], auth_token: str | None = None) dict[str, Any][source]

Sends a request to the api.

Parameters:
  • path (str) – The api path

  • data (dict[str, str]) – The data to send

  • auth_token (str | None) – The authorization token to use

Returns:

The response json dictionary

Raises:

urllib.error.HTTPError – If the request failed

Return type:

dict[str, Any]

class integreat_cms.textlab_api.textlab_api_client.TextlabResult[source]

Bases: TypedDict

The result that is returned from the textlab api via benchmark.

__init__(*args, **kwargs)[source]
clear() None.  Remove all items from D.[source]
copy() a shallow copy of D[source]
feedback: list[dict[str, Any]][source]
fromkeys(value=None, /)[source]

Create a new dictionary with keys from iterable and values set to value.

get(key, default=None, /)[source]

Return the value for key if key is in the dictionary, else default.

items() a set-like object providing a view on D's items[source]
keys() a set-like object providing a view on D's keys[source]
pop(k[, d]) v, remove specified key and return the corresponding value.[source]

If the key is not found, return the default if given; otherwise, raise a KeyError.

popitem()[source]

Remove and return a (key, value) pair as a 2-tuple.

Pairs are returned in LIFO (last-in, first-out) order. Raises KeyError if the dict is empty.

score: float | None[source]
setdefault(key, default=None, /)[source]

Insert key with a value of default if key is not in the dictionary.

Return the value for key if key is in the dictionary, else default.

update([E, ]**F) None.  Update D from dict/iterable E and F.[source]

If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

values() an object providing a view on D's values[source]

Utils

This module contains helpers for the TextLab API client

integreat_cms.textlab_api.utils.check_hix_score(request: HttpRequest, source_translation: EventTranslation | PageTranslation | POITranslation, show_message: bool = True) bool[source]

Check whether the required HIX score is met and it is not ignored

Parameters:
Returns:

Whether the HIX constraints are valid

Return type:

bool

integreat_cms.textlab_api.utils.dict_path(data: dict, path: list[str]) Any[source]

Resolves a path in the given data dictionary and returns the value :param data: The data dictionary to get the value from :param path: The path to lookup :return: The result of the lookup in the dictionary

Parameters:
Return type:

Any

integreat_cms.textlab_api.utils.format_hix_feedback(response: dict) list[dict[str, Any]][source]

Format HIX feedback from Textlab, so it can be well handled in the front end

Parameters:

response (dict) – The response from the Textlab

Returns:

count for each feedback category

Return type:

list[dict[str, Any]]