utils.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. class UnsetValue(object):
  2. """
  3. An unset value.
  4. This is used in situations where a blank value like `None` is acceptable
  5. usually as the default value of a class variable or function parameter
  6. (iow, usually when `None` is a valid value.)
  7. """
  8. def __str__(self):
  9. return '<unset value>'
  10. def __repr__(self):
  11. return '<unset value>'
  12. def __bool__(self):
  13. return False
  14. def __nonzero__(self):
  15. return False
  16. unset_value = UnsetValue()
  17. class WebobInputWrapper(object):
  18. """
  19. Wrap a webob MultiDict for use as passing as `formdata` to Field.
  20. Since for consistency, we have decided in WTForms to support as input a
  21. small subset of the API provided in common between cgi.FieldStorage,
  22. Django's QueryDict, and Werkzeug's MultiDict, we need to wrap Webob, the
  23. only supported framework whose multidict does not fit this API, but is
  24. nevertheless used by a lot of frameworks.
  25. While we could write a full wrapper to support all the methods, this will
  26. undoubtedly result in bugs due to some subtle differences between the
  27. various wrappers. So we will keep it simple.
  28. """
  29. def __init__(self, multidict):
  30. self._wrapped = multidict
  31. def __iter__(self):
  32. return iter(self._wrapped)
  33. def __len__(self):
  34. return len(self._wrapped)
  35. def __contains__(self, name):
  36. return (name in self._wrapped)
  37. def getlist(self, name):
  38. return self._wrapped.getall(name)