annotate wikked/webimpl/edit.py @ 389:08b831d894c8

web: Actually pass the correct author to the SCM backend.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 13 Oct 2015 23:56:21 -0700
parents 0979ace46676
children e28f2c76691c
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
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6 from wikked.utils import PageNotFoundError, split_page_url
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,
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9 get_page_or_raise, get_page_meta)
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 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
44 endpoint, path = split_page_url(url)
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
45 last_slash = path.rstrip('/').rfind('/')
342
0979ace46676 Fix "edit page" title.
Ludovic Chabant <ludovic@chabant.com>
parents: 341
diff changeset
46 if last_slash < 0 or last_slash == 0:
0979ace46676 Fix "edit page" title.
Ludovic Chabant <ludovic@chabant.com>
parents: 341
diff changeset
47 title = path.lstrip('/')
341
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
48 else:
342
0979ace46676 Fix "edit page" title.
Ludovic Chabant <ludovic@chabant.com>
parents: 341
diff changeset
49 title = path[last_slash + 1:]
341
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
50 if endpoint:
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
51 return '%s: %s' % (endpoint, title)
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
52 return title
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
53
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
54
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
55 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
56 page = None
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
57 try:
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
58 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
59 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
60 except PageNotFoundError:
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
61 # 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
62 # 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
63 page = None
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
64
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
65 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
66 result = {
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
67 'meta': {
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
68 '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
69 '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
70 },
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
71 'text': ''
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
72 }
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
73 else:
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
74 result = {
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
75 '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
76 '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
77 }
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
78
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
79 result['commit_meta'] = {
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
80 'author': author,
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
81 '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
82 }
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
83
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
84 if custom_data:
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
85 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
86
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
87 return result
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
88
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
89
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
90 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
91 try:
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
92 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
93 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
94 except PageNotFoundError:
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
95 # 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
96 # 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
97 pass
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 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
100 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
101 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
102
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
103 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
104 page_fields = {
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
105 'text': text,
389
08b831d894c8 web: Actually pass the correct author to the SCM backend.
Ludovic Chabant <ludovic@chabant.com>
parents: 342
diff changeset
106 'author': author,
341
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
107 'message': message
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
108 }
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
109 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
110
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
111
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
112 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
113 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
114 resolver = PageResolver(dummy)
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
115 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
116 return dummy.text
37f426e067c4 Big refactor to get rid of this whole single page app crap.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
117