summaryrefslogtreecommitdiff
path: root/jimbrella/admin.py
diff options
context:
space:
mode:
authorFrederick Yin <fkfd@fkfd.me>2022-02-02 11:16:23 +0800
committerFrederick Yin <fkfd@fkfd.me>2022-02-02 11:16:23 +0800
commit357cc3d8d9f2b02e60f0763f4701bd614b876436 (patch)
treeaaf9aca396dc7ccc67790f313c445b04744e23df /jimbrella/admin.py
parentc8caa0ca774c89484d64b5e7ee308e379f834cea (diff)
Adapt web interface
Diffstat (limited to 'jimbrella/admin.py')
-rw-r--r--jimbrella/admin.py24
1 files changed, 13 insertions, 11 deletions
diff --git a/jimbrella/admin.py b/jimbrella/admin.py
index 49cbf55..4454c69 100644
--- a/jimbrella/admin.py
+++ b/jimbrella/admin.py
@@ -1,14 +1,14 @@
from flask import Blueprint, request, session, render_template, redirect, url_for, abort
from user_agents import parse as user_agent
-from .database import Database
+from .umbrellas import Umbrellas
from .admin_log import AdminLog
from .users import Users
from .exceptions import *
from .config import *
bp = Blueprint("admin", __name__, url_prefix="/admin")
-db = Database(DATABASE_PATH)
-users = Users(USERS_PATH)
+db = Umbrellas(DATABASE_PATH)
+users = Users(DATABASE_PATH)
admin_log = AdminLog(ADMIN_LOG_PATH)
@@ -28,7 +28,11 @@ def check_privilege():
@bp.route("/")
def index():
umbrellas = db.read()
- statuses = Database.group_by_status(umbrellas)
+ # count # of umbrellas in each status
+ statuses = {
+ status: len([u for u in umbrellas if u["status"] == status])
+ for status in ("available", "lent", "overdue")
+ }
return render_template(
"admin/index.html",
umbrellas=umbrellas,
@@ -52,7 +56,7 @@ def umbrellas():
return render_template(
template,
umbrellas=umbrellas,
- edit=int(edit) if edit else None,
+ edit=int(edit) if edit.isnumeric() else None,
error=error,
)
@@ -61,13 +65,11 @@ def umbrellas():
def umbrellas_edit():
data = {}
for key in [
- "serial",
- "alias",
+ "id",
"status",
"tenant_name",
"tenant_id",
"tenant_phone",
- "tenant_email",
"lent_at",
]:
data[key] = request.form.get(key)
@@ -78,11 +80,11 @@ def umbrellas_edit():
# invalid field is in `e.message`.
return redirect(
"{0}?edit={1}&error={2}".format(
- url_for("admin.umbrellas"), request.form.get("serial"), e.message
+ url_for("admin.umbrellas"), request.form.get("id"), e.message
)
)
except UmbrellaNotFoundError:
- pass # impossible on web console
+ abort(400)
for column, value_pair in diff.items():
past, new = value_pair
@@ -90,7 +92,7 @@ def umbrellas_edit():
"ADMIN_MODIFY_DB",
{
"admin_name": session["username"],
- "serial": data["serial"],
+ "id": data["id"],
"column": column,
"past_value": past,
"new_value": new,