summaryrefslogtreecommitdiff
path: root/guard/templates.py
blob: 3cc137352d5caf723b8559bc91360d994f195443 (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
from . import model

from typing import Iterable

from lxml.builder import E
from lxml import etree
from flask import url_for


def _html(head: etree.Element, body: etree.Element) -> str:
    return etree.tostring(E.html(
        head,
        body
    ), encoding='utf-8')


def login() -> str:
    return _html(E.head(E.titile("Login")), E.body(E.form(
        E.input(type="password", name="secret", placeholder="Secret admin token"),
        E.button("login", type="submit"),
        method="POST"
    )))


def _index_access(accesses: Iterable[model.Access]):
    return map(lambda access: (
        E.tr(
            E.td(access.token),
            E.td(access.pattern),
            E.td(str(access.create_date)),
            E.td(access.comment),
            E.td(
                E.form(
                    E.input(type="hidden", name="id", value=str(access.id)),
                    E.button("X", title="Delete", type="submit"),
                    method="POST", action=url_for("delete")))
        )
    ), accesses)


def index(serverport: str, cookiename: str) -> str:
    return _html(
        E.head(
            E.title("Manage access tokens"),
            E.script("", src="static/main.js")
        ),
        E.body(E.div(
            E.p(E.a("Logout", href=url_for("logout"))),
            E.p("Call the write-service like: ",
                E.pre("http://thisMachine:{}/write".format(serverport)),
                "with the token in a cookie named {}".format(cookiename)),
            E.p("Or directly in the url: ",
                E.pre("http://thisMachine:{}/write/<token>".format(serverport))),
            E.form(
                E.input(type="hidden", name="action", value="create"),
                E.input(name="token", placeholder="Token secret", id="tokenField"),
                E.input(value="↻", type="button", onclick="genToken()"),
                E.input(name="pattern", placeholder="Paths pattern"),
                E.input(name="comment", placeholder="Comment"),
                E.button("Create", type="submit"),
                method="POST"),
            E.hr(),
            E.table(
                E.tr(
                    E.th("cookie: " + cookiename + " value"), E.th("Pattern"),
                    E.th("Create Date"), E.th("Comment"), E.th()
                ), border="1", style="width: 100%",
                *_index_access(model.Access.select().order_by(model.Access.create_date))
            )))
    )