naturalday_partial filter for working with SealedDate

This commit is contained in:
Adeodato Simó 2023-10-20 23:05:02 -03:00
parent 46d80d56a5
commit 777c8b4549
No known key found for this signature in database
GPG key ID: CDF447845F1A986F
2 changed files with 23 additions and 2 deletions

View file

@ -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 %}

View file

@ -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)