From: Michael Tremer Date: Sat, 1 Jul 2023 10:39:13 +0000 (+0000) Subject: docs: Move edit handler X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d75804696449d57b58bdeaa88a4f8cbe2c294f94;p=ipfire.org.git docs: Move edit handler Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index 1dfc56ac..7962435d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -202,6 +202,7 @@ templates_docs_DATA = \ src/templates/docs/base.html \ src/templates/docs/confirm-restore.html \ src/templates/docs/diff.html \ + src/templates/docs/edit.html \ src/templates/docs/page.html \ src/templates/docs/recent-changes.html \ src/templates/docs/revisions.html \ @@ -347,8 +348,7 @@ templates_voip_modules_DATA = \ templates_voip_modulesdir = $(templates_voipdir)/modules templates_wiki_DATA = \ - src/templates/wiki/confirm-delete.html \ - src/templates/wiki/edit.html + src/templates/wiki/confirm-delete.html templates_wikidir = $(templatesdir)/wiki diff --git a/src/templates/wiki/edit.html b/src/templates/docs/edit.html similarity index 100% rename from src/templates/wiki/edit.html rename to src/templates/docs/edit.html diff --git a/src/web/__init__.py b/src/web/__init__.py index f76d31d2..bfd2934f 100644 --- a/src/web/__init__.py +++ b/src/web/__init__.py @@ -146,6 +146,8 @@ class Application(tornado.web.Application): (r"/docs/search", docs.SearchHandler), (r"/docs/tree", docs.TreeHandler), (r"/docs/watchlist", docs.WatchlistHandler), + (r"/docs/([A-Za-z0-9\-_\/]+)?/_edit", docs.ActionEditHandler), + (r"/docs/([A-Za-z0-9\-_\/]+)?/_render", docs.ActionRenderHandler), (r"/docs((?:[A-Za-z0-9\-_\/]+)?(?:.*)\.(?:\w+))$", docs.FileHandler), (r"/docs([A-Za-z0-9\-_\/]+)?", docs.PageHandler), @@ -347,8 +349,6 @@ class Application(tornado.web.Application): # Actions (r"((?:[A-Za-z0-9\-_\/]+)?(?:.*)\.(?:\w+))/_delete", wiki.ActionDeleteHandler), - (r"([A-Za-z0-9\-_\/]+)?/_edit", wiki.ActionEditHandler), - (r"([A-Za-z0-9\-_\/]+)?/_render", wiki.ActionRenderHandler), (r"([A-Za-z0-9\-_\/]+)?/_(watch|unwatch)", wiki.ActionWatchHandler), (r"/actions/restore", wiki.ActionRestoreHandler), (r"/actions/upload", wiki.ActionUploadHandler), diff --git a/src/web/docs.py b/src/web/docs.py index 0a55fffa..ae93ce9b 100644 --- a/src/web/docs.py +++ b/src/web/docs.py @@ -138,6 +138,82 @@ class FileHandler(base.BaseHandler): self.finish(blob) +class ActionEditHandler(base.BaseHandler): + @tornado.web.authenticated + def get(self, path): + if path is None: + path = "/" + + # Check permissions + if not self.backend.wiki.check_acl(path, self.current_user): + raise tornado.web.HTTPError(403, "Access to %s not allowed for %s" % (path, self.current_user)) + + # Fetch the wiki page + page = self.backend.wiki.get_page(path) + + # Empty page if it was deleted + if page and page.was_deleted(): + page = None + + # Render page + self.render("docs/edit.html", page=page, path=path) + + @tornado.web.authenticated + def post(self, path): + if path is None: + path = "/" + + # Check permissions + if not self.backend.wiki.check_acl(path, self.current_user): + raise tornado.web.HTTPError(403, "Access to %s not allowed for %s" % (path, self.current_user)) + + content = self.get_argument("content", None) + changes = self.get_argument("changes") + + # Create a new page in the database + with self.db.transaction(): + page = self.backend.wiki.create_page(path, + self.current_user, content, changes=changes, address=self.get_remote_ip()) + + # Add user as a watcher if wanted + watch = self.get_argument("watch", False) + if watch: + page.add_watcher(self.current_user) + + # Redirect back + if page.was_deleted(): + self.redirect("/") + else: + self.redirect(page.url) + + def on_finish(self): + """ + Updates the search index after the page has been edited + """ + # This is being executed in the background and after + # the response has been set to the client + with self.db.transaction(): + self.backend.wiki.refresh() + + +class ActionRenderHandler(base.BaseHandler): + def check_xsrf_cookie(self): + pass # disabled + + @tornado.web.authenticated + @base.ratelimit(minutes=5, requests=180) + def post(self, path): + if path is None: + path = "/" + + content = self.get_argument("content") + + # Render the content + html = self.backend.wiki.render(path, content) + + self.finish(html) + + class SearchHandler(base.BaseHandler): @base.ratelimit(minutes=5, requests=25) def get(self): diff --git a/src/web/wiki.py b/src/web/wiki.py index 52a04414..0c9858d7 100644 --- a/src/web/wiki.py +++ b/src/web/wiki.py @@ -5,64 +5,6 @@ import tornado.web from . import base from . import ui_modules -class ActionEditHandler(base.BaseHandler): - @tornado.web.authenticated - def get(self, path): - if path is None: - path = "/" - - # Check permissions - if not self.backend.wiki.check_acl(path, self.current_user): - raise tornado.web.HTTPError(403, "Access to %s not allowed for %s" % (path, self.current_user)) - - # Fetch the wiki page - page = self.backend.wiki.get_page(path) - - # Empty page if it was deleted - if page and page.was_deleted(): - page = None - - # Render page - self.render("wiki/edit.html", page=page, path=path) - - @tornado.web.authenticated - def post(self, path): - if path is None: - path = "/" - - # Check permissions - if not self.backend.wiki.check_acl(path, self.current_user): - raise tornado.web.HTTPError(403, "Access to %s not allowed for %s" % (path, self.current_user)) - - content = self.get_argument("content", None) - changes = self.get_argument("changes") - - # Create a new page in the database - with self.db.transaction(): - page = self.backend.wiki.create_page(path, - self.current_user, content, changes=changes, address=self.get_remote_ip()) - - # Add user as a watcher if wanted - watch = self.get_argument("watch", False) - if watch: - page.add_watcher(self.current_user) - - # Redirect back - if page.was_deleted(): - self.redirect("/") - else: - self.redirect(page.url) - - def on_finish(self): - """ - Updates the search index after the page has been edited - """ - # This is being executed in the background and after - # the response has been set to the client - with self.db.transaction(): - self.backend.wiki.refresh() - - class ActionUploadHandler(base.BaseHandler): @tornado.web.authenticated @base.ratelimit(minutes=60, requests=24) @@ -177,24 +119,6 @@ class ActionWatchHandler(base.BaseHandler): self.redirect(page.url) -class ActionRenderHandler(base.BaseHandler): - def check_xsrf_cookie(self): - pass # disabled - - @tornado.web.authenticated - @base.ratelimit(minutes=5, requests=180) - def post(self, path): - if path is None: - path = "/" - - content = self.get_argument("content") - - # Render the content - html = self.backend.wiki.render(path, content) - - self.finish(html) - - class FilesHandler(base.BaseHandler): @tornado.web.authenticated def get(self, path):