Skip to content

Commit

Permalink
custom_fields: allow to search vocab
Browse files Browse the repository at this point in the history
  • Loading branch information
Javier Romero Caatro authored and kpsherva committed Aug 5, 2024
1 parent bb785b1 commit bf5e3a7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,14 @@ <h2>{{ add_description.type.title_l10n }} <span
{% endfor %}
{% endmacro %}

{% macro list_vocabulary_values(values) %}
{% macro list_vocabulary_values(field, values) %}
{% if values.title_l10n is defined %}
{{ values.title_l10n }}
{% set search_url = field | custom_vocab_fields_search(values.title_l10n, "title") %}
{% if search_url %}
<a href="{{ search_url }}">{{ values.title_l10n | safe }}</a>
{% else %}
{{ values.title_l10n }}
{% endif %}
{% else %}
{% for value in values %}
{{ value.title_l10n }}{{ ", " if not loop.last }}
Expand Down Expand Up @@ -240,7 +245,7 @@ <h2>{{ add_description.type.title_l10n }} <span
{% else %}
<dt class="ui tiny header">
{{ field_cfg.props.label }}
{% set namespace_url = field_cfg.field | namespace_url %}
{% set namespace_url = field_cfg.display_url if field_cfg.display_url is defined else field_cfg.field | namespace_url %}
{% if namespace_url %}
<a href="{{ namespace_url }}" aria-label="{{ namespace_url }}">
<i class="icon external" aria-hidden="true"></i>
Expand All @@ -265,7 +270,7 @@ <h2>{{ add_description.type.title_l10n }} <span
{% endif %}
</dd>
{% elif field_cfg.is_vocabulary %}
<dd>{{ list_vocabulary_values(field_value) }}</dd>
<dd>{{ list_vocabulary_values(field_cfg.field, field_value) }}</dd>
{% elif field_value is iterable and field_value|length > 0 and field_value[0] is string %}
<dd>{{ list_string_values(field_cfg.field, field_value) }}</dd>
{% elif field_value is iterable and field_value|length > 0 and field_value[0] is number %}
Expand Down
2 changes: 2 additions & 0 deletions invenio_app_rdm/records_ui/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
can_list_files,
compact_number,
custom_fields_search,
custom_vocab_fields_search,
get_scheme_label,
has_images,
has_previewable_files,
Expand Down Expand Up @@ -184,6 +185,7 @@ def create_blueprint(app):
blueprint.add_app_template_filter(truncate_number)
blueprint.add_app_template_filter(namespace_url)
blueprint.add_app_template_filter(custom_fields_search)
blueprint.add_app_template_filter(custom_vocab_fields_search)
blueprint.add_app_template_filter(transform_record)

# Register context processor
Expand Down
20 changes: 20 additions & 0 deletions invenio_app_rdm/records_ui/views/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from babel.numbers import format_compact_decimal, format_decimal
from flask import current_app, url_for
from invenio_base.utils import obj_or_import_string
from invenio_i18n import get_locale
from invenio_previewer.views import is_previewable
from invenio_records_files.api import FileObject
from invenio_records_permissions.policies import get_record_permission_policy
Expand Down Expand Up @@ -182,6 +183,25 @@ def custom_fields_search(field, field_value):
)


def custom_vocab_fields_search(field, field_value, sub_field):
"""Get custom field search url for vocabulary values."""
namespace_array = field.split(":")
namespace = namespace_array[0]
namespaces = current_app.config.get("RDM_NAMESPACES")

if not namespaces.get(namespace):
return None

locale = get_locale()
if not locale:
locale = current_app.config.get("BABEL_DEFAULT_LOCALE", "en")

namespace_string = "\:".join(namespace_array) + f".{sub_field}.{locale}"
return url_for(
"invenio_search_ui.search", q=f"custom_fields.{namespace_string}:{field_value}"
)


def transform_record(record, serializer, module=None, throws=True, **kwargs):
"""Transform a record using a serializer."""
try:
Expand Down

0 comments on commit bf5e3a7

Please sign in to comment.