Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handle undefined record attributes in templates to avoid rendering errors #2932

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{#
Copyright (C) 2020-2024 CERN.
Copyright (C) 2024 Northwestern University.
Copyright (C) 2024 KTH Royal Institute of Technology.

Invenio RDM Records is free software; you can redistribute it and/or modify
it under the terms of the MIT License; see LICENSE file for more details.
Expand Down Expand Up @@ -59,18 +60,25 @@

{% macro show_add_descriptions(add_descriptions) %}
{% for add_description in add_descriptions %}
<section id="additional-description-{{ loop.index }}" class="rel-mt-2 rich-input-content"
aria-label="{{ _( add_description.type.title_l10n ) }}">
<h2>{{ add_description.type.title_l10n }} <span
class="text-muted language">{{ '(' + add_description.lang.title_l10n + ')' if add_description.lang }}</span>
{% set desc_type_defined = add_description.type is defined %}
{% set desc_text = add_description.description|default('') %}
<section
id="additional-description-{{ loop.index }}"
class="rel-mt-2 rich-input-content"
aria-label="{{ (add_description.type.title_l10n if desc_type_defined else _('Missing description type!')) }}"
>
<h2>
{{ add_description.type.title_l10n if desc_type_defined else _('Missing description type!') }}
<span class="text-muted language">
{{ '(' ~ add_description.lang.title_l10n ~ ')' if add_description.lang is defined else '' }}
</span>
</h2>

{% if add_description.type.id == "notes" %}
{% if desc_type_defined and add_description.type.id == "notes" %}
<div class="ui message warning">
{{ add_description.description | sanitize_html() | safe }}
{{ desc_text | sanitize_html() | safe }}
</div>
{% else %}
{{ add_description.description | sanitize_html() | safe }}
{{ desc_text | sanitize_html() | safe }}
{% endif %}
</section>
{% endfor %}
Expand All @@ -79,10 +87,11 @@ <h2>{{ add_description.type.title_l10n }} <span

{% macro show_dates(dates) %}
{% for date in dates %}
<dt class="ui tiny header">{{ date.type.title_l10n }}</dt>
{% set date_type_title = date.type.title_l10n if (date.type is defined and date.type.title_l10n is defined) else _('Unknown date type') %}
<dt class="ui tiny header">{{ date_type_title }}</dt>
<dd>
<div>{{ date.date }}</div>
<div class="text-muted">{{ date.description }}</div>
<div>{{ date.date|default('') }}</div>
<div class="text-muted">{{ date.description|default('') }}</div>
</dd>
{% endfor %}
{% endmacro %}
Expand All @@ -96,25 +105,28 @@ <h2>{{ add_description.type.title_l10n }} <span


{% macro _show_funding_item(item, index) %}
{%- if item.award -%}
{%- if item.award.title_l10n -%}
{% set has_award = item is defined and item.award is defined %}
{% set award_title = item.award.title_l10n if has_award and item.award.title_l10n is defined else '' %}
{% set funder_name = item.funder.name if item.funder is defined and item.funder.name is defined else '' %}

{% if has_award %}
{% if award_title %}
<dt class="ui tiny header">
<span class="mr-5">
{% if item.award.acronym %}
{% if item.award.acronym is defined and item.award.acronym %}
{{ item.award.acronym }} –
{% endif %}

{{ item.award.title_l10n }}
{{ award_title }}
</span>

{%- if item.award.number -%}
{%- if item.award.number is defined and item.award.number -%}
<span class="ui mini basic label ml-0 mr-5" id="number-label-{{ index }}">
{{ item.award.number }}
</span>
{%- endif -%}

{%- if item.award.identifiers -%}
{% for identifier in item.award.identifiers if 'url' == identifier.scheme %}
{%- if item.award.identifiers is defined -%}
{% for identifier in item.award.identifiers if identifier.scheme == 'url' %}
<a href="{{ identifier.identifier }}" target="_blank"
rel="noopener noreferrer" aria-label="{{ _('Open external link') }}">
<i class="external alternate icon"></i>
Expand All @@ -123,25 +135,23 @@ <h2>{{ add_description.type.title_l10n }} <span
{%- endif -%}
</dt>
{%- endif -%}
<dd class="text-muted">{{ item.funder.name if item.funder }}</dd>
<dd class="text-muted">{{ funder_name }}</dd>
{%- else -%}
<dt class="ui tiny header">{{ item.funder.name if item.funder }}</dt>
<dt class="ui tiny header">{{ funder_name }}</dt>
{%- endif -%}
{% endmacro %}


{% macro show_references(references) %}
<ul class="ui bulleted list details-list">
{% for reference in references %}
{% set reference_string = reference.reference %}
{% if 'identifier' in reference %}
{% if 'scheme' in reference %}
{% set reference_string = reference.reference|default('') %}
{% if reference.identifier is defined and reference.identifier %}
{% if reference.scheme is defined and reference.scheme %}
{% set reference_scheme = reference.scheme | get_scheme_label %}
{% set reference_string = reference_string + ' ( ' + reference_scheme +
' - ' + reference.identifier + ' )' %}
{% set reference_string = reference_string ~ ' (' ~ reference_scheme ~ ' - ' ~ reference.identifier ~ ')' %}
{% else %}
{% set reference_string = reference_string + ' ( ' + reference.identifier +
' )' %}
{% set reference_string = reference_string ~ ' (' ~ reference.identifier ~ ')' %}
{% endif %}
{% endif %}
<li class="item">{{ reference_string | urlize }}</li>
Expand All @@ -153,28 +163,31 @@ <h2>{{ add_description.type.title_l10n }} <span
{% macro _identifiers_for_group(related_identifiers) %}
{% for identifier in related_identifiers %}
<dd>
{% if identifier.resource_type is defined %}
{% if identifier.resource_type is defined and identifier.resource_type.title_l10n is defined %}
{{ identifier.resource_type.title_l10n }}:
{% endif %}

{% set url = identifier.identifier|pid_url %}
{% set ident_val = identifier.identifier|default('') %}
{% set url = ident_val|pid_url %}
{% if url %}
<a href="{{ url }}" target="_blank" title="{{ _('Opens in new tab') }}">
{{ identifier.identifier }}
{{ ident_val }}
</a>
{% else %}
{{ identifier.identifier }}
{{ ident_val }}
{% endif %}

{{ ' (' + identifier.scheme | get_scheme_label + ')' }}
{% if identifier.scheme is defined and identifier.scheme %}
{{ ' (' ~ (identifier.scheme|get_scheme_label) ~ ')' }}
{% endif %}
</dd>
{% endfor %}
{% endmacro %}


{% macro show_related_identifiers(related_identifiers) %}
<dl class="details-list">
{%- for group in related_identifiers | groupby('relation_type.title_l10n') %}
{%- for group in (related_identifiers|selectattr("relation_type","defined")|list) | groupby('relation_type.title_l10n') %}
<dt class="ui tiny header">{{ group.grouper }}</dt>
{{ _identifiers_for_group(group.list) }}
{%- endfor %}
Expand Down