aboutsummaryrefslogtreecommitdiffstats
path: root/pysite/views/wiki/source.py
blob: 836744477d0f2ee39f64a7a5c487c7e48937f933 (plain) (blame)
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
from flask import redirect, url_for
from pygments import highlight
from pygments.formatters.html import HtmlFormatter
from pygments.lexers import get_lexer_by_name
from werkzeug.exceptions import NotFound

from pysite.base_route import RouteView
from pysite.constants import DEBUG_MODE, EDITOR_ROLES
from pysite.mixins import DBMixin


class PageView(RouteView, DBMixin):
    path = "/source/<path:page>"  # "path" means that it accepts slashes
    name = "source"
    table_name = "wiki"

    def get(self, page):
        obj = self.db.get(self.table_name, page)

        if obj is None:
            if self.is_staff():
                return redirect(url_for("wiki.edit", page=page, can_edit=False))

            raise NotFound()

        rst = obj["rst"]
        rst = highlight(rst, get_lexer_by_name("rst"), HtmlFormatter(preclass="code", linenos="inline"))
        return self.render("wiki/page_source.html", page=page, data=obj, rst=rst, can_edit=self.is_staff())

    def is_staff(self):
        if DEBUG_MODE:
            return True
        if not self.logged_in:
            return False

        roles = self.user_data.get("roles", [])

        for role in roles:
            if role in EDITOR_ROLES:
                return True

        return False