diff options
author | Frederick Yin <fkfd@fkfd.me> | 2022-02-02 23:23:15 +0800 |
---|---|---|
committer | Frederick Yin <fkfd@fkfd.me> | 2022-02-02 23:23:15 +0800 |
commit | 272eba3bc1217d339cb1b0e23fd5aad2bee752a4 (patch) | |
tree | 2969dbc45aa9eb662d8bffa8b3163d3e9e8830b7 /jimbrella/admin.py | |
parent | dd160916a8f1a9028d95d7e0344d40544078151f (diff) |
Mostly westling timezones
Diffstat (limited to 'jimbrella/admin.py')
-rw-r--r-- | jimbrella/admin.py | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/jimbrella/admin.py b/jimbrella/admin.py index 8a52bb8..799ff66 100644 --- a/jimbrella/admin.py +++ b/jimbrella/admin.py @@ -1,10 +1,13 @@ from flask import Blueprint, request, session, render_template, redirect, url_for, abort from user_agents import parse as user_agent +from datetime import datetime +from dateutil.parser import isoparse from .umbrellas import Umbrellas from .admin_log import AdminLog from .users import Users from .exceptions import * from .config import * +from .utils import human_timedelta, CST bp = Blueprint("admin", __name__, url_prefix="/admin") db = Umbrellas(DATABASE_PATH) @@ -45,8 +48,30 @@ def index(): @bp.route("/umbrellas") def umbrellas(): - umbrellas = db.read() + # sqlite3.Row does not support item assignment, but dict does + umbrellas = [dict(umb) for umb in db.read()] edit = request.args.get("edit") + if edit is not None: + if not edit.isnumeric(): + abort(400) + edit = int(edit) + + for umb in umbrellas: + if umb["id"] == edit and not umb["lent_at"]: + # autofill current time when admin is editing and lent_at is empty + umb["lent_at"] = datetime.now().isoformat(timespec="seconds") + elif umb["lent_at"]: + try: + # web interface provides no timezone so UTC+8 is assumed + lent_at = isoparse(umb["lent_at"]).replace(tzinfo=CST) + umb["lent_at"] = lent_at.isoformat(timespec="seconds") + umb["lent_time_ago"] = human_timedelta( + datetime.now().astimezone(CST) - lent_at + ) + " ago" + except ValueError: + umb["lent_at"] = "Invalid date" + umb["lent_time_ago"] = "" + error = request.args.get("error") template = ( "admin/umbrellas_mobile.html" @@ -56,7 +81,7 @@ def umbrellas(): return render_template( template, umbrellas=umbrellas, - edit=int(edit) if edit is not None and edit.isnumeric() else None, + edit=edit, error=error, ) |