]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/python3 | |
2 | ||
3 | import difflib | |
4 | import tornado.web | |
5 | ||
6 | from . import auth | |
7 | from . import base | |
8 | from . import ui_modules | |
9 | ||
10 | class ActionEditHandler(auth.CacheMixin, base.BaseHandler): | |
11 | @tornado.web.authenticated | |
12 | def get(self, path): | |
13 | # Check permissions | |
14 | if not self.backend.wiki.check_acl(path, self.current_user): | |
15 | raise tornado.web.HTTPError(403, "Access to %s not allowed for %s" % (path, self.current_user)) | |
16 | ||
17 | # Fetch the wiki page | |
18 | page = self.backend.wiki.get_page(path) | |
19 | ||
20 | # Empty page if it was deleted | |
21 | if page and page.was_deleted(): | |
22 | page = None | |
23 | ||
24 | # Render page | |
25 | self.render("wiki/edit.html", page=page) | |
26 | ||
27 | @tornado.web.authenticated | |
28 | def post(self, path): | |
29 | # Check permissions | |
30 | if not self.backend.wiki.check_acl(path, self.current_user): | |
31 | raise tornado.web.HTTPError(403, "Access to %s not allowed for %s" % (path, self.current_user)) | |
32 | ||
33 | content = self.get_argument("content", None) | |
34 | changes = self.get_argument("changes") | |
35 | ||
36 | # Create a new page in the database | |
37 | with self.db.transaction(): | |
38 | page = self.backend.wiki.create_page(path, | |
39 | self.current_user, content, changes=changes, address=self.get_remote_ip()) | |
40 | ||
41 | # Add user as a watcher if wanted | |
42 | watch = self.get_argument("watch", False) | |
43 | if watch: | |
44 | page.add_watcher(self.current_user) | |
45 | ||
46 | # Redirect back | |
47 | if page.was_deleted(): | |
48 | self.redirect("/") | |
49 | else: | |
50 | self.redirect(page.url) | |
51 | ||
52 | def on_finish(self): | |
53 | """ | |
54 | Updates the search index after the page has been edited | |
55 | """ | |
56 | # This is being executed in the background and after | |
57 | # the response has been set to the client | |
58 | with self.db.transaction(): | |
59 | self.backend.wiki.refresh() | |
60 | ||
61 | ||
62 | class ActionUploadHandler(auth.CacheMixin, base.BaseHandler): | |
63 | @tornado.web.authenticated | |
64 | def post(self): | |
65 | path = self.get_argument("path") | |
66 | ||
67 | # Check permissions | |
68 | if not self.backend.wiki.check_acl(path, self.current_user): | |
69 | raise tornado.web.HTTPError(403, "Access to %s not allowed for %s" % (path, self.current_user)) | |
70 | ||
71 | try: | |
72 | filename, data, mimetype = self.get_file("file") | |
73 | ||
74 | # XXX check valid mimetypes | |
75 | ||
76 | with self.db.transaction(): | |
77 | file = self.backend.wiki.upload(path, filename, data, | |
78 | mimetype=mimetype, author=self.current_user, | |
79 | address=self.get_remote_ip()) | |
80 | ||
81 | except TypeError as e: | |
82 | raise e | |
83 | ||
84 | self.redirect("%s/_files" % path) | |
85 | ||
86 | ||
87 | class ActionWatchHandler(auth.CacheMixin, base.BaseHandler): | |
88 | @tornado.web.authenticated | |
89 | def get(self, path, action): | |
90 | page = self.backend.wiki.get_page(path) | |
91 | if not page: | |
92 | raise tornado.web.HTTPError(404, "Page does not exist: %s" % path) | |
93 | ||
94 | # Check permissions | |
95 | if not self.backend.wiki.check_acl(path, self.current_user): | |
96 | raise tornado.web.HTTPError(403, "Access to %s not allowed for %s" % (path, self.current_user)) | |
97 | ||
98 | with self.db.transaction(): | |
99 | if action == "watch": | |
100 | page.add_watcher(self.current_user) | |
101 | elif action == "unwatch": | |
102 | page.remove_watcher(self.current_user) | |
103 | ||
104 | # Redirect back to page | |
105 | self.redirect(page.url) | |
106 | ||
107 | ||
108 | class FilesHandler(auth.CacheMixin, base.BaseHandler): | |
109 | @tornado.web.authenticated | |
110 | def get(self, path): | |
111 | # Check permissions | |
112 | if not self.backend.wiki.check_acl(path, self.current_user): | |
113 | raise tornado.web.HTTPError(403, "Access to %s not allowed for %s" % (path, self.current_user)) | |
114 | ||
115 | files = self.backend.wiki.get_files(path) | |
116 | ||
117 | self.render("wiki/files/index.html", path=path, files=files) | |
118 | ||
119 | ||
120 | class FileHandler(base.BaseHandler): | |
121 | @property | |
122 | def action(self): | |
123 | return self.get_argument("action", None) | |
124 | ||
125 | def get(self, path): | |
126 | # Check permissions | |
127 | if not self.backend.wiki.check_acl(path, self.current_user): | |
128 | raise tornado.web.HTTPError(403, "Access to %s not allowed for %s" % (path, self.current_user)) | |
129 | ||
130 | # Fetch the file | |
131 | file = self.backend.wiki.get_file_by_path(path) | |
132 | if not file: | |
133 | raise tornado.web.HTTPError(404, "Could not find %s" % path) | |
134 | ||
135 | # Render detail page | |
136 | if self.action == "detail": | |
137 | page = None | |
138 | ||
139 | for breadcrumb, title in self.backend.wiki.make_breadcrumbs(path): | |
140 | page = self.backend.wiki.get_page(breadcrumb) | |
141 | if page: | |
142 | break | |
143 | ||
144 | self.render("wiki/files/detail.html", page=page, file=file) | |
145 | return | |
146 | ||
147 | size = self.get_argument_int("s", None) | |
148 | ||
149 | # Check if image should be resized | |
150 | if file.is_image() and size: | |
151 | blob = file.get_thumbnail(size) | |
152 | else: | |
153 | blob = file.blob | |
154 | ||
155 | # Set headers | |
156 | self.set_header("Content-Type", file.mimetype or "application/octet-stream") | |
157 | self.set_header("Content-Length", len(blob)) | |
158 | ||
159 | # Set expires | |
160 | self.set_expires(3600) | |
161 | ||
162 | # Deliver content | |
163 | self.finish(blob) | |
164 | ||
165 | ||
166 | class PageHandler(auth.CacheMixin, base.BaseHandler): | |
167 | @property | |
168 | def action(self): | |
169 | return self.get_argument("action", None) | |
170 | ||
171 | def write_error(self, status_code, **kwargs): | |
172 | # Render a custom page for 404 | |
173 | if status_code == 404: | |
174 | self.render("wiki/404.html", **kwargs) | |
175 | return | |
176 | ||
177 | # Otherwise raise this to one layer above | |
178 | super().write_error(status_code, **kwargs) | |
179 | ||
180 | @tornado.web.removeslash | |
181 | def get(self, path): | |
182 | # Check permissions | |
183 | if not self.backend.wiki.check_acl(path, self.current_user): | |
184 | raise tornado.web.HTTPError(403, "Access to %s not allowed for %s" % (path, self.current_user)) | |
185 | ||
186 | # Check if we are asked to render a certain revision | |
187 | revision = self.get_argument("revision", None) | |
188 | ||
189 | # Fetch the wiki page | |
190 | page = self.backend.wiki.get_page(path, revision=revision) | |
191 | ||
192 | # Diff | |
193 | if self.action == "diff": | |
194 | # Get both revisions | |
195 | a = self.get_argument("a") | |
196 | b = self.get_argument("b") | |
197 | ||
198 | # Fetch both versions of the page | |
199 | a = self.backend.wiki.get_page(path, revision=a) | |
200 | b = self.backend.wiki.get_page(path, revision=b) | |
201 | if not a or not b: | |
202 | raise tornado.web.HTTPError(404) | |
203 | ||
204 | # Cannot render a diff for the identical page | |
205 | if a == b: | |
206 | raise tornado.web.HTTPError(400) | |
207 | ||
208 | # Make sure that b is newer than a | |
209 | if a > b: | |
210 | a, b = b, a | |
211 | ||
212 | self.render("wiki/diff.html", page=page, a=a, b=b) | |
213 | return | |
214 | ||
215 | # Revisions | |
216 | elif self.action == "revisions": | |
217 | self.render("wiki/revisions.html", page=page) | |
218 | return | |
219 | ||
220 | # If the page does not exist, we send 404 | |
221 | if not page or page.was_deleted(): | |
222 | raise tornado.web.HTTPError(404) | |
223 | ||
224 | # Fetch the latest revision | |
225 | latest_revision = page.get_latest_revision() | |
226 | ||
227 | # Render page | |
228 | self.render("wiki/page.html", page=page, latest_revision=latest_revision) | |
229 | ||
230 | ||
231 | class SearchHandler(auth.CacheMixin, base.BaseHandler): | |
232 | @base.blacklisted | |
233 | def get(self): | |
234 | q = self.get_argument("q") | |
235 | ||
236 | pages = self.backend.wiki.search(q, account=self.current_user, limit=50) | |
237 | ||
238 | self.render("wiki/search-results.html", q=q, pages=pages) | |
239 | ||
240 | ||
241 | class RecentChangesHandler(auth.CacheMixin, base.BaseHandler): | |
242 | def get(self): | |
243 | recent_changes = self.backend.wiki.get_recent_changes(self.current_user, limit=50) | |
244 | ||
245 | self.render("wiki/recent-changes.html", recent_changes=recent_changes) | |
246 | ||
247 | ||
248 | class WatchlistHandler(auth.CacheMixin, base.BaseHandler): | |
249 | @tornado.web.authenticated | |
250 | def get(self): | |
251 | pages = self.backend.wiki.get_watchlist(self.current_user) | |
252 | ||
253 | self.render("wiki/watchlist.html", pages=pages) | |
254 | ||
255 | ||
256 | class WikiDiffModule(ui_modules.UIModule): | |
257 | differ = difflib.Differ() | |
258 | ||
259 | def render(self, a, b): | |
260 | diff = self.differ.compare( | |
261 | a.markdown.splitlines(), | |
262 | b.markdown.splitlines(), | |
263 | ) | |
264 | ||
265 | return self.render_string("wiki/modules/diff.html", diff=diff) | |
266 | ||
267 | ||
268 | class WikiListModule(ui_modules.UIModule): | |
269 | def render(self, pages, link_revision=False, show_breadcrumbs=True, | |
270 | show_author=True, show_changes=False): | |
271 | return self.render_string("wiki/modules/list.html", link_revision=link_revision, | |
272 | pages=pages, show_breadcrumbs=show_breadcrumbs, | |
273 | show_author=show_author, show_changes=show_changes) | |
274 | ||
275 | ||
276 | class WikiNavbarModule(ui_modules.UIModule): | |
277 | @property | |
278 | def path(self): | |
279 | """ | |
280 | Returns the path of the page (without any actions) | |
281 | """ | |
282 | path = self.request.path.split("/") | |
283 | ||
284 | if path and path[-1].startswith("_"): | |
285 | path.pop() | |
286 | ||
287 | return "/".join(path) | |
288 | ||
289 | def render(self, suffix=None): | |
290 | _ = self.locale.translate | |
291 | ||
292 | # Make the path | |
293 | page = self.request.path.split("/") | |
294 | ||
295 | # Drop the action bit | |
296 | if page and page[-1].startswith("_"): | |
297 | page.pop() | |
298 | ||
299 | page = "/".join(page) | |
300 | ||
301 | breadcrumbs = self.backend.wiki.make_breadcrumbs(page) | |
302 | title = self.backend.wiki.get_page_title(page) | |
303 | ||
304 | if self.request.path.endswith("/_edit"): | |
305 | suffix = _("Edit") | |
306 | elif self.request.path.endswith("/_files"): | |
307 | suffix = _("Files") | |
308 | ||
309 | return self.render_string("wiki/modules/navbar.html", | |
310 | breadcrumbs=breadcrumbs, page=page, page_title=title, suffix=suffix) |