1 """
2 HTML forms
3 (part of web.py)
4 """
5
6 import copy, re
7 import webapi as web
8 import utils, net
9
11 if hasattr(obj, 'has_key') and obj.has_key(attr): return obj[attr]
12 if hasattr(obj, attr): return getattr(obj, attr)
13 return value
14
60
64
78
86
89
94
101
107
109 return utils.storage([(i.name, i.get_value()) for i in self.inputs])
110 d = property(_get_d)
111
169
171 """List of atributes of input.
172
173 >>> a = AttributeList(type='text', name='x', value=20)
174 >>> a
175 <attrs: 'type="text" name="x" value="20"'>
176 """
179
181 return " ".join(['%s="%s"' % (k, net.websafe(v)) for k, v in self.items()])
182
184 return '<attrs: %s>' % repr(str(self))
185
186 -class Textbox(Input):
187 """Textbox input.
188
189 >>> Textbox(name='foo', value='bar').render()
190 '<input type="text" id="foo" value="bar" name="foo"/>'
191 >>> Textbox(name='foo', value=0).render()
192 '<input type="text" id="foo" value="0" name="foo"/>'
193 """
194 - def get_type(self):
196
198 """Password input.
199
200 >>> Password(name='password', value='secret').render()
201 '<input type="password" id="password" value="secret" name="password"/>'
202 """
203
206
207 -class Textarea(Input):
208 """Textarea input.
209
210 >>> Textarea(name='foo', value='bar').render()
211 '<textarea id="foo" name="foo">bar</textarea>'
212 """
214 attrs = self.attrs.copy()
215 attrs['name'] = self.name
216 value = net.websafe(self.value or '')
217 return '<textarea %s>%s</textarea>' % (attrs, value)
218
220 r"""Dropdown/select input.
221
222 >>> Dropdown(name='foo', args=['a', 'b', 'c'], value='b').render()
223 '<select id="foo" name="foo">\n <option value="a">a</option>\n <option selected="selected" value="b">b</option>\n <option value="c">c</option>\n</select>\n'
224 >>> Dropdown(name='foo', args=[('a', 'aa'), ('b', 'bb'), ('c', 'cc')], value='b').render()
225 '<select id="foo" name="foo">\n <option value="a">aa</option>\n <option selected="selected" value="b">bb</option>\n <option value="c">cc</option>\n</select>\n'
226 """
227 - def __init__(self, name, args, *validators, **attrs):
230
232 attrs = self.attrs.copy()
233 attrs['name'] = self.name
234
235 x = '<select %s>\n' % attrs
236
237 for arg in self.args:
238 if isinstance(arg, (tuple, list)):
239 value, desc= arg
240 else:
241 value, desc = arg, arg
242
243 if self.value == value: select_p = ' selected="selected"'
244 else: select_p = ''
245 x += ' <option%s value="%s">%s</option>\n' % (select_p, net.websafe(value), net.websafe(desc))
246
247 x += '</select>\n'
248 return x
249
251 - def __init__(self, name, args, *validators, **attrs):
254
256 x = '<span>'
257 for arg in self.args:
258 if isinstance(arg, (tuple, list)):
259 value, desc= arg
260 else:
261 value, desc = arg, arg
262 attrs = self.attrs.copy()
263 attrs['name'] = self.name
264 attrs['type'] = 'radio'
265 attrs['value'] = arg
266 if self.value == arg:
267 attrs['checked'] = 'checked'
268 x += '<input %s/> %s' % (attrs, net.websafe(desc))
269 x += '</span>'
270 return x
271
273 """Checkbox input.
274
275 >>> Checkbox('foo', value='bar', checked=True).render()
276 '<input checked="checked" type="checkbox" id="foo_bar" value="bar" name="foo"/>'
277 >>> Checkbox('foo', value='bar').render()
278 '<input type="checkbox" id="foo_bar" value="bar" name="foo"/>'
279 >>> c = Checkbox('foo', value='bar')
280 >>> c.validate('on')
281 True
282 >>> c.render()
283 '<input checked="checked" type="checkbox" id="foo_bar" value="bar" name="foo"/>'
284 """
285 - def __init__(self, name, *validators, **attrs):
286 self.checked = attrs.pop('checked', False)
287 Input.__init__(self, name, *validators, **attrs)
288
290 value = utils.safestr(self.value or "")
291 return self.name + '_' + value.replace(' ', '_')
292
294 attrs = self.attrs.copy()
295 attrs['type'] = 'checkbox'
296 attrs['name'] = self.name
297 attrs['value'] = self.value
298
299 if self.checked:
300 attrs['checked'] = 'checked'
301 return '<input %s/>' % attrs
302
304 self.checked = bool(value)
305
308
328
330 """Hidden Input.
331
332 >>> Hidden(name='foo', value='bar').render()
333 '<input type="hidden" id="foo" value="bar" name="foo"/>'
334 """
337
340
342 """File input.
343
344 >>> File(name='f').render()
345 '<input type="file" id="f" name="f"/>'
346 """
349
354 try: return self.test(value)
355 except: return False
356
357 notnull = Validator("Required", bool)
358
361 self.rexp = re.compile(rexp)
362 self.msg = msg
363
365 return bool(self.rexp.match(value))
366
367 if __name__ == "__main__":
368 import doctest
369 doctest.testmod()
370