summaryrefslogtreecommitdiff
path: root/jimbrella/admin.py
blob: df751ee523993b345ee77fcb82d7ce7d7da5b129 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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"))