summaryrefslogtreecommitdiff
path: root/jimbrella
diff options
context:
space:
mode:
authorFrederick Yin <fkfd@fkfd.me>2021-10-24 23:12:27 +0800
committerFrederick Yin <fkfd@fkfd.me>2021-10-24 23:12:27 +0800
commit18b4424faba782e136202ab4d41086e79ace1bd7 (patch)
treee003e292e3626332b54e7bb79444d3b0c467d713 /jimbrella
parent0ed10326a182d6d37845cf5eb074e3e505d403cf (diff)
Admin can now modify database from web console
Diffstat (limited to 'jimbrella')
-rw-r--r--jimbrella/templates/admin/logs.html34
-rw-r--r--jimbrella/templates/admin/umbrellas.html42
-rw-r--r--jimbrella/web.py23
3 files changed, 97 insertions, 2 deletions
diff --git a/jimbrella/templates/admin/logs.html b/jimbrella/templates/admin/logs.html
new file mode 100644
index 0000000..6f0ed71
--- /dev/null
+++ b/jimbrella/templates/admin/logs.html
@@ -0,0 +1,34 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>JI Umbrella</title>
+ <link rel="stylesheet" href="/static/jimbrella.css" />
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
+ </head>
+ <body>
+ <h1>JI Umbrella Logs</h1>
+ <div id="container">
+ {% include "admin/tabs.html" %}
+ <div class="data-table-container">
+ <table class="data">
+ <thead>
+ <tr>
+ <th>Date</th>
+ <th>Event</th>
+ <th>Note</th>
+ </tr>
+ </thead>
+ <tbody>
+ {% for log in logs %}
+ <tr class="{{ log.event }}">
+ <td>{{ log.date_str }}</td>
+ <td>{{ log.description }}</td>
+ <td>{{ log.note }}</td>
+ </tr>
+ {% endfor %}
+ </tbody>
+ </table>
+ </div>
+ </div>
+ </body>
+</html>
diff --git a/jimbrella/templates/admin/umbrellas.html b/jimbrella/templates/admin/umbrellas.html
index 6bb4af5..15b6f02 100644
--- a/jimbrella/templates/admin/umbrellas.html
+++ b/jimbrella/templates/admin/umbrellas.html
@@ -15,7 +15,11 @@
{% for umb in umbrellas %}
{% if umb.status in ["lent", "overdue"] %}
<tr class="{{ umb.status }}">
- <th rowspan=5>{{ umb.serial }} <br /> {{ umb.status }}</th>
+ <th rowspan=5>
+ {{ umb.serial }} <br />
+ {{ umb.status }} <br />
+ <a href="/admin/umbrellas?edit={{ umb.serial }}">Edit</a>
+ </th>
<td>{{ umb.tenant_name }}</td>
</tr>
<tr class="{{ umb.status }}"><td>{{ umb.tenant_id }}</td></tr>
@@ -47,8 +51,40 @@
</tr>
</thead>
<tbody>
+ {% macro status_opt(umb, status) -%}
+ <option value="{{ status }}"
+ {% if umb.status == status %}
+ selected
+ {% endif %}>
+ {{ status }}
+ </option>
+ {%- endmacro %}
+
{% for umb in umbrellas %}
<tr class="{{ umb.status }}">
+ {% if umb.serial == edit %}
+ <form method="post" action="/admin/umbrellas/edit">
+ <input name="serial" type="number" value="{{ umb.serial }}" hidden/>
+ <td>{{ umb.serial }}</td>
+ <td>
+ <select name="status">
+ {% for status in ["available", "lent", "overdue"] %}
+ {{ status_opt(umb, status) }}
+ {% endfor %}
+ </select>
+ </td>
+ <td><input name="tenant_name" value="{{ umb.tenant_name }}" class="db-edit" /></td>
+ <td><input name="tenant_id" type="number" value="{{ umb.tenant_id }}" class="db-edit" /></td>
+ <td><input name="tenant_phone" type="tel" value="{{ umb.tenant_phone }}" class="db-edit" /></td>
+ <td><input name="lent_at" type="datetime-local" value="{{ umb.lent_at_str }}"/></td>
+ <td>{{ umb.lent_time_ago_str }}
+ {% if umb.status in ["lent", "overdue"] %} ago {% endif %}
+ </td>
+ <td>
+ <input type="submit" value="Submit" />
+ </td>
+ </form>
+ {% else %}
<td>{{ umb.serial }}</td>
<td>{{ umb.status }}</td>
<td>{{ umb.tenant_name }}</td>
@@ -58,6 +94,10 @@
<td>{{ umb.lent_time_ago_str }}
{% if umb.status in ["lent", "overdue"] %} ago {% endif %}
</td>
+ <td>
+ <a href="/admin/umbrellas?edit={{ umb.serial }}">Edit</a>
+ </td>
+ {% endif %}
</tr>
{% endfor %}
</tbody>
diff --git a/jimbrella/web.py b/jimbrella/web.py
index 4237055..434de57 100644
--- a/jimbrella/web.py
+++ b/jimbrella/web.py
@@ -1,4 +1,4 @@
-from flask import Flask, request, render_template
+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 *
@@ -25,12 +25,33 @@ def admin_index():
@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()