aboutsummaryrefslogtreecommitdiff
path: root/model.py
blob: b4e0b1c45ba8e23c5c41080dd5be02eee80fa48d (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
import sqlite3
from datetime import datetime

import inflection
import markdown


def bootstrap_db(db):
    with open("schema.sql", "rt") as fd:
        schema = fd.read()
        con = sqlite3.connect(db)
        cur = con.cursor()
        cur.executescript(schema)
        con.commit()
        cur.close()
        con.close()


class Page(object):
    @classmethod
    def now(cls):
        return datetime.utcnow().isoformat(sep=" ", timespec="seconds")

    def __init__(
        self,
        name="WikiHome",
        version=None,
        content="",
        changelog=None,
        updated_at=None,
        history=False,
    ):
        now = Page.now()
        if updated_at is None:
            updated_at = now

        self.name = name
        self.version = version
        self.content = content
        self.changelog = changelog
        self.updated_at = updated_at
        self.history = history

    def render(self):
        return markdown.markdown(self.content)

    @property
    def title(self):
        pretty = inflection.titleize(self.name)
        if self.history:
            pretty = f"{pretty} (history)"
        return pretty


class History(object):
    def __init__(self, version, changelog, updated_at):
        self.version = version
        self.changelog = changelog
        self.updated_at = updated_at


def get_page(db, name, version=None):
    cur = db.cursor()
    if version:
        cur.execute(
            "SELECT name, version, content, changelog, updated_at"
            " FROM pages"
            " WHERE name = :name AND version = :version",
            dict(name=name, version=version),
        )
    else:
        cur.execute(
            "SELECT name, version, content, changelog, updated_at"
            " FROM pages"
            " WHERE name = :name ORDER BY updated_at DESC LIMIT 1",
            dict(name=name),
        )
    row = cur.fetchone()
    cur.close()

    if row:
        return Page(*row, history=version is not None)
    else:
        return Page(name=name)


def save_page(db, page):
    cur = db.cursor()
    cur.execute(
        "INSERT INTO pages"
        "  VALUES(:name, :version, :content, :changelog, :updated_at)",
        dict(
            name=page.name,
            version=page.version,
            content=page.content,
            changelog=page.changelog,
            updated_at=page.updated_at,
        ),
    )
    db.commit()
    cur.close()


def get_page_history(db, name):
    cur = db.cursor()
    cur.execute(
        "SELECT version, changelog, updated_at"
        " FROM pages"
        " WHERE name = :name ORDER BY updated_at DESC",
        dict(name=name),
    )
    rows = cur.fetchall()
    cur.close()

    if rows:
        return [History(*r) for r in rows]