Guest User

Untitled

a guest
Jul 21st, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import web
  4. from pprint import pformat
  5.  
  6. template = """
  7. <form name="main" method="post"> %s
  8. <input type="submit"/>
  9. </form>'
  10. """
  11. urls = ('/', 'index')
  12. app = web.application(urls, globals())
  13.  
  14.  
  15. class CheckboxList(web.form.Input):
  16. """CheckboxList input."""
  17.  
  18. def __init__(self, name, args, *validators, **attrs):
  19. self.args = args
  20. super(CheckboxList, self).__init__(name, *validators, **attrs)
  21.  
  22. def render(self):
  23. attrs = self.attrs.copy()
  24. x = '<fieldset %s>\n' % attrs
  25. for arg in self.args:
  26. chk_attrs = web.form.AttributeList()
  27. if isinstance(arg, (tuple, list)):
  28. value, desc= arg
  29. else:
  30. value, desc = arg, arg
  31. chk_attrs["name"] = self.name
  32. chk_attrs["type"] = "checkbox"
  33. chk_attrs["value"] = value
  34. if isinstance(self.value, (tuple, list)) and value in self.value:
  35. chk_attrs["checked"] = "checked"
  36.  
  37. x += ' <label><input %s/> %s</label>\n' % (chk_attrs, web.net.websafe(desc))
  38. x += '</fieldset>\n'
  39. return x
  40.  
  41. myform = web.form.Form(CheckboxList(name='chk', args=[('a', 'aa'), ('b', 'bb'), ('c', 'cc')]))
  42.  
  43. class index:
  44. def GET(self):
  45. form = myform(dict(chk=['a','c']))
  46. return template % form.render()
  47.  
  48. def POST(self):
  49. form = myform(web.input(chk=[]))
  50. return pformat(form.d)
  51.  
  52. if __name__=="__main__":
  53. web.internalerror = web.debugerror
  54. app.run()
Add Comment
Please, Sign In to add comment