diff options
author | Frederick Yin <fkfd@fkfd.me> | 2022-02-04 20:44:07 +0800 |
---|---|---|
committer | Frederick Yin <fkfd@fkfd.me> | 2022-02-04 20:44:07 +0800 |
commit | 87c661557186a4975fa94977ac1ac1be9bd87d2e (patch) | |
tree | 394f718b68c83a992fffe61b8b29e3497a25e9aa /jimbrella/admin.py | |
parent | 15dd33f6b1db117716e16f1dfee281efd9b43a2a (diff) |
Supply Jinja with full umbrella data sorted by status
Diffstat (limited to 'jimbrella/admin.py')
-rw-r--r-- | jimbrella/admin.py | 34 |
1 files changed, 25 insertions, 9 deletions
diff --git a/jimbrella/admin.py b/jimbrella/admin.py index 9cbf4c8..d5df4d4 100644 --- a/jimbrella/admin.py +++ b/jimbrella/admin.py @@ -30,18 +30,34 @@ def check_privilege(): @bp.route("/") def index(): - umbrellas = db.read() - # count # of umbrellas in each status - status_count = { - status: len([u for u in umbrellas if u["status"] == status]) - for status in ("available", "lent", "overdue") + umbrellas = [dict(umb) for umb in db.read()] + # sort umbrellas by their status + statuses = { + "available": [], + "lent": [], + "overdue": [], } + for umb in umbrellas: + if umb["status"] == "available": + statuses["available"].append(umb) + elif umb["status"] in ("lent", "overdue"): + statuses[umb["status"]].append(umb) + try: + lent_at = isoparse(umb["lent_at"]) + umb["lent_at"] = human_datetime(lent_at) + umb["lent_time_ago"] = ( + human_timedelta(datetime.now(tz=CST) - lent_at) + " ago" + ) + except ValueError: + umb["lent_at"] = "Invalid date" + umb["lent_time_ago"] = "" + return render_template( "admin/index.html", - umbrellas=len(umbrellas), - available=status_count["available"], - lent=status_count["lent"], - overdue=status_count["overdue"], + umbrellas=umbrellas, + available=statuses["available"], + lent=statuses["lent"], + overdue=statuses["overdue"], mobile=user_agent(request.user_agent.string).is_mobile, ) |