simple.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. from .. import widgets
  2. from .core import Field
  3. __all__ = (
  4. "BooleanField",
  5. "TextAreaField",
  6. "PasswordField",
  7. "FileField",
  8. "MultipleFileField",
  9. "HiddenField",
  10. "SearchField",
  11. "SubmitField",
  12. "StringField",
  13. "TelField",
  14. "URLField",
  15. "EmailField",
  16. "ColorField",
  17. )
  18. class BooleanField(Field):
  19. """
  20. Represents an ``<input type="checkbox">``. Set the ``checked``-status by using the
  21. ``default``-option. Any value for ``default``, e.g. ``default="checked"`` puts
  22. ``checked`` into the html-element and sets the ``data`` to ``True``
  23. :param false_values:
  24. If provided, a sequence of strings each of which is an exact match
  25. string of what is considered a "false" value. Defaults to the tuple
  26. ``(False, "false", "")``
  27. """
  28. widget = widgets.CheckboxInput()
  29. false_values = (False, "false", "")
  30. def __init__(self, label=None, validators=None, false_values=None, **kwargs):
  31. super().__init__(label, validators, **kwargs)
  32. if false_values is not None:
  33. self.false_values = false_values
  34. def process_data(self, value):
  35. self.data = bool(value)
  36. def process_formdata(self, valuelist):
  37. if not valuelist or valuelist[0] in self.false_values:
  38. self.data = False
  39. else:
  40. self.data = True
  41. def _value(self):
  42. if self.raw_data:
  43. return str(self.raw_data[0])
  44. return "y"
  45. class StringField(Field):
  46. """
  47. This field is the base for most of the more complicated fields, and
  48. represents an ``<input type="text">``.
  49. """
  50. widget = widgets.TextInput()
  51. def process_formdata(self, valuelist):
  52. if valuelist:
  53. self.data = valuelist[0]
  54. def _value(self):
  55. return str(self.data) if self.data is not None else ""
  56. class TextAreaField(StringField):
  57. """
  58. This field represents an HTML ``<textarea>`` and can be used to take
  59. multi-line input.
  60. """
  61. widget = widgets.TextArea()
  62. class PasswordField(StringField):
  63. """
  64. A StringField, except renders an ``<input type="password">``.
  65. Also, whatever value is accepted by this field is not rendered back
  66. to the browser like normal fields.
  67. """
  68. widget = widgets.PasswordInput()
  69. class FileField(Field):
  70. """Renders a file upload field.
  71. By default, the value will be the filename sent in the form data.
  72. WTForms **does not** deal with frameworks' file handling capabilities.
  73. A WTForms extension for a framework may replace the filename value
  74. with an object representing the uploaded data.
  75. """
  76. widget = widgets.FileInput()
  77. def _value(self):
  78. # browser ignores value of file input for security
  79. return False
  80. class MultipleFileField(FileField):
  81. """A :class:`FileField` that allows choosing multiple files."""
  82. widget = widgets.FileInput(multiple=True)
  83. def process_formdata(self, valuelist):
  84. self.data = valuelist
  85. class HiddenField(StringField):
  86. """
  87. HiddenField is a convenience for a StringField with a HiddenInput widget.
  88. It will render as an ``<input type="hidden">`` but otherwise coerce to a string.
  89. """
  90. widget = widgets.HiddenInput()
  91. class SubmitField(BooleanField):
  92. """
  93. Represents an ``<input type="submit">``. This allows checking if a given
  94. submit button has been pressed.
  95. """
  96. widget = widgets.SubmitInput()
  97. class SearchField(StringField):
  98. """
  99. Represents an ``<input type="search">``.
  100. """
  101. widget = widgets.SearchInput()
  102. class TelField(StringField):
  103. """
  104. Represents an ``<input type="tel">``.
  105. """
  106. widget = widgets.TelInput()
  107. class URLField(StringField):
  108. """
  109. Represents an ``<input type="url">``.
  110. """
  111. widget = widgets.URLInput()
  112. class EmailField(StringField):
  113. """
  114. Represents an ``<input type="email">``.
  115. """
  116. widget = widgets.EmailInput()
  117. class ColorField(StringField):
  118. """
  119. Represents an ``<input type="color">``.
  120. """
  121. widget = widgets.ColorInput()