Skip to content

Commit

Permalink
views: meta_highwire rendering fix for old dates
Browse files Browse the repository at this point in the history
* Fixes issue which occurs when rendering publication dates older than
  1900-01-01. (closes #47)
  • Loading branch information
omelkonian authored and lnielsen committed May 16, 2017
1 parent 9d20694 commit c86026a
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 9 deletions.
3 changes: 1 addition & 2 deletions examples/templates/index.html
@@ -1,3 +1,2 @@
{% set mydate = mydate|from_isodate -%}
Today is {{ mydate.strftime('%Y-%m-%d') }}
Today is {{ mydate|to_arrow|format_arrow('YYYY-MM-DD') }}
<img src="{{ url_for('invenio_formatter_badges.badge', title='DOI', value='invenio.12345', ext='svg') }}"></img>
5 changes: 2 additions & 3 deletions invenio_formatter/__init__.py
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2015 CERN.
# Copyright (C) 2015, 2016, 2017 CERN.
#
# Invenio is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
Expand Down Expand Up @@ -36,8 +36,7 @@
.. code-block:: html
{% set mydate = mydate|from_isodate -%}
Today is {{ mydate.strftime('%Y-%m-%d') }}
Today is {{ mydate|to_arrow|format_arrow('YYYY-MM-DD') }}
Your book: {{ badge_svg('isbn','9780399547331')|safe }}
Expand Down
5 changes: 4 additions & 1 deletion invenio_formatter/ext.py
Expand Up @@ -29,7 +29,8 @@
from pkg_resources import DistributionNotFound, get_distribution

from . import config
from .filters.datetime import from_isodate, from_isodatetime
from .filters.datetime import format_arrow, from_isodate, from_isodatetime, \
to_arrow
from .views import create_badge_blueprint


Expand All @@ -55,6 +56,8 @@ def init_app(self, app):
app.jinja_env.filters.update(
from_isodate=from_isodate,
from_isodatetime=from_isodatetime,
to_arrow=to_arrow,
format_arrow=format_arrow,
)

if app.config['FORMATTER_BADGES_ENABLE']:
Expand Down
24 changes: 23 additions & 1 deletion invenio_formatter/filters/datetime.py
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2015, 2016 CERN.
# Copyright (C) 2015, 2016, 2017 CERN.
#
# Invenio is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
Expand Down Expand Up @@ -53,3 +53,25 @@ def from_isodatetime(value, strict=False):
"""
if value or strict:
return arrow.get(value).datetime


def format_arrow(value, format_string):
"""Format an arrow datetime object.
:param value: The arrow datetime object.
:param format_string: The date format string
:returns: Returns a string representation of the given arrow datetime
object, formatted according to the given format string.
.. note::
Do not use this filter to format date/times presented to an end
user. Instead use ``datetimeformat`` or ``dateformat`` from
Invenio-I18N.
"""
assert isinstance(value, arrow.Arrow)
return value.format(format_string)


def to_arrow(value):
"""Convert a Date object to an arrow datetime object."""
return arrow.get(value)
@@ -1,6 +1,6 @@
{#
# This file is part of Invenio.
# Copyright (C) 2015, 2016 CERN.
# Copyright (C) 2015, 2016, 2017 CERN.
#
# Invenio is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
Expand Down Expand Up @@ -58,7 +58,7 @@
<meta name="citation_publisher" content="{{publisher}}" />
{%- endif %}
{%- if publication_date %}
<meta name="citation_publication_date" content="{{publication_date.strftime('%Y/%m/%d')}}" />
<meta name="citation_publication_date" content="{{publication_date|to_arrow|format_arrow('YYYY/MM/DD')}}" />
{%- endif %}
{%- if doi %}
<meta name="citation_doi" content="{{doi}}" />
Expand Down

0 comments on commit c86026a

Please sign in to comment.