Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

root/branches/cherrypy-2.x/cherrypy/lib/defaultformmask.py

Revision 1028 (checked in by rdelon, 2 years ago)

Implemented #478

  • Property svn:eol-style set to native
Line 
1 """Default mask for the form.py module"""
2
3 import warnings
4 warnings.warn("cherrypy.lib.defaultformmask is deprecated and might disappear in future versions of CherryPy", DeprecationWarning, stacklevel = 2)
5
6 from xml.sax.saxutils import quoteattr as q
7
8
9 def selected(value):
10     """If value is True, return a valid XHTML 'selected' attribute, else ''."""
11     if value:
12         return ' selected="selected" '
13     return ''
14
15 def checked(value):
16     """If value is True, return a valid XHTML 'checked' attribute, else ''."""
17     if value:
18         return ' checked="checked" '
19     return ''
20
21
22 def defaultMask(field):
23    
24     res = ["<tr>",
25            "<td valign='top'>%s</td>" % field.label]
26    
27     if field.typ == 'text':
28         res.append('<td><input name=%s type="text" value=%s size=%s /></td>'
29                    % (q(field.name), q(field.currentValue), q(field.size)))
30     elif field.typ == 'forced':
31         res.append('<td><input name=%s type="hidden" value=%s />%s</td>'
32                    % (q(field.name), q(field.currentValue), field.currentValue))
33     elif field.typ == 'password':
34         res.append('<td><input name=%s type="password" value=%s /></td>'
35                    % (q(field.name), q(field.currentValue)))
36     elif field.typ == 'select':
37         res.append('<td><select name=%s>' % q(field.name))
38         for option in field.optionList:
39             if isinstance(option, tuple):
40                 id, option = option
41                 sel = selected(field.currentValue in (id, str(id)))
42                 value = " value=%s" % q(id)
43             else:
44                 sel = selected(option == field.currentValue)
45                 value = ""
46             res.append("    <option%s%s>%s</option>" % (sel, value, option))
47         res.append('</select></td>')
48     elif field.typ == 'textarea':
49         # Size is cols x rows
50         if field.size == 15:
51             size = "15x15"
52         else:
53             size = field.size
54         cols, rows = size.split('x')
55         res.append('<td><textarea name=%s rows="%s" cols="%s">%s</textarea></td>'
56                    % (q(field.name), rows, cols, field.currentValue))
57     elif field.typ == 'submit':
58         res.append('<td><input type="submit" value=%s /></td>' % q(field.name))
59     elif field.typ == 'hidden':
60         if isinstance(field.currentValue, list):
61             vals = field.currentValue
62         else:
63             vals = [field.currentValue]
64         i = '<input name=%s type="hidden" value=%%s />' % q(field.name)
65         return ''.join([i % q(v) for v in vals])
66     elif field.typ in ('checkbox', 'radio'):
67         res.append('<td>')
68         for option in field.optionList:
69             if isinstance(option, tuple):
70                 val, label = option
71             else:
72                 val, label = option, option
73            
74             if isinstance(field.currentValue, list):
75                 c = checked(val in field.currentValue)
76             else:
77                 c = checked(val == field.currentValue)
78            
79             res.append('<input type="%s" name=%s value=%s %s />&nbsp;&nbsp;%s<br />'
80                        % (field.typ, q(field.name), q(val), c, label))
81         res.append('</td>')
82    
83     if field.errorMessage:
84         res.append("<td><font color='red'>%s</font></td>" % field.errorMessage)
85     else:
86         res.append("<td>&nbsp;</td>")
87    
88     res.append("</tr>")
89     return "\n".join(res)
90
91 def hiddenMask(field):
92     if isinstance(field.currentValue, list):
93         currentValue = field.currentValue
94     else:
95         currentValue = [field.currentValue]
96     return "\n".join(['<input name=%s type="hidden" value=%s />' %
97                       (q(field.name), q(value)) for value in currentValue])
98
99 def defaultHeader(label):
100     return "<table>"
101
102 def defaultFooter(label):
103     return "</table>"
104
105 def echoMask(label):
106     return label
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets