Skip to content

Utility Functions

Utility functions are meant to be used in the extension codebase to perform some specific tasks. They are defined in the utils.py file of the extension.

Some utils functions could be useful for other extensions as well. You can use them by importing the ap_main.utils module.

import ckanext.admin_panel.utils as ap_utils

List of Utility Functions

ap_before_request()

Check if user has access to the admin panel.

Calls admin_panel_access auth function to check if user has access to the admin panel view. If you want to change the auth function logic, you can chain it.

RAISES DESCRIPTION
NotAuthorized

If user does not have access to the admin panel

Example
from flask import Blueprint, Response

from ckanext.ap_main.utils import ap_before_request

blueprint = Blueprint("my_blueprint", __name__, url_prefix="/admin-panel/my_blueprint")
blueprint.before_request(ap_before_request)
Source code in ckanext/ap_main/utils.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def ap_before_request() -> None:
    """Check if user has access to the admin panel.

    Calls `admin_panel_access` auth function to check if user has access to the
    admin panel view. If you want to change the auth function logic, you can chain it.

    Raises:
        tk.NotAuthorized: If user does not have access to the admin panel

    Example:
        ```python
        from flask import Blueprint, Response

        from ckanext.ap_main.utils import ap_before_request

        blueprint = Blueprint("my_blueprint", __name__, url_prefix="/admin-panel/my_blueprint")
        blueprint.before_request(ap_before_request)
        ```
    """
    try:
        tk.check_access(
            "admin_panel_access",
            {"user": tk.current_user.name},
        )
    except tk.NotAuthorized:
        tk.abort(403, tk._("Need to be system administrator to administer"))

get_all_renderers()

Get all registered column renderers.

A renderer is a function that takes a column value and can modify its appearance in a table.

RETURNS DESCRIPTION
dict[str, ColRenderer]

A mapping of renderer names to renderer functions

Source code in ckanext/ap_main/utils.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def get_all_renderers() -> dict[str, ap_types.ColRenderer]:
    """Get all registered column renderers.

    A renderer is a function that takes a column value and can modify its appearance
    in a table.

    Returns:
        A mapping of renderer names to renderer functions
    """
    if not _renderers_cache:
        for plugin in reversed(list(p.PluginImplementations(IAdminPanel))):
            for name, fn in plugin.get_col_renderers().items():
                _renderers_cache[name] = fn

    return _renderers_cache

get_config_schema(schema_id)

Get a schema by its id from the loaded schemas.

PARAMETER DESCRIPTION
schema_id

The id of the schema to get

TYPE: str

RETURNS DESCRIPTION
dict[Any, Any] | None

The schema if found, otherwise None

Source code in ckanext/ap_main/utils.py
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
def get_config_schema(schema_id: str) -> dict[Any, Any] | None:
    """Get a schema by its id from the loaded schemas.

    Args:
        schema_id: The id of the schema to get

    Returns:
        The schema if found, otherwise None
    """
    from ckanext.scheming.plugins import _load_schemas, _expand_schemas

    for _, schemas_paths in collect_config_schemas_signal.send():
        schemas = _load_schemas(schemas_paths, "schema_id")
        expanded_schemas = _expand_schemas(schemas)

        if schema := expanded_schemas.get(schema_id):
            return schema