annotate wikked/webimpl/edit.py @ 425:e28f2c76691c

web: Add "broken links" and "wanted pages" lists. * Broken links relies on usual page list stuff. * Wanted pages relies on new DB tables and information.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 28 Mar 2017 21:24:44 -0700
parents 08b831d894c8
children 3985b3d72353
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
341
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import logging
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 import urllib.parse
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 from wikked.page import Page, PageData
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4 from wikked.formatter import PageFormatter, FormattingContext
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5 from wikked.resolver import PageResolver
425
e28f2c76691c web: Add "broken links" and "wanted pages" lists.
Ludovic Chabant <ludovic@chabant.com>
parents: 389
diff changeset
6 from wikked.utils import PageNotFoundError
341
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7 from wikked.webimpl import (
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8 CHECK_FOR_WRITE,
425
e28f2c76691c web: Add "broken links" and "wanted pages" lists.
Ludovic Chabant <ludovic@chabant.com>
parents: 389
diff changeset
9 get_page_or_raise, get_page_meta, make_page_title)
341
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12 logger = logging.getLogger(__name__)
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15 class DummyPage(Page):
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16 """ A dummy page for previewing in-progress editing.
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17 """
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18 def __init__(self, wiki, url, text):
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
19 data = self._loadData(wiki, url, text)
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20 super(DummyPage, self).__init__(wiki, data)
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22 def _loadData(self, wiki, url, text):
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23 data = PageData()
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24 extension = wiki.fs.default_extension
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25 data.url = url
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26 data.path = '__preview__.' + extension
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27 data.raw_text = text
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
29 ctx = FormattingContext(url)
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 f = PageFormatter()
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31 data.formatted_text = f.formatText(ctx, data.raw_text)
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32 data.local_meta = ctx.meta
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33 data.local_links = ctx.out_links
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
35 data.title = (data.local_meta.get('title') or
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
36 make_page_title(url))
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37 if isinstance(data.title, list):
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38 data.title = data.title[0]
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
40 return data
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
41
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
42
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
43 def get_edit_page(wiki, user, url, author=None, custom_data=None):
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
44 page = None
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
45 try:
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
46 page = get_page_or_raise(wiki, url,
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
47 check_perms=(user, CHECK_FOR_WRITE))
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
48 except PageNotFoundError:
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
49 # Only catch errors about the page not existing. Permission
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
50 # errors still go through.
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
51 page = None
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
52
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
53 if page is None:
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
54 result = {
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
55 'meta': {
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
56 'url': urllib.parse.quote(url.encode('utf-8')),
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
57 'title': make_page_title(url)
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
58 },
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
59 'text': ''
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
60 }
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
61 else:
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
62 result = {
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
63 'meta': get_page_meta(page, True),
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
64 'text': page.raw_text
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
65 }
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
66
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
67 result['commit_meta'] = {
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
68 'author': author,
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
69 'desc': 'Editing ' + result['meta']['title']
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
70 }
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
71
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
72 if custom_data:
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
73 result.update(custom_data)
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
74
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
75 return result
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
76
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
77
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
78 def do_edit_page(wiki, user, url, text, author=None, message=None):
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
79 try:
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
80 get_page_or_raise(wiki, url,
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
81 check_perms=(user, CHECK_FOR_WRITE))
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
82 except PageNotFoundError:
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
83 # Only catch errors about the page not existing. Permission
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
84 # errors still go through.
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
85 pass
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
86
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
87 author = author or user
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
88 if author is None:
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
89 raise Exception("No author or user was specified.")
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
90
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
91 message = message or 'Edited ' + url
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
92 page_fields = {
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
93 'text': text,
389
08b831d894c8 web: Actually pass the correct author to the SCM backend.
Ludovic Chabant <ludovic@chabant.com>
parents: 342
diff changeset
94 'author': author,
341
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
95 'message': message
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
96 }
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
97 wiki.setPage(url, page_fields)
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
98
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
99
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
100 def preview_edited_page(wiki, url, raw_text):
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
101 dummy = DummyPage(wiki, url, raw_text)
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
102 resolver = PageResolver(dummy)
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
103 dummy._setExtendedData(resolver.run())
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
104 return dummy.text