move date humanization out of jinja and into python where exceptions can be handled properly

In the arrow package, Catalan language does not have 'weeks' defined as a way to display datetimes. A PR has been submitted.
This commit is contained in:
rimu 2024-09-30 16:34:17 +13:00
parent 5465f945be
commit d2327fd3c1
8 changed files with 21 additions and 7 deletions

View file

@ -3,6 +3,7 @@ from time import time
from typing import List, Union
from urllib.parse import urlparse, parse_qs, urlencode, urlunparse
import arrow
from flask import current_app, escape, url_for, render_template_string
from flask_login import UserMixin, current_user
from sqlalchemy import or_, text, desc
@ -1174,6 +1175,13 @@ class Post(db.Model):
return name
return False
def posted_at_localized(self, sort, locale):
# some locales do not have a definition for 'weeks' so are unable to display some dates in some languages. Fall back to english for those languages.
try:
return arrow.get(self.last_active if sort == 'active' else self.posted_at).humanize(locale=locale)
except ValueError as v:
return arrow.get(self.last_active if sort == 'active' else self.posted_at).humanize(locale='en')
def notify_new_replies(self, user_id: int) -> bool:
existing_notification = NotificationSubscription.query.filter(NotificationSubscription.entity_id == self.id,
NotificationSubscription.user_id == user_id,
@ -1458,6 +1466,12 @@ class PostReply(db.Model):
def public_url(self):
return self.profile_id()
def posted_at_localized(self, locale):
try:
return arrow.get(self.posted_at).humanize(locale=locale)
except ValueError as v:
return arrow.get(self.posted_at).humanize(locale='en')
# the ap_id of the parent object, whether it's another PostReply or a Post
def in_reply_to(self):
if self.parent_id is None:

View file

@ -21,7 +21,7 @@
{{ render_username(comment['comment'].author) }}
{% endwith -%}
{% if comment['comment'].author.id == post.author.id %}<span title="Submitter of original post" aria-label="submitter" class="small">[OP] </span>{% endif %}
<span class="text-muted small">{{ arrow.get(comment['comment'].posted_at).humanize(locale=locale) }}{% if comment['comment'].edited_at %}, edited {{ arrow.get(comment['comment'].edited_at).humanize(locale=locale) }} {% endif %}</span>
<span class="text-muted small">{{ comment['comment'].posted_at_localized(locale) }}{% if comment['comment'].edited_at %}, edited {{ arrow.get(comment['comment'].edited_at).humanize(locale=locale) }} {% endif %}</span>
<a class="unhide" href="#"><span class="fe fe-expand"></span></a>
{% if comment['comment'].reports and current_user.is_authenticated and post.community.is_moderator(current_user)%}
<span class="red fe fe-report" title="{{ _('Reported. Check comment for issues.') }}"></span>

View file

@ -84,7 +84,7 @@
{{ render_username(comment['comment'].author) }}
{% endwith -%}
{% if comment['comment'].author.id == post.author.id -%}<span title="Submitter of original post" aria-label="{{ _('Post creator') }}" class="small">[OP] </span>{% endif -%}
<a href="#comment_{{ comment['comment'].id }}" class="text-muted small" aria_label="{{ _('When: ') }}">{{ arrow.get(comment['comment'].posted_at).humanize(locale=locale) }}{% if comment['comment'].edited_at -%}, edited {{ arrow.get(comment['comment'].edited_at).humanize(locale=locale) }} {% endif -%}</a>
<a href="#comment_{{ comment['comment'].id }}" class="text-muted small" aria_label="{{ _('When: ') }}">{{ comment['comment'].posted_at_localized(locale) }}{% if comment['comment'].edited_at -%}, edited {{ arrow.get(comment['comment'].edited_at).humanize(locale=locale) }} {% endif -%}</a>
<a class="unhide" href="#"><span class="fe fe-expand"></span></a>
{% if comment['comment'].reports and current_user.is_authenticated and post.community.is_moderator(current_user)%}
<span class="red fe fe-report" title="{{ _('Reported. Check comment for issues.') }}"></span>

View file

@ -8,7 +8,7 @@
{% if post.sticky -%}<span class="fe fe-sticky-right"></span>{% endif -%}
</h3>
<span class="author small">{% if show_post_community -%}<a href="/c/{{ post.community.link() }}" aria-label="{{ _('Go to community %(name)s', name=post.community.name) }}">c/{{ post.community.name }}</a>{% endif -%}
by {{ render_username(post.author) }} <time datetime="{{ post.last_active }}">{{ arrow.get(post.last_active if sort == 'active' else post.posted_at).humanize(locale=locale) }}</time></span>
by {{ render_username(post.author) }} <time datetime="{{ post.last_active }}">{{ post.posted_at_localized(sort, locale) }}</time></span>
{% if post.body_html -%}
<div class="post_teaser_article_preview small">
{{ first_paragraph(post.body_html) | safe }}

View file

@ -11,7 +11,7 @@
{% if post.sticky -%}<span class="fe fe-sticky-right"></span>{% endif -%}
</h3>
<span class="author small">{% if show_post_community -%}<a href="/c/{{ post.community.link() }}" aria-label="{{ _('Go to community %(name)s', name=post.community.name) }}">c/{{ post.community.name }}</a>{% endif -%}
by {{ render_username(post.author) }} <time datetime="{{ post.last_active }}">{{ arrow.get(post.last_active if sort == 'active' else post.posted_at).humanize(locale=locale) }}</time></span>
by {{ render_username(post.author) }} <time datetime="{{ post.last_active }}">{{ post.posted_at_localized(sort, locale) }}</time></span>
{% if post.image_id and not low_bandwidth -%}
<div class="post_teaser_image_preview">
<a href="{{ post.image.view_url() }}" rel="nofollow ugc" aria-label="{{ _('View image') }}" target="_blank"><img src="{{ post.image.medium_url() }}"

View file

@ -14,7 +14,7 @@
{% if post.sticky -%}<span class="fe fe-sticky-right"></span>{% endif -%}
</h3>
<span class="author small">{% if show_post_community -%}<a href="/c/{{ post.community.link() }}" aria-label="{{ _('Go to community %(name)s', name=post.community.name) }}">c/{{ post.community.name }}</a>{% endif -%}
by {{ render_username(post.author) }} <time datetime="{{ post.last_active }}">{{ arrow.get(post.last_active if sort == 'active' else post.posted_at).humanize(locale=locale) }}</time></span>
by {{ render_username(post.author) }} <time datetime="{{ post.last_active }}">{{ post.posted_at_localized(sort, locale) }}</time></span>
{% if post.body_html -%}
<div class="post_teaser_link_preview small">
{{ first_paragraph(post.body_html) | safe }}

View file

@ -9,7 +9,7 @@
{% if post.sticky -%}<span class="fe fe-sticky-right"></span>{% endif -%}
</h3>
<span class="author small">{% if show_post_community -%}<a href="/c/{{ post.community.link() }}" aria-label="{{ _('Go to community %(name)s', name=post.community.name) }}">c/{{ post.community.name }}</a>{% endif -%}
by {{ render_username(post.author) }} <time datetime="{{ post.last_active }}">{{ arrow.get(post.last_active if sort == 'active' else post.posted_at).humanize(locale=locale) }}</time></span>
by {{ render_username(post.author) }} <time datetime="{{ post.last_active }}">{{ post.posted_at_localized(sort, locale) }}</time></span>
{% include "post/post_teaser/_utilities_bar.html" %}
</div>
<div class="col col-2 col_thumbnail">

View file

@ -16,7 +16,7 @@
{% if post.sticky -%}<span class="fe fe-sticky-right"></span>{% endif -%}
</h3>
<span class="author small">{% if show_post_community -%}<a href="/c/{{ post.community.link() }}" aria-label="{{ _('Go to community %(name)s', name=post.community.name) }}">c/{{ post.community.name }}</a>{% endif -%}
by {{ render_username(post.author) }} <time datetime="{{ post.last_active }}">{{ arrow.get(post.last_active if sort == 'active' else post.posted_at).humanize(locale=locale) }}</time></span>
by {{ render_username(post.author) }} <time datetime="{{ post.last_active }}">{{ post.posted_at_localized(sort, locale) }}</time></span>
{% if not low_bandwidth %}
<div class="post_teaser_video_preview">
<div class="max_width_512">