diff --git a/amanuensis/backend/user.py b/amanuensis/backend/user.py index 73dfe57..cfeb7ca 100644 --- a/amanuensis/backend/user.py +++ b/amanuensis/backend/user.py @@ -114,6 +114,6 @@ def update_logged_in(db: DbContext, username: str) -> None: db( update(User) .where(User.username == username) - .values(last_login=datetime.datetime.utcnow()) + .values(last_login=datetime.datetime.now(datetime.timezone.utc)) ) db.session.commit() diff --git a/amanuensis/server/__init__.py b/amanuensis/server/__init__.py index de649ec..cf97468 100644 --- a/amanuensis/server/__init__.py +++ b/amanuensis/server/__init__.py @@ -1,3 +1,4 @@ +from datetime import datetime, timezone import json import os @@ -48,6 +49,15 @@ def get_app( # Configure jinja options 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 auth.get_login_manager().init_app(app) diff --git a/amanuensis/server/helpers.py b/amanuensis/server/helpers.py index a533ea1..f89f1fb 100644 --- a/amanuensis/server/helpers.py +++ b/amanuensis/server/helpers.py @@ -21,13 +21,6 @@ def register_custom_filters(app): val = getattr(user.cfg, attr) 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") def article_link(title): return url_for( diff --git a/amanuensis/server/macros.jinja b/amanuensis/server/macros.jinja index fcf3d45..71e9e2a 100644 --- a/amanuensis/server/macros.jinja +++ b/amanuensis/server/macros.jinja @@ -40,6 +40,6 @@

{{ user.username }} {% if user.username != user.display_name %} / {{ user.display_name }}{% endif %} (id #{{user.id}}){% if user.is_site_admin %} [ADMIN]{% endif %}

-

Last activity: {{ user.last_activity }} — Last login: {{ user.last_login }}

+

Last activity: {{ user.last_activity|date }} — Last login: {{ user.last_login|date }}

{% endmacro %} \ No newline at end of file