]>
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("docs/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 FilesHandler(base
.BaseHandler
):
93 @tornado.web
.authenticated
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 files
= self
.backend
.wiki
.get_files(path
)
104 self
.render("docs/files/index.html", path
=path
, files
=files
)
107 class FileHandler(base
.BaseHandler
):
110 return self
.get_argument("action", None)
112 async def get(self
, path
):
114 if not self
.backend
.wiki
.check_acl(path
, self
.current_user
):
115 raise tornado
.web
.HTTPError(403, "Access to %s not allowed for %s" % (path
, self
.current_user
))
117 # Check if we are asked to render a certain revision
118 revision
= self
.get_argument("revision", None)
121 file = self
.backend
.wiki
.get_file_by_path(path
, revision
=revision
)
123 raise tornado
.web
.HTTPError(404, "Could not find %s" % path
)
126 if self
.action
== "detail":
129 for breadcrumb
, title
in self
.backend
.wiki
.make_breadcrumbs(path
):
130 page
= self
.backend
.wiki
.get_page(breadcrumb
)
134 self
.render("docs/files/detail.html", page
=page
, file=file)
137 size
= self
.get_argument_int("s", None)
139 # Check if image should be resized
140 if size
and file.is_bitmap_image():
141 blob
= await file.get_thumbnail(size
)
146 self
.set_header("Content-Type", file.mimetype
or "application/octet-stream")
147 self
.set_header("Content-Length", len(blob
))
150 self
.set_expires(3600)
156 class EditHandler(base
.BaseHandler
):
157 @tornado.web
.authenticated
163 if not self
.backend
.wiki
.check_acl(path
, self
.current_user
):
164 raise tornado
.web
.HTTPError(403, "Access to %s not allowed for %s" % (path
, self
.current_user
))
166 # Fetch the wiki page
167 page
= self
.backend
.wiki
.get_page(path
)
169 # Empty page if it was deleted
170 if page
and page
.was_deleted():
174 self
.render("docs/edit.html", page
=page
, path
=path
)
176 @tornado.web
.authenticated
177 def post(self
, path
):
182 if not self
.backend
.wiki
.check_acl(path
, self
.current_user
):
183 raise tornado
.web
.HTTPError(403, "Access to %s not allowed for %s" % (path
, self
.current_user
))
185 content
= self
.get_argument("content", None)
186 changes
= self
.get_argument("changes")
188 # Create a new page in the database
189 with self
.db
.transaction():
190 page
= self
.backend
.wiki
.create_page(path
,
191 self
.current_user
, content
, changes
=changes
, address
=self
.get_remote_ip())
193 # Add user as a watcher if wanted
194 watch
= self
.get_argument("watch", False)
196 page
.add_watcher(self
.current_user
)
199 if page
.was_deleted():
202 self
.redirect(page
.url
)
206 Updates the search index after the page has been edited
208 # This is being executed in the background and after
209 # the response has been set to the client
210 with self
.db
.transaction():
211 self
.backend
.wiki
.refresh()
214 class RenderHandler(base
.BaseHandler
):
215 def check_xsrf_cookie(self
):
218 @tornado.web
.authenticated
219 @base.ratelimit(minutes
=5, requests
=180)
220 def post(self
, path
):
224 content
= self
.get_argument("content")
227 renderer
= self
.backend
.wiki
.render(path
, content
)
229 self
.finish(renderer
.html
)
232 class RestoreHandler(base
.BaseHandler
):
233 @tornado.web
.authenticated
234 @base.ratelimit(minutes
=60, requests
=24)
236 path
= self
.get_argument("path")
239 if not self
.backend
.wiki
.check_acl(path
, self
.current_user
):
240 raise tornado
.web
.HTTPError(403, "Access to %s not allowed for %s" % (path
, self
.current_user
))
242 # Check if we are asked to render a certain revision
243 revision
= self
.get_argument("revision", None)
244 comment
= self
.get_argument("comment", None)
246 # Fetch the wiki page
247 page
= self
.backend
.wiki
.get_page(path
, revision
=revision
)
249 with self
.db
.transaction():
251 author
=self
.current_user
,
252 address
=self
.get_remote_ip(),
256 # Redirect back to page
257 self
.redirect(page
.page
)
260 class UploadHandler(base
.BaseHandler
):
261 @tornado.web
.authenticated
262 @base.ratelimit(minutes
=60, requests
=24)
264 path
= self
.get_argument("path")
267 if not self
.backend
.wiki
.check_acl(path
, self
.current_user
):
268 raise tornado
.web
.HTTPError(403, "Access to %s not allowed for %s" % (path
, self
.current_user
))
271 filename
, data
, mimetype
= self
.get_file("file")
273 # Use filename from request if any
274 filename
= self
.get_argument("filename", filename
)
276 # XXX check valid mimetypes
278 with self
.db
.transaction():
279 file = self
.backend
.wiki
.upload(path
, filename
, data
,
280 mimetype
=mimetype
, author
=self
.current_user
,
281 address
=self
.get_remote_ip())
283 except TypeError as e
:
286 self
.redirect("/docs%s/_files" % path
)
289 class WatchHandler(base
.BaseHandler
):
290 @tornado.web
.authenticated
291 @base.ratelimit(minutes
=60, requests
=180)
292 def get(self
, path
, action
):
296 page
= self
.backend
.wiki
.get_page(path
)
298 raise tornado
.web
.HTTPError(404, "Page does not exist: %s" % path
)
301 if not self
.backend
.wiki
.check_acl(path
, self
.current_user
):
302 raise tornado
.web
.HTTPError(403, "Access to %s not allowed for %s" % (path
, self
.current_user
))
304 with self
.db
.transaction():
305 if action
== "watch":
306 page
.add_watcher(self
.current_user
)
307 elif action
== "unwatch":
308 page
.remove_watcher(self
.current_user
)
310 # Redirect back to page
311 self
.redirect(page
.url
)
314 class DeleteFileHandler(base
.BaseHandler
):
315 @tornado.web
.authenticated
318 if not self
.backend
.wiki
.check_acl(path
, self
.current_user
):
319 raise tornado
.web
.HTTPError(403, "Access to %s not allowed for %s" % (path
, self
.current_user
))
322 file = self
.backend
.wiki
.get_file_by_path(path
)
324 raise tornado
.web
.HTTPError(404, "Could not find %s" % path
)
326 self
.render("docs/confirm-delete.html", file=file)
328 @tornado.web
.authenticated
329 @base.ratelimit(minutes
=60, requests
=24)
330 def post(self
, path
):
332 if not self
.backend
.wiki
.check_acl(path
, self
.current_user
):
333 raise tornado
.web
.HTTPError(403, "Access to %s not allowed for %s" % (path
, self
.current_user
))
336 file = self
.backend
.wiki
.get_file_by_path(path
)
338 raise tornado
.web
.HTTPError(404, "Could not find %s" % path
)
340 with self
.db
.transaction():
341 file.delete(self
.current_user
)
343 self
.redirect("/docs%s/_files" % file.path
)
346 class SearchHandler(base
.BaseHandler
):
347 @base.ratelimit(minutes
=5, requests
=25)
349 q
= self
.get_argument("q")
352 with self
.db
.transaction():
353 pages
= self
.backend
.wiki
.search(q
, account
=self
.current_user
, limit
=50)
355 self
.render("docs/search-results.html", q
=q
, pages
=pages
)
358 class RecentChangesHandler(base
.BaseHandler
):
360 recent_changes
= self
.backend
.wiki
.get_recent_changes(self
.current_user
, limit
=50)
362 self
.render("docs/recent-changes.html", recent_changes
=recent_changes
)
365 class TreeHandler(base
.BaseHandler
):
367 self
.render("docs/tree.html", pages
=self
.backend
.wiki
)
370 class WatchlistHandler(base
.BaseHandler
):
371 @tornado.web
.authenticated
373 pages
= self
.backend
.wiki
.get_watchlist(self
.current_user
)
375 self
.render("docs/watchlist.html", pages
=pages
)
378 class ListModule(ui_modules
.UIModule
):
379 def render(self
, pages
, link_revision
=False, show_breadcrumbs
=True,
380 show_author
=True, show_changes
=False):
381 return self
.render_string("docs/modules/list.html", link_revision
=link_revision
,
382 pages
=pages
, show_breadcrumbs
=show_breadcrumbs
,
383 show_author
=show_author
, show_changes
=show_changes
)
386 class HeaderModule(ui_modules
.UIModule
):
390 Returns the path of the page (without any actions)
392 path
= self
.request
.path
.removeprefix("/docs")
394 return "/".join((p
for p
in path
.split("/") if not p
.startswith("_")))
396 def render(self
, suffix
=None):
397 _
= self
.locale
.translate
399 breadcrumbs
= self
.backend
.wiki
.make_breadcrumbs(self
.page
)
400 title
= self
.backend
.wiki
.get_page_title(self
.page
)
402 if self
.request
.path
.endswith("/_edit"):
404 elif self
.request
.path
.endswith("/_files"):
407 return self
.render_string("docs/modules/header.html",
408 breadcrumbs
=breadcrumbs
, page
=self
.page
, page_title
=title
, suffix
=suffix
)
411 class DiffModule(ui_modules
.UIModule
):
412 differ
= difflib
.Differ()
414 def render(self
, a
, b
):
415 diff
= self
.differ
.compare(
416 a
.markdown
.splitlines(),
417 b
.markdown
.splitlines(),
420 return self
.render_string("docs/modules/diff.html", diff
=diff
)