]>
git.ipfire.org Git - ipfire.org.git/blob - src/web/wiki.py
8 from . import ui_modules
10 class ActionEditHandler(auth
.CacheMixin
, base
.BaseHandler
):
11 @tornado.web
.authenticated
17 if not self
.backend
.wiki
.check_acl(path
, self
.current_user
):
18 raise tornado
.web
.HTTPError(403, "Access to %s not allowed for %s" % (path
, self
.current_user
))
21 page
= self
.backend
.wiki
.get_page(path
)
23 # Empty page if it was deleted
24 if page
and page
.was_deleted():
28 self
.render("wiki/edit.html", page
=page
, path
=path
)
30 @tornado.web
.authenticated
36 if not self
.backend
.wiki
.check_acl(path
, self
.current_user
):
37 raise tornado
.web
.HTTPError(403, "Access to %s not allowed for %s" % (path
, self
.current_user
))
39 content
= self
.get_argument("content", None)
40 changes
= self
.get_argument("changes")
42 # Create a new page in the database
43 with self
.db
.transaction():
44 page
= self
.backend
.wiki
.create_page(path
,
45 self
.current_user
, content
, changes
=changes
, address
=self
.get_remote_ip())
47 # Add user as a watcher if wanted
48 watch
= self
.get_argument("watch", False)
50 page
.add_watcher(self
.current_user
)
53 if page
.was_deleted():
56 self
.redirect(page
.url
)
60 Updates the search index after the page has been edited
62 # This is being executed in the background and after
63 # the response has been set to the client
64 with self
.db
.transaction():
65 self
.backend
.wiki
.refresh()
68 class ActionUploadHandler(auth
.CacheMixin
, base
.BaseHandler
):
69 @tornado.web
.authenticated
70 @base.ratelimit(minutes
=60, requests
=24)
72 path
= self
.get_argument("path")
75 if not self
.backend
.wiki
.check_acl(path
, self
.current_user
):
76 raise tornado
.web
.HTTPError(403, "Access to %s not allowed for %s" % (path
, self
.current_user
))
79 filename
, data
, mimetype
= self
.get_file("file")
81 # XXX check valid mimetypes
83 with self
.db
.transaction():
84 file = self
.backend
.wiki
.upload(path
, filename
, data
,
85 mimetype
=mimetype
, author
=self
.current_user
,
86 address
=self
.get_remote_ip())
88 except TypeError as e
:
91 self
.redirect("%s/_files" % path
)
94 class ActionWatchHandler(auth
.CacheMixin
, base
.BaseHandler
):
95 @tornado.web
.authenticated
96 @base.ratelimit(minutes
=60, requests
=180)
97 def get(self
, path
, action
):
101 page
= self
.backend
.wiki
.get_page(path
)
103 raise tornado
.web
.HTTPError(404, "Page does not exist: %s" % path
)
106 if not self
.backend
.wiki
.check_acl(path
, self
.current_user
):
107 raise tornado
.web
.HTTPError(403, "Access to %s not allowed for %s" % (path
, self
.current_user
))
109 with self
.db
.transaction():
110 if action
== "watch":
111 page
.add_watcher(self
.current_user
)
112 elif action
== "unwatch":
113 page
.remove_watcher(self
.current_user
)
115 # Redirect back to page
116 self
.redirect(page
.url
)
119 class ActionRenderHandler(auth
.CacheMixin
, base
.BaseHandler
):
120 def check_xsrf_cookie(self
):
123 @tornado.web
.authenticated
124 @base.ratelimit(minutes
=5, requests
=180)
125 def post(self
, path
):
129 content
= self
.get_argument("content")
132 html
= self
.backend
.wiki
.render(path
, content
)
137 class FilesHandler(auth
.CacheMixin
, base
.BaseHandler
):
138 @tornado.web
.authenticated
144 if not self
.backend
.wiki
.check_acl(path
, self
.current_user
):
145 raise tornado
.web
.HTTPError(403, "Access to %s not allowed for %s" % (path
, self
.current_user
))
147 files
= self
.backend
.wiki
.get_files(path
)
149 self
.render("wiki/files/index.html", path
=path
, files
=files
)
152 class FileHandler(base
.BaseHandler
):
155 return self
.get_argument("action", None)
159 if not self
.backend
.wiki
.check_acl(path
, self
.current_user
):
160 raise tornado
.web
.HTTPError(403, "Access to %s not allowed for %s" % (path
, self
.current_user
))
163 file = self
.backend
.wiki
.get_file_by_path(path
)
165 raise tornado
.web
.HTTPError(404, "Could not find %s" % path
)
168 if self
.action
== "detail":
171 for breadcrumb
, title
in self
.backend
.wiki
.make_breadcrumbs(path
):
172 page
= self
.backend
.wiki
.get_page(breadcrumb
)
176 self
.render("wiki/files/detail.html", page
=page
, file=file)
179 size
= self
.get_argument_int("s", None)
181 # Check if image should be resized
182 if file.is_image() and size
:
183 blob
= file.get_thumbnail(size
)
188 self
.set_header("Content-Type", file.mimetype
or "application/octet-stream")
189 self
.set_header("Content-Length", len(blob
))
192 self
.set_expires(3600)
198 class PageHandler(auth
.CacheMixin
, base
.BaseHandler
):
201 return self
.get_argument("action", None)
203 def write_error(self
, status_code
, **kwargs
):
204 # Render a custom page for 404
205 if status_code
== 404:
206 self
.render("wiki/404.html", **kwargs
)
209 # Otherwise raise this to one layer above
210 super().write_error(status_code
, **kwargs
)
212 @tornado.web
.removeslash
218 if not self
.backend
.wiki
.check_acl(path
, self
.current_user
):
219 raise tornado
.web
.HTTPError(403, "Access to %s not allowed for %s" % (path
, self
.current_user
))
221 # Check if we are asked to render a certain revision
222 revision
= self
.get_argument("revision", None)
224 # Fetch the wiki page
225 page
= self
.backend
.wiki
.get_page(path
, revision
=revision
)
228 if self
.action
== "diff":
230 a
= self
.get_argument("a")
231 b
= self
.get_argument("b")
233 # Fetch both versions of the page
234 a
= self
.backend
.wiki
.get_page(path
, revision
=a
)
235 b
= self
.backend
.wiki
.get_page(path
, revision
=b
)
237 raise tornado
.web
.HTTPError(404)
239 # Cannot render a diff for the identical page
241 raise tornado
.web
.HTTPError(400)
243 # Make sure that b is newer than a
247 self
.render("wiki/diff.html", page
=page
, a
=a
, b
=b
)
251 elif self
.action
== "revisions":
252 self
.render("wiki/revisions.html", page
=page
)
255 # If the page does not exist, we send 404
256 if not page
or page
.was_deleted():
257 raise tornado
.web
.HTTPError(404)
259 # Fetch the latest revision
260 latest_revision
= page
.get_latest_revision()
263 self
.render("wiki/page.html", page
=page
, latest_revision
=latest_revision
)
266 class SearchHandler(auth
.CacheMixin
, base
.BaseHandler
):
267 @base.ratelimit(minutes
=5, requests
=25)
269 q
= self
.get_argument("q")
271 pages
= self
.backend
.wiki
.search(q
, account
=self
.current_user
, limit
=50)
273 self
.render("wiki/search-results.html", q
=q
, pages
=pages
)
276 class RecentChangesHandler(auth
.CacheMixin
, base
.BaseHandler
):
278 recent_changes
= self
.backend
.wiki
.get_recent_changes(self
.current_user
, limit
=50)
280 self
.render("wiki/recent-changes.html", recent_changes
=recent_changes
)
283 class WatchlistHandler(auth
.CacheMixin
, base
.BaseHandler
):
284 @tornado.web
.authenticated
286 pages
= self
.backend
.wiki
.get_watchlist(self
.current_user
)
288 self
.render("wiki/watchlist.html", pages
=pages
)
291 class WikiDiffModule(ui_modules
.UIModule
):
292 differ
= difflib
.Differ()
294 def render(self
, a
, b
):
295 diff
= self
.differ
.compare(
296 a
.markdown
.splitlines(),
297 b
.markdown
.splitlines(),
300 return self
.render_string("wiki/modules/diff.html", diff
=diff
)
303 class WikiListModule(ui_modules
.UIModule
):
304 def render(self
, pages
, link_revision
=False, show_breadcrumbs
=True,
305 show_author
=True, show_changes
=False):
306 return self
.render_string("wiki/modules/list.html", link_revision
=link_revision
,
307 pages
=pages
, show_breadcrumbs
=show_breadcrumbs
,
308 show_author
=show_author
, show_changes
=show_changes
)
311 class WikiNavbarModule(ui_modules
.UIModule
):
315 Returns the path of the page (without any actions)
317 path
= self
.request
.path
.split("/")
319 if path
and path
[-1].startswith("_"):
322 return "/".join(path
)
324 def render(self
, suffix
=None):
325 _
= self
.locale
.translate
328 page
= self
.request
.path
.split("/")
330 # Drop the action bit
331 if page
and page
[-1].startswith("_"):
334 page
= "/".join(page
)
336 breadcrumbs
= self
.backend
.wiki
.make_breadcrumbs(page
)
337 title
= self
.backend
.wiki
.get_page_title(page
)
339 if self
.request
.path
.endswith("/_edit"):
341 elif self
.request
.path
.endswith("/_files"):
344 return self
.render_string("wiki/modules/navbar.html",
345 breadcrumbs
=breadcrumbs
, page
=page
, page_title
=title
, suffix
=suffix
)