summaryrefslogtreecommitdiff
path: root/jimbrella/admin.py
blob: 19c5a12276767cf61401f1dfe18b5bd9a07e6328 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
from flask import Blueprint, request, session, render_template, redirect, url_for, abort
from user_agents import parse as user_agent
from datetime import datetime
from dateutil.parser import isoparse
from .umbrellas import Umbrellas
from .logger import Logger
from .users import Users
from .exceptions import *
from .config import config
from .utils import human_datetime, human_timedelta, CST

bp = Blueprint("admin", __name__, url_prefix="/admin")
db = Umbrellas(config.get("general", "db_path"))
users = Users(config.get("general", "db_path"))
logger = Logger(config.get("general", "db_path"))


@bp.before_request
def check_privilege():
    # only clients who have obtained a session and sent it in the Cookie header
    # will have a decryptable username here
    if "username" not in session:
        return redirect(url_for("auth.auth", action="login"))

    username = session["username"]
    user = users.find(username)  # under normal circumstances it must exist
    if user["role"] != "admin":
        abort(403)


@bp.route("/")
def index():
    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"] = "Invalid date"

    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():
    # sqlite3.Row does not support item assignment, but dict does
    umbrellas = [dict(umb) for umb in db.read()]
    edit = request.args.get("edit")
    if edit is not None:
        if not edit.isnumeric():
            abort(400)
        edit = int(edit)

    for umb in umbrellas:
        if umb["id"] == edit and not umb["lent_at"]:
            # autofill current time when admin is editing and lent_at is empty
            umb["lent_at"] = datetime.now(tz=CST).replace(tzinfo=None).isoformat(timespec="seconds")
        elif umb["lent_at"]:
            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"] = "Invalid date"

    error = request.args.get("error")
    template = (
        "admin/umbrellas_mobile.html"
        if user_agent(request.user_agent.string).is_mobile
        else "admin/umbrellas_desktop.html"
    )
    return render_template(
        template,
        umbrellas=umbrellas,
        edit=edit,
        error=error,
    )


@bp.route("/umbrellas/edit", methods=["POST"])
def umbrellas_edit():
    data = {}
    for key in [
        "id",
        "status",
        "tenant_name",
        "tenant_id",
        "tenant_phone",
        "lent_at",
    ]:
        data[key] = request.form.get(key)

    try:
        db.admin_modify(session["username"], data)
    except UmbrellaValueError as e:
        # invalid field is in `e.message`.
        return redirect(
            "{0}?edit={1}&error={2}".format(
                url_for("admin.umbrellas"), request.form.get("id"), e.message
            )
        )
    except UmbrellaNotFoundError:
        abort(400)

    return redirect(url_for("admin.umbrellas"))


@bp.route("/adminlogs")
def adminlogs():
    logs = logger.read_admin()
    return render_template("admin/admin_logs.html", logs=logs)

@bp.route("/logs")
def logs():
    logs = logger.read_tenant()
    return render_template("admin/logs.html", logs=logs)