Add prettier date formatter

This commit is contained in:
Tim Van Baak 2021-06-16 00:58:09 -07:00
parent a07b342da3
commit 0e35f15a3a
4 changed files with 12 additions and 9 deletions

View File

@ -114,6 +114,6 @@ def update_logged_in(db: DbContext, username: str) -> None:
db( db(
update(User) update(User)
.where(User.username == username) .where(User.username == username)
.values(last_login=datetime.datetime.utcnow()) .values(last_login=datetime.datetime.now(datetime.timezone.utc))
) )
db.session.commit() db.session.commit()

View File

@ -1,3 +1,4 @@
from datetime import datetime, timezone
import json import json
import os import os
@ -48,6 +49,15 @@ def get_app(
# Configure jinja options # Configure jinja options
app.jinja_options.update(trim_blocks=True, lstrip_blocks=True) app.jinja_options.update(trim_blocks=True, lstrip_blocks=True)
def date_format(dt: datetime, formatstr="%Y-%m-%d %H:%M:%S%z") -> str:
if dt is None:
return "never"
# Cast db time to UTC, then convert to local timezone
adjusted = dt.replace(tzinfo=timezone.utc).astimezone()
return adjusted.strftime(formatstr)
app.template_filter("date")(date_format)
# Set up Flask-Login # Set up Flask-Login
auth.get_login_manager().init_app(app) auth.get_login_manager().init_app(app)

View File

@ -21,13 +21,6 @@ def register_custom_filters(app):
val = getattr(user.cfg, attr) val = getattr(user.cfg, attr)
return val return val
@app.template_filter("asdate")
def timestamp_to_readable(ts, formatstr="%Y-%m-%d %H:%M:%S"):
if ts is None:
return "null"
dt = datetime.fromtimestamp(ts)
return dt.strftime(formatstr)
@app.template_filter("articlelink") @app.template_filter("articlelink")
def article_link(title): def article_link(title):
return url_for( return url_for(

View File

@ -40,6 +40,6 @@
<p> <p>
<b>{{ user.username }}</b> {% if user.username != user.display_name %} / {{ user.display_name }}{% endif %} (id #{{user.id}}){% if user.is_site_admin %} <b>[ADMIN]</b>{% endif %} <b>{{ user.username }}</b> {% if user.username != user.display_name %} / {{ user.display_name }}{% endif %} (id #{{user.id}}){% if user.is_site_admin %} <b>[ADMIN]</b>{% endif %}
</p> </p>
<p>Last activity: {{ user.last_activity }} &mdash; Last login: {{ user.last_login }}</p> <p>Last activity: {{ user.last_activity|date }} &mdash; Last login: {{ user.last_login|date }}</p>
</div> </div>
{% endmacro %} {% endmacro %}