aboutsummaryrefslogtreecommitdiff
path: root/wiki.py
blob: 617f841a34c0bd20276513a8af0230df09b80db3 (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
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)