From 777c8b45497d0fd671db76e8774e9ca0cac49d05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adeodato=20Sim=C3=B3?= Date: Fri, 20 Oct 2023 23:05:02 -0300 Subject: [PATCH] naturalday_partial filter for working with SealedDate --- bookwyrm/templates/book/publisher_info.html | 4 ++-- bookwyrm/templatetags/sealed_dates.py | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 bookwyrm/templatetags/sealed_dates.py diff --git a/bookwyrm/templates/book/publisher_info.html b/bookwyrm/templates/book/publisher_info.html index e3ffedca8..26d8e43fd 100644 --- a/bookwyrm/templates/book/publisher_info.html +++ b/bookwyrm/templates/book/publisher_info.html @@ -1,7 +1,7 @@ {% spaceless %} {% load i18n %} -{% load humanize %} +{% load sealed_dates %} {% firstof book.physical_format_detail book.get_physical_format_display as format %} {% firstof book.physical_format book.physical_format_detail as format_property %} @@ -57,7 +57,7 @@ {% endfor %} {% endif %} - {% with date=book.published_date|default:book.first_published_date|naturalday publisher=book.publishers|join:', ' %} + {% with date=book.published_date|default:book.first_published_date|naturalday_partial publisher=book.publishers|join:', ' %} {% if book.published_date and publisher %} {% blocktrans %}Published {{ date }} by {{ publisher }}.{% endblocktrans %} {% elif publisher %} diff --git a/bookwyrm/templatetags/sealed_dates.py b/bookwyrm/templatetags/sealed_dates.py new file mode 100644 index 000000000..fb64734fa --- /dev/null +++ b/bookwyrm/templatetags/sealed_dates.py @@ -0,0 +1,21 @@ +""" formatting of SealedDate instances """ +from django import template +from django.template import defaultfilters +from django.contrib.humanize.templatetags.humanize import naturalday + +from bookwyrm.utils.sealed_date import SealedDate + +register = template.Library() + + +@register.filter(expects_localtime=True, is_safe=False) +def naturalday_partial(date): + if not isinstance(date, SealedDate): + return defaultfilters.date(date) + if date.has_day: + fmt = "DATE_FORMAT" + elif date.has_month: + fmt = "YEAR_MONTH_FORMAT" + else: + fmt = "Y" + return naturalday(date, fmt)