Skip to content

oarepo_config.detail_sidebar

oarepo_config.detail_sidebar

Configuration of the boxes in the record detail page sidebar.

DETAIL_SIDEBAR_TEMPLATES: dict[str, str] = {'manage_menu': 'invenio_app_rdm/records/details/side_bar/manage_menu.html', 'versions': 'invenio_app_rdm/records/details/side_bar/versions.html', 'external_resources': 'invenio_app_rdm/records/details/side_bar/external_resources.html', 'communities': 'invenio_app_rdm/records/details/side_bar/communities.html', 'keywords_subjects': 'invenio_app_rdm/records/details/side_bar/keywords_subjects.html', 'details': 'invenio_app_rdm/records/details/side_bar/details.html', 'locations': 'invenio_app_rdm/records/details/side_bar/locations.html', 'licenses': 'invenio_app_rdm/records/details/side_bar/licenses.html', 'citations': 'invenio_app_rdm/records/details/side_bar/citations.html', 'export': 'oarepo_ui/record_detail/side_bar/export.html', 'technical_metadata': 'invenio_app_rdm/records/details/side_bar/technical_metadata.html', 'metrics': 'invenio_app_rdm/records/details/side_bar/metrics.html', 'ark': 'oarepo_ark/ark_identifier.html'} module-attribute

Symbolic names of the known sidebar boxes and their templates.

DEFAULT_DETAIL_SIDEBAR = ['manage_menu', 'versions', 'external_resources', 'communities', 'keywords_subjects', 'details', 'licenses', 'citations', 'export', 'metrics', 'technical_metadata'] module-attribute

Boxes set by configure_ui, in this order.

update_detail_sidebar(current: Sequence[str], *, known: Mapping[str, str] | None = None, templates: Mapping[str, str] | None = None, order: Sequence[str] | None = None, move: Sequence[str] | None = None, remove: Sequence[str] | None = None) -> list[str]

Return current (a list of template paths) with the changes applied.

known maps box names to templates of current (e.g. as registered by earlier calls), taking precedence over DETAIL_SIDEBAR_TEMPLATES; templates that have no name are named by their path.

Source code in oarepo_config/detail_sidebar.py
def update_detail_sidebar(  # noqa: PLR0913
    current: Sequence[str],
    *,
    known: Mapping[str, str] | None = None,
    templates: Mapping[str, str] | None = None,
    order: Sequence[str] | None = None,
    move: Sequence[str] | None = None,
    remove: Sequence[str] | None = None,
) -> list[str]:
    """Return `current` (a list of template paths) with the changes applied.

    `known` maps box names to templates of `current` (e.g. as registered by earlier
    calls), taking precedence over
    [`DETAIL_SIDEBAR_TEMPLATES`][oarepo_config.detail_sidebar.DETAIL_SIDEBAR_TEMPLATES];
    templates that have no name are named by their path.
    """
    # mapping of box name to template
    boxes = _name_boxes(current, known or {})

    for name, path in (templates or {}).items():
        # one template under two names would render the same box twice
        if other := next((n for n, p in boxes.items() if p == path and n != name), None):
            raise ValueError(f"Sidebar template '{path}' of box '{name}' is already used by box '{other}'")
        boxes[name] = path  # replaces in place, a new name is appended
    if order is not None:
        boxes = _reorder(boxes, order)
    for spec in move or ():
        boxes = _move(boxes, spec)
    for name in remove or ():
        _check_box(boxes, name)
        del boxes[name]
    return list(boxes.values())

configure_detail_sidebar(*, templates: dict[str, str] | None = None, order: list[str] | None = None, move: list[str] | None = None, remove: list[str] | None = None, model: str | None = None) -> None

Change the boxes shown in the sidebar of the record detail page.

The boxes are referred to by symbolic names (see DETAIL_SIDEBAR_TEMPLATES): manage_menu, versions, external_resources, communities, keywords_subjects, details, locations, licenses, citations, export, technical_metadata, metrics and ark (added by configure_ark). A box registered via templates is referred to by its new name.

The changes are applied to the current sidebar, in this order: templates, order, move, remove. Must be called after configure_ui and any other configure_* function that adds a box (e.g. configure_ark). Can be called multiple times.

Parameters:

Name Type Description Default
templates dict[str, str] | None

Box name -> template. An existing box gets the new template in place; a new box is appended at the end.

None
order list[str] | None

The new order of the boxes. "..." stands for all the boxes not listed, in their current order; without it, the boxes not listed are removed.

None
move list[str] | None

Moves applied one after another, each one of "<box> before <box>", "<box> after <box>", "<box> first" or "<box> last".

None
remove list[str] | None

Boxes to remove.

None
model str | None

Change only the sidebar of this model (the model name). Its sidebar starts as a copy of the global one.

None

Invenio configuration variables set:

  • APP_RDM_DETAIL_SIDE_BAR_TEMPLATES - without model.
  • OAREPO_UI_SIDEBAR_TEMPLATES[model] - with model.
  • _OAREPO_CONFIG_DETAIL_SIDEBAR_TEMPLATES - internal: the boxes registered via templates, per sidebar (model name, None for the global one), so that later calls know their names.
Example
config.configure_ui(...)

config.configure_detail_sidebar(
    templates={
        "locations": "datasets/record_detail/side_bar/locations.html",
        "licenses": "datarepo/records/details/side_bar/licenses.html",
    },
    move=["locations after details"],
)
Source code in oarepo_config/detail_sidebar.py
def configure_detail_sidebar(
    *,
    templates: dict[str, str] | None = None,
    order: list[str] | None = None,
    move: list[str] | None = None,
    remove: list[str] | None = None,
    model: str | None = None,
) -> None:
    """Change the boxes shown in the sidebar of the record detail page.

    The boxes are referred to by symbolic names (see
    [`DETAIL_SIDEBAR_TEMPLATES`][oarepo_config.detail_sidebar.DETAIL_SIDEBAR_TEMPLATES]):
    `manage_menu`, `versions`, `external_resources`, `communities`,
    `keywords_subjects`, `details`, `locations`, `licenses`,
    `citations`, `export`, `technical_metadata`, `metrics` and `ark`
    (added by [`configure_ark`][oarepo_config.configure_ark]). A box
    registered via `templates` is referred to by its new name.

    The changes are applied to the current sidebar, in this order:
    `templates`, `order`, `move`, `remove`. Must be called *after*
    [`configure_ui`][oarepo_config.configure_ui] and any other `configure_*`
    function that adds a box (e.g. [`configure_ark`][oarepo_config.configure_ark]).
    Can be called multiple times.

    Args:
        templates: Box name -> template. An existing box gets the new
            template in place; a new box is appended at the end.
        order: The new order of the boxes. `"..."` stands for all the
            boxes not listed, in their current order; without it, the boxes
            not listed are removed.
        move: Moves applied one after another, each one of
            `"<box> before <box>"`, `"<box> after <box>"`,
            `"<box> first"` or `"<box> last"`.
        remove: Boxes to remove.
        model: Change only the sidebar of this model (the model name). Its
            sidebar starts as a copy of the global one.

    Invenio configuration variables set:

    * `APP_RDM_DETAIL_SIDE_BAR_TEMPLATES` - without `model`.
    * `OAREPO_UI_SIDEBAR_TEMPLATES[model]` - with `model`.
    * `_OAREPO_CONFIG_DETAIL_SIDEBAR_TEMPLATES` - internal: the boxes registered via
      `templates`, per sidebar (model name, `None` for the global one),
      so that later calls know their names.

    Example:
        ```python
        config.configure_ui(...)

        config.configure_detail_sidebar(
            templates={
                "locations": "datasets/record_detail/side_bar/locations.html",
                "licenses": "datarepo/records/details/side_bar/licenses.html",
            },
            move=["locations after details"],
        )
        ```

    """
    sidebar = get_constant_from_caller("APP_RDM_DETAIL_SIDE_BAR_TEMPLATES")
    if sidebar is None:
        raise ValueError(
            "APP_RDM_DETAIL_SIDE_BAR_TEMPLATES must be set, please configure UI before configuring the sidebar."
        )
    # names registered by earlier calls, per sidebar (None is the global one)
    registered = dict(get_constant_from_caller("_OAREPO_CONFIG_DETAIL_SIDEBAR_TEMPLATES", {}))
    model_sidebars = dict(get_constant_from_caller("OAREPO_UI_SIDEBAR_TEMPLATES", {}))
    known = registered.get(None, {})
    if model is not None and model in model_sidebars:
        sidebar = model_sidebars[model]
        known = registered.get(model, {})

    # known must not contain this call's templates yet: the current sidebar is named by the
    # earlier registrations, so that a re-registered box is replaced, not added twice
    sidebar = update_detail_sidebar(sidebar, known=known, templates=templates, order=order, move=move, remove=remove)
    registered[model] = {**known, **(templates or {})}

    constants: dict[str, object] = {"_OAREPO_CONFIG_DETAIL_SIDEBAR_TEMPLATES": registered}
    if model is None:
        constants["APP_RDM_DETAIL_SIDE_BAR_TEMPLATES"] = sidebar
    else:
        model_sidebars[model] = sidebar
        constants["OAREPO_UI_SIDEBAR_TEMPLATES"] = model_sidebars
    set_constants_in_caller(constants)