]>
git.ipfire.org Git - ipfire.org.git/blob - src/web/docs.py
7 from . import ui_modules
9 class PageHandler(base
.BaseHandler
):
12 return self
.get_argument("action", None)
14 def write_error(self
, status_code
, **kwargs
):
15 # Render a custom page for 404
16 if status_code
== 404:
17 self
.render("wiki/404.html", **kwargs
)
20 # Otherwise raise this to one layer above
21 super().write_error(status_code
, **kwargs
)
23 @tornado.web
.removeslash
29 if not self
.backend
.wiki
.check_acl(path
, self
.current_user
):
30 raise tornado
.web
.HTTPError(403, "Access to %s not allowed for %s" % (path
, self
.current_user
))
32 # Check if we are asked to render a certain revision
33 revision
= self
.get_argument("revision", None)
36 page
= self
.backend
.wiki
.get_page(path
, revision
=revision
)
39 if self
.action
== "diff":
41 a
= self
.get_argument("a")
42 b
= self
.get_argument("b")
44 # Fetch both versions of the page
45 a
= self
.backend
.wiki
.get_page(path
, revision
=a
)
46 b
= self
.backend
.wiki
.get_page(path
, revision
=b
)
48 raise tornado
.web
.HTTPError(404)
50 # Cannot render a diff for the identical page
52 raise tornado
.web
.HTTPError(400)
54 # Make sure that b is newer than a
58 self
.render("docs/diff.html", page
=page
, a
=a
, b
=b
)
62 elif self
.action
== "restore":
63 self
.render("docs/confirm-restore.html", page
=page
)
67 elif self
.action
== "revisions":
68 self
.render("docs/revisions.html", page
=page
)
71 # If the page does not exist, we send 404
72 if not page
or page
.was_deleted():
73 # Handle /start links which were in the format of DokuWiki
74 if path
.endswith("/start"):
75 # Strip /start from path
76 path
= path
[:-6] or "/"
78 # Redirect user to page if it exists
79 page
= self
.backend
.wiki
.page_exists(path
)
83 raise tornado
.web
.HTTPError(404)
85 # Fetch the latest revision
86 latest_revision
= page
.get_latest_revision()
89 self
.render("docs/page.html", page
=page
, latest_revision
=latest_revision
)
92 class FileHandler(base
.BaseHandler
):
95 return self
.get_argument("action", None)
99 if not self
.backend
.wiki
.check_acl(path
, self
.current_user
):
100 raise tornado
.web
.HTTPError(403, "Access to %s not allowed for %s" % (path
, self
.current_user
))
102 # Check if we are asked to render a certain revision
103 revision
= self
.get_argument("revision", None)
106 file = self
.backend
.wiki
.get_file_by_path(path
, revision
=revision
)
108 raise tornado
.web
.HTTPError(404, "Could not find %s" % path
)
111 if self
.action
== "detail":
114 for breadcrumb
, title
in self
.backend
.wiki
.make_breadcrumbs(path
):
115 page
= self
.backend
.wiki
.get_page(breadcrumb
)
119 self
.render("wiki/files/detail.html", page
=page
, file=file)
122 size
= self
.get_argument_int("s", None)
124 # Check if image should be resized
125 if size
and file.is_bitmap_image():
126 blob
= file.get_thumbnail(size
)
131 self
.set_header("Content-Type", file.mimetype
or "application/octet-stream")
132 self
.set_header("Content-Length", len(blob
))
135 self
.set_expires(3600)
141 class HeaderModule(ui_modules
.UIModule
):
145 Returns the path of the page (without any actions)
147 path
= self
.request
.path
.removeprefix("/docs")
149 return "/".join((p
for p
in path
.split("/") if not p
.startswith("_")))
151 def render(self
, suffix
=None):
152 _
= self
.locale
.translate
154 breadcrumbs
= self
.backend
.wiki
.make_breadcrumbs(self
.page
)
155 title
= self
.backend
.wiki
.get_page_title(self
.page
)
157 if self
.request
.path
.endswith("/_edit"):
159 elif self
.request
.path
.endswith("/_files"):
162 return self
.render_string("docs/modules/header.html",
163 breadcrumbs
=breadcrumbs
, page
=self
.page
, page_title
=title
, suffix
=suffix
)