From 272eba3bc1217d339cb1b0e23fd5aad2bee752a4 Mon Sep 17 00:00:00 2001 From: Frederick Yin Date: Wed, 2 Feb 2022 23:23:15 +0800 Subject: Mostly westling timezones --- jimbrella/admin.py | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) (limited to 'jimbrella/admin.py') 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, ) -- cgit v1.2.3