Plug in API Object - czcorpus/kontext GitHub Wiki

# Plug-ins / Plug-in API object (kontext.PluginApi)

Please note that the PluginApi object lifetime is delimited by a single request. It means that there can be no permanent 1:1 relationship between plug-in object and PluginApi object. PluginApi objects are typically injected via plug-in's exposed methods used by the controller.

# PluginApi.set_shared(key, value)

Put a data item into a request-scope shared storage. This is the only way how plug-ins can exchange data between themselves (remember - plug-in attributes/state cannot be used for this).

Example: ucnk_remote_auth3 loads a response from authentication service where the response contains not only user's credentials but also an HTML snippet to be displayed on pages. But the HTML handling task is actually performed by ucnk_toolbar3 plug-in. To prevent it from calling the API for the second time, auth plugin stores the HTML into the shared space and makes it available for application_bar plug-in.

# PluginApi.get_shared(key)

Get data from a request-scope shared storage. See set_shared for more details.

# PluginApi.get_from_environ(key, default=None)

Return a WSGI environment variable.

# PluginApi.redirect(url, code=303)

Set an HTTP redirect. Calling this method does not interrupt current action processing - it just defines an additional HTTP response header.

# PluginApi.cookies (@property)

Refers to the controller's KonTextCookie instance.

# PluginApi.session (@property)

Refers to the "session" object (see Werkzeug sessions).

# PluginApi.refresh_session_id()

Registers a request to change session_id. This is typically done once a user changes status (logged-in/logged-out) etc. The actual change is done after an action is processed.

# PluginApi.user_lang (@property)

Contains current user's language (languge + region form - e.g. en_US, cs_CZ)

# PluginApi.user_id (@property)

Contains a database ID of the current user

# PluginApi.user_is_anonymous (@property)

A bool attribute telling whether the current user is anonymous or not.

# PluginApi.current_corpus (@property)

Contains a current corpus (= manatee.Corpus) instance

# PluginApi.get_canonical_corpname(c)

Transform a general corpus name into a canonical one (e.g. public/europarl to europarl).

# PluginApi.text_types (@property)

Contains all the text types (= structural attributes and their respective values) for the current corpus.

# PluginApi.current_url (@property)

Contains current URL (including correct protocol and application path prefix).

# PluginApi.root_url (@property)

Contains application's root URL (including correct protocol and application path prefix).