from flask import Flask, request, render_template, redirect, url_for from user_agents import parse as user_agent from .database import Database 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") if __name__ == "__main__": app.run()