aboutsummaryrefslogtreecommitdiff
path: root/wiki.py
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2024-03-23 19:01:29 +0000
committerJuan J. Martinez <jjm@usebox.net>2024-03-23 19:10:43 +0000
commit0a5471217b9f562b92f32802de4260390f639880 (patch)
treedaf0655fe2753351d7fac9010e3b50c499194bb2 /wiki.py
downloadpersonal-wiki-pybottle-0a5471217b9f562b92f32802de4260390f639880.tar.gz
personal-wiki-pybottle-0a5471217b9f562b92f32802de4260390f639880.zip
Initial import
Diffstat (limited to 'wiki.py')
-rw-r--r--wiki.py89
1 files changed, 89 insertions, 0 deletions
diff --git a/wiki.py b/wiki.py
new file mode 100644
index 0000000..617f841
--- /dev/null
+++ b/wiki.py
@@ -0,0 +1,89 @@
+import logging
+from datetime import datetime
+import logging.config
+import json
+from hashlib import md5
+
+from bottle import Bottle, static_file, run, template, redirect, request
+from bottle_sqlite import SQLitePlugin
+
+import model
+
+with open("logger.json", "rt") as fd:
+ logging.config.dictConfig(json.load(fd))
+log = logging.getLogger("wiki")
+
+app = Bottle()
+app.config.load_config("wiki.conf")
+
+model.bootstrap_db(app.config["sqlite.db"])
+
+app.install(SQLitePlugin(dbfile=app.config["sqlite.db"]))
+
+DEV = app.config["wiki.dev"]
+
+if DEV:
+ log.warning("Running in dev mode")
+
+ @app.route("/static/<filename:path>")
+ def send_static(filename):
+ return static_file(filename, root="static/")
+
+
+@app.route("/")
+def index():
+ redirect("/WikiHome")
+
+
+@app.route("/<name>")
+def page(db, name):
+ return template(
+ "page", dict(page=model.get_page(db, name))
+ )
+
+
+@app.get("/edit/<name>")
+def edit(db, name):
+ return template(
+ "edit", dict(page=model.get_page(db, name))
+ )
+
+
+@app.get("/history/<name>")
+def history(db, name):
+ return template(
+ "history",
+ dict(
+ page=model.Page(name=name),
+ history=model.get_page_history(db, name),
+ ),
+ )
+
+
+@app.get("/history/<name>/<version>")
+def history_view(db, name, version):
+ return template(
+ "page",
+ dict(
+ page=model.get_page(db, name, version),
+ ),
+ )
+
+
+@app.post("/edit/<name>")
+def edit_save(db, name):
+ page = model.get_page(db, name)
+
+ if page.version is not None:
+ page.updated_at = datetime.utcnow()
+ page.content = request.forms.get("content")
+ page.changelog = request.forms.get("changelog")
+ page.version = md5(str(page.updated_at).encode()).hexdigest()
+
+ model.save_page(db, page)
+
+ redirect(f"/{name}")
+
+
+if __name__ == "__main__":
+ run(app, reloader=DEV, host="localhost", port=8080)