Skip to content

Commit

Permalink
views: fix template filters
Browse files Browse the repository at this point in the history
* Fix `doi_identifier` template filter
* Create a template filter called `format_date`
  to accept date strings without formatting them.
  This is needed to handle edtf dates for now.

closes inveniosoftware#80, inveniosoftware#81
  • Loading branch information
zzacharo committed Mar 24, 2020
1 parent 9b302d5 commit 452c148
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
<a href="{{ url_for('invenio_records_ui.recid', pid_value=record.pid) }}">{{ record.version }}</a>
<small class="text-muted">{{ record.identifiers|doi_identifier }}</small>
</td>
<td align="right"><small class="text-muted">{{ record.publication_date|to_date|dateformat(format='medium')}}</small></td>
<td align="right"><small class="text-muted">{{ record.publication_date|to_date|format_date(format='medium')}}</small></td>
</tr>
</tbody></table>
<small>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<div class="row">
<div class="col-sm-8 col-md-8 col-left">
{%- block record_body %}
<span class="label label-info" title="Publication date">{{ record.publication_date|to_date|dateformat(format='long') }}</span>
<span class="label label-info" title="Publication date">{{ record.publication_date|to_date|format_date(format='long') }}</span>
<span class="label record-version"> | Version {{ record.version }}</span>
<div class="pull-right">
<span class="label label-default">{{ record.resource_type.type if record.resource_type else "resource type" }}</span>
Expand Down
34 changes: 29 additions & 5 deletions invenio_rdm_records/theme/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

import arrow
import idutils
from arrow.parser import ParserError
from flask import Blueprint, current_app, render_template
from flask_babelex import format_date as babel_format_date
from invenio_previewer.views import is_previewable
from invenio_records_permissions.policies import get_record_permission_policy

Expand Down Expand Up @@ -49,7 +51,29 @@ def to_date(date_string):
```
"""
assert isinstance(date_string, str)
return arrow.get(date_string).date()
date = ""
try:
date = arrow.get(date_string).date()
except ParserError:
date = date_string
return date


@blueprint.app_template_filter('format_date')
def format_date(date, format):
"""Return a formatted Date object.
If the passed date is a string then it returns it
Typically used as follows:
```jinja2
{{ date | format_date("long") }}
```
"""
if isinstance(date, str):
return date
return babel_format_date(date=date, format=format)


@blueprint.app_template_filter()
Expand Down Expand Up @@ -108,12 +132,12 @@ def pid_url(identifier, scheme=None, url_scheme='https'):


@blueprint.app_template_filter('doi_identifier')
def doi_identifier(pids):
def doi_identifier(identifiers):
"""Determine if DOI is managed locally."""
for id in pids:
for identifier in identifiers:
# TODO: extract this "DOI" constant to a registry?
if id['scheme'] == "DOI":
return id['identifier']
if identifier == 'DOI':
return identifiers[identifier]


@blueprint.app_template_filter('doi_locally_managed')
Expand Down

0 comments on commit 452c148

Please sign in to comment.