Attachment 'CreateNewPage.py'
Download 1 # -*- coding: utf-8 -*-
2 """
3 MoinMoin - CreateNewPage Action
4
5 XXX ToDo
6 Provide translations for:
7 _('Page Template')
8 _('Page Name')
9 _('Create')
10
11 CreateNewPage partly based on NewPage action
12 NewPage Action
13 @copyright: 2004 Vito Miliano (vito_moinnewpagewithtemplate@perilith.com)
14 @copyright: 2004 by Nir Soffer <nirs@freeshell.org>
15 @copyright: 2004 Alexander Schremmer <alex AT alexanderweb DOT de>
16
17 CreateNewPage Action
18 @copyright: 2007 by Oliver Siemoneit
19 @license: GNU GPL, see COPYING for details.
20 """
21
22 from MoinMoin import wikiutil
23 from MoinMoin.Page import Page
24
25 def execute(pagename, request):
26 return NewPageHandler(pagename, request).handle()
27
28 class NewPageHandler:
29
30 def __init__(self, pagename, request):
31 self.request = request
32 self._ = request.getText
33 self.pagename = pagename
34 self.newpage = ''
35 self.template = ''
36
37 def showForm(self, msg=None):
38 """ Assemble form and output it
39
40 @rtype: -
41 @return: -
42 """
43 _ = self._
44 request = self.request
45
46 # Get available templates
47 templates = []
48 filter = request.cfg.cache.page_template_regex.search
49 results = request.rootpage.getPageList(filter=filter)
50 results.sort()
51 templates.append("<option selected=\"selected\">%s</option>" % '') # _('<None>')
52 for result in results:
53 templates.append("<option>%s</option>" % result)
54
55 # Assemble form
56 sn = request.script_root
57 pi = request.path
58 action = u"%s%s" % (sn, pi)
59 lang_attr = request.theme.ui_lang_attr()
60
61 form = '''
62 <form action="%(action)s" method="POST">
63 <div class="userpref" lang="%(lang_attr)s">
64 <input type="hidden" name="action" value="CreateNewPage" />
65 <table style="border:0;">
66 <tr>
67 <td><span class="bold">%(template)s</span></td>
68 <td>
69 <select name="template" size="1">
70 %(templates)s
71 </select>
72 </td>
73 </tr>
74 <tr>
75 <td><span class="bold">%(input)s</span></td>
76 <td><input type="text" name="newpage" value="" size="36" /></td>
77 </tr>
78 <tr>
79 <td> </td>
80 <td><input type="submit" name="create" value="%(button)s" /></td>
81 </tr>
82 </table>
83 </div>
84 </form>''' % { 'action': action,
85 'lang_attr': lang_attr,
86 'template': _('Page Template'),
87 'templates': ''.join(templates),
88 'input': _('Page Name'),
89 'button': _('Create') }
90
91 # Output form
92 # fixed for 1.9
93 try:
94 request.emit_http_headers()
95 except AttributeError:
96 try:
97 request.http_headers()
98 except AttributeError:
99 pass
100 request.theme.send_title(_('Create New Page'), pagename=self.pagename, msg=msg)
101 request.write(request.formatter.startContent("content"))
102 request.write(request.formatter.rawHTML(unicode(form)))
103 request.write(request.formatter.endContent())
104 request.theme.send_footer(self.pagename)
105 request.theme.send_closing_html()
106
107 def checkInput(self):
108 """ Check if input is valid
109
110 @rtype: unicode
111 @return: error message
112 """
113 _ = self._
114 self.newpage = wikiutil.escape(self.newpage.strip())
115 self.template = wikiutil.escape(self.template.strip())
116 if not self.newpage:
117 return _("Cannot create a new page without a page name."
118 " Please specify a page name.")
119 page = Page(self.request, self.newpage)
120 if not (page.isWritable() and self.request.user.may.read(self.newpage)):
121 return _('You are not allowed to edit this page.')
122 return ''
123
124 def handle(self):
125 request = self.request
126 form = self.request.form
127 # If user has pressed "create" button, process it. Otherwise show form only.
128 if form.has_key('create'):
129 self.newpage = form.get('newpage', [''])[0]
130 self.template = form.get('template', [''])[0]
131 error = self.checkInput()
132 if error:
133 self.showForm(error)
134 else:
135 query = {'action': 'edit', 'backto': self.pagename}
136 if self.template:
137 query['template'] = wikiutil.quoteWikinameURL(self.template)
138 url = Page(request, self.newpage).url(request, query, escape=0, relative=False)
139 self.request.http_redirect(url)
140 else:
141 self.showForm()
142 return ''
143
144
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.