summaryrefslogtreecommitdiff
path: root/jimbrella
diff options
context:
space:
mode:
authorFrederick Yin <fkfd@fkfd.me>2021-10-24 23:36:37 +0800
committerFrederick Yin <fkfd@fkfd.me>2021-10-24 23:36:37 +0800
commit46e8097001aa29c27a5acce59f5c2ad83e850630 (patch)
treea726e1005f779b1d2b26d8d2684cce1f02bde876 /jimbrella
parent9e4d79512d31e960e9730da61d9b31785478df93 (diff)
Separate admin routes to a blueprint
Because modularity
Diffstat (limited to 'jimbrella')
-rw-r--r--jimbrella/admin.py52
-rw-r--r--jimbrella/web.py54
2 files changed, 55 insertions, 51 deletions
diff --git a/jimbrella/admin.py b/jimbrella/admin.py
new file mode 100644
index 0000000..df751ee
--- /dev/null
+++ b/jimbrella/admin.py
@@ -0,0 +1,52 @@
+from flask import Blueprint, request, render_template, redirect, url_for
+from user_agents import parse as user_agent
+from .database import Database
+from .config import *
+
+bp = Blueprint("admin", __name__, url_prefix="/admin")
+db = Database(DATABASE_PATH)
+
+
+@bp.route("/")
+def index():
+ umbrellas = db.read()
+ statuses = Database.group_by_status(umbrellas)
+ return render_template(
+ "admin/index.html",
+ umbrellas=umbrellas,
+ available=statuses["available"],
+ lent=statuses["lent"],
+ overdue=statuses["overdue"],
+ mobile=user_agent(request.user_agent.string).is_mobile,
+ )
+
+
+@bp.route("/umbrellas")
+def umbrellas():
+ umbrellas = db.read()
+ edit = request.args.get("edit")
+ return render_template(
+ "admin/umbrellas.html",
+ umbrellas=umbrellas,
+ edit=int(edit) if edit else None,
+ mobile=user_agent(request.user_agent.string).is_mobile,
+ )
+
+
+@bp.route("/umbrellas/edit", methods=["POST"])
+def umbrellas_edit():
+ data = {}
+ for key in [
+ "serial",
+ "alias",
+ "status",
+ "tenant_name",
+ "tenant_id",
+ "tenant_phone",
+ "tenant_email",
+ "lent_at",
+ ]:
+ data[key] = request.form.get(key)
+
+ db.update(data)
+ return redirect(url_for("admin.umbrellas"))
diff --git a/jimbrella/web.py b/jimbrella/web.py
index 434de57..f9bc586 100644
--- a/jimbrella/web.py
+++ b/jimbrella/web.py
@@ -1,57 +1,9 @@
-from flask import Flask, request, render_template, redirect, url_for
-from user_agents import parse as user_agent
-from .database import Database
+from flask import Flask
+from .admin import bp as admin_bp
from .config import *
-db = Database(DATABASE_PATH)
-
app = Flask("jimbrella")
-
-
-@app.route("/admin")
-def admin_index():
- umbrellas = db.read()
- statuses = Database.group_by_status(umbrellas)
- return render_template(
- "admin/index.html",
- umbrellas=umbrellas,
- available=statuses["available"],
- lent=statuses["lent"],
- overdue=statuses["overdue"],
- mobile=user_agent(request.user_agent.string).is_mobile,
- )
-
-
-@app.route("/admin/umbrellas")
-def admin_umbrellas():
- umbrellas = db.read()
- edit = request.args.get("edit")
- return render_template(
- "admin/umbrellas.html",
- umbrellas=umbrellas,
- edit=int(edit) if edit else None,
- mobile=user_agent(request.user_agent.string).is_mobile,
- )
-
-
-@app.route("/admin/umbrellas/edit", methods=["POST"])
-def admin_umbrellas_edit():
- data = {}
- for key in [
- "serial",
- "alias",
- "status",
- "tenant_name",
- "tenant_id",
- "tenant_phone",
- "tenant_email",
- "lent_at",
- ]:
- data[key] = request.form.get(key)
-
- db.update(data)
- return redirect("/admin/umbrellas")
-
+app.register_blueprint(admin_bp)
if __name__ == "__main__":
app.run()