comparison MoinMoin/action/newpage.py @ 0:77665d8e2254

tag of nonpublic@localhost--archive/moin--enterprise--1.5--base-0 (automatically generated log message) imported from: moin--main--1.5--base-0
author Thomas Waldmann <tw-public@gmx.de>
date Thu, 22 Sep 2005 15:09:50 +0000
parents
children e0cbf33ff15c
comparison
equal deleted inserted replaced
-1:000000000000 0:77665d8e2254
1 """
2 MoinMoin - newpage action
3
4 Create a new page with optional template. Can be used with NewPage.py macro.
5
6 @copyright: 2004 Vito Miliano (vito_moinnewpagewithtemplate@perilith.com)
7 @copyright: 2004 by Nir Soffer <nirs@freeshell.org>
8 @copyright: 2004 Alexander Schremmer <alex AT alexanderweb DOT de>
9 @license: GNU GPL, see COPYING for details.
10 """
11
12 from MoinMoin.util import MoinMoinNoFooter
13 from MoinMoin.Page import Page
14 import time
15
16 class NewPage:
17 """ Open editor for a new page, using template """
18
19 def __init__(self, request, referrer):
20 self.request = request
21 self.referrer = referrer # The page the user came from
22 self.pagename = self.request.form.get('pagename', [None])[0]
23 self.nametemplate = self.request.form.get('nametemplate', ['%s'])[0]
24 self.nametemplate = self.nametemplate.replace('\x00','')
25
26 def checkAndCombineArguments(self):
27 """ Check arguments in form, return error msg
28
29 @rtype: unicode
30 @return: error message
31 """
32 _ = self.request.getText
33 need_replace = self.nametemplate.find('%s') != -1
34 if not self.pagename and need_replace:
35 return _("Cannot create a new page without a page name."
36 " Please specify a page name.")
37 if need_replace:
38 # generate a string that can be safely used as the pagename
39 # template variable
40 repl = 'A@'
41 i = 0
42 while self.nametemplate.find(repl) != -1:
43 repl += ['#','&','$','x','X',':','@'][i]
44 i += 1
45 i = i % 7
46 template = self.nametemplate.replace('%s', repl)
47 else:
48 template = self.nametemplate
49 template = template.encode('utf-8')
50 template = time.strftime(template, self.request.user.getTime(time.time()))
51 template = template.decode('utf-8')
52 if need_replace:
53 self.pagename = template.replace(repl, self.pagename)
54 else:
55 self.pagename = template
56 return ''
57
58 def checkPermissions(self):
59 """ Check write permission in form, return error msg
60
61 @rtype: unicode
62 @return: error message
63 """
64 _ = self.request.getText
65 page = Page(self.request, self.pagename)
66 if not (page.isWritable() and self.request.user.may.read(self.pagename)):
67 # Same error as the edit page for localization reasons
68 return _('You are not allowed to edit this page.')
69 return ''
70
71 def render(self):
72 """ Redirect to the new page, using edit action and template """
73
74 error = self.checkAndCombineArguments() or self.checkPermissions()
75 if error:
76 # Send back to the page you came from, with an error msg
77 page = Page(self.request, self.referrer)
78 page.send_page(self.request, msg=error)
79 else:
80 # Redirect to new page using edit action. No error checking
81 # is needed because it is done later in new request.
82 pagename = self.pagename
83 query = {'action': 'edit', 'backto': self.referrer}
84
85 template = self.request.form.get('template', [''])[0]
86 if template:
87 from MoinMoin.wikiutil import quoteWikinameURL
88 query['template'] = quoteWikinameURL(template)
89
90 parent = self.request.form.get('parent', [''])[0]
91 if parent:
92 pagename = "%s/%s" % (parent, pagename)
93
94 url = Page(self.request, pagename).url(self.request, query, 0)
95 self.request.http_redirect(url)
96 raise MoinMoinNoFooter
97
98 return ''
99
100 def execute(pagename, request):
101 """ Temporary glue code for current moin action system """
102 return NewPage(request, pagename).render()
103