comparison MoinMoin/wikiform.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
comparison
equal deleted inserted replaced
-1:000000000000 0:77665d8e2254
1 # -*- coding: iso-8859-1 -*-
2 """
3 MoinMoin - User Forms
4
5 @copyright: 2001-2004 by Jürgen Hermann <jh@web.de>
6 @license: GNU GPL, see COPYING for details.
7 """
8
9 from MoinMoin import wikiutil
10 from MoinMoin.Page import Page
11
12 #############################################################################
13 ### Form definitions
14 #############################################################################
15
16 _required_attributes = ['type', 'name', 'label']
17
18 def parseDefinition(request, fielddef, fieldlist):
19 """ Parse a form field definition and return the HTML markup for it
20 """
21 _ = request.getText
22
23 row = '<tr><td nowrap="nowrap" valign="top">&nbsp;%s&nbsp;</td><td>%s</td></tr>\n'
24 fields, msg = wikiutil.parseAttributes(request, fielddef)
25
26 if not msg:
27 for required in _required_attributes:
28 if not fields.has_key(required):
29 msg = _('Required attribute "%(attrname)s" missing') % {
30 'attrname': required}
31 break
32
33 if msg:
34 # create visible error
35 result = row % (msg, fielddef)
36 elif fields['type'] == '"caption"':
37 # create a centered, bold italic caption
38 result = '<tr><td colspan="2" align="center"><em><strong>%s</strong></em></td></tr>\n' % (
39 fields['label'][1:-1])
40 else:
41 # for submit buttons, use `label` as the value
42 if fields['type'] == '"submit"':
43 fields['value'] = fields['label']
44 fields['label'] = ''
45
46 # make sure user cannot use a system name
47 fields['name'] = '"form_' + fields['name'][1:]
48 fieldlist.append(fields['name'][1:-1])
49
50 wrapper = ('<input', '>\n')
51 if fields['type'] == '"textarea"':
52 wrapper = ('<textarea', '></textarea>\n')
53
54 result = wrapper[0]
55 for key, val in fields.items():
56 if key == 'label': continue
57 result = '%s %s=%s' % (result, key, val)
58 result = result + wrapper[1]
59
60 #result = result + wikiutil.escape(`fields`)
61
62 if fields['type'] == '"submit"':
63 result = '<tr><td colspan="2" align="center">%s</td></tr>\n' % result
64 else:
65 result = row % (fields['label'][1:-1], result)
66
67 return result
68
69
70 def _get_formvalues(form):
71 result = {}
72 for key in form.keys():
73 if key[:5] != 'form_': continue
74
75 val = form.get(key, ["<empty>"])
76 if type(val) is type([]):
77 # Multiple username fields specified
78 val = "|".join(val).replace('\r','')
79
80 result[key] = val
81
82 return result
83
84
85 def do_formtest(pagename, request):
86 """ Test a user defined form.
87 """
88 _ = request.getText
89
90 result = []
91 for key, val in _get_formvalues(request.form).items():
92 result.append('<li><em>%s</em> = %s</li>' % (key.upper(), repr(wikiutil.escape(val))))
93 msg = '%s<ul>\n%s</ul>\n' % (_('Submitted form data:'), '\n'.join(result))
94
95 Page(request, pagename).send_page(request, msg=msg)
96