core.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  1. from __future__ import unicode_literals
  2. import datetime
  3. import decimal
  4. import itertools
  5. from wtforms import widgets
  6. from wtforms.compat import text_type, izip
  7. from wtforms.i18n import DummyTranslations
  8. from wtforms.validators import StopValidation
  9. from wtforms.utils import unset_value
  10. __all__ = (
  11. 'BooleanField', 'DecimalField', 'DateField', 'DateTimeField', 'FieldList',
  12. 'FloatField', 'FormField', 'IntegerField', 'RadioField', 'SelectField',
  13. 'SelectMultipleField', 'StringField',
  14. )
  15. class Field(object):
  16. """
  17. Field base class
  18. """
  19. errors = tuple()
  20. process_errors = tuple()
  21. raw_data = None
  22. validators = tuple()
  23. widget = None
  24. _formfield = True
  25. _translations = DummyTranslations()
  26. do_not_call_in_templates = True # Allow Django 1.4 traversal
  27. def __new__(cls, *args, **kwargs):
  28. if '_form' in kwargs and '_name' in kwargs:
  29. return super(Field, cls).__new__(cls)
  30. else:
  31. return UnboundField(cls, *args, **kwargs)
  32. def __init__(self, label=None, validators=None, filters=tuple(),
  33. description='', id=None, default=None, widget=None,
  34. render_kw=None, _form=None, _name=None, _prefix='',
  35. _translations=None, _meta=None):
  36. """
  37. Construct a new field.
  38. :param label:
  39. The label of the field.
  40. :param validators:
  41. A sequence of validators to call when `validate` is called.
  42. :param filters:
  43. A sequence of filters which are run on input data by `process`.
  44. :param description:
  45. A description for the field, typically used for help text.
  46. :param id:
  47. An id to use for the field. A reasonable default is set by the form,
  48. and you shouldn't need to set this manually.
  49. :param default:
  50. The default value to assign to the field, if no form or object
  51. input is provided. May be a callable.
  52. :param widget:
  53. If provided, overrides the widget used to render the field.
  54. :param dict render_kw:
  55. If provided, a dictionary which provides default keywords that
  56. will be given to the widget at render time.
  57. :param _form:
  58. The form holding this field. It is passed by the form itself during
  59. construction. You should never pass this value yourself.
  60. :param _name:
  61. The name of this field, passed by the enclosing form during its
  62. construction. You should never pass this value yourself.
  63. :param _prefix:
  64. The prefix to prepend to the form name of this field, passed by
  65. the enclosing form during construction.
  66. :param _translations:
  67. A translations object providing message translations. Usually
  68. passed by the enclosing form during construction. See
  69. :doc:`I18n docs <i18n>` for information on message translations.
  70. :param _meta:
  71. If provided, this is the 'meta' instance from the form. You usually
  72. don't pass this yourself.
  73. If `_form` and `_name` isn't provided, an :class:`UnboundField` will be
  74. returned instead. Call its :func:`bind` method with a form instance and
  75. a name to construct the field.
  76. """
  77. if _translations is not None:
  78. self._translations = _translations
  79. if _meta is not None:
  80. self.meta = _meta
  81. elif _form is not None:
  82. self.meta = _form.meta
  83. else:
  84. raise TypeError("Must provide one of _form or _meta")
  85. self.default = default
  86. self.description = description
  87. self.render_kw = render_kw
  88. self.filters = filters
  89. self.flags = Flags()
  90. self.name = _prefix + _name
  91. self.short_name = _name
  92. self.type = type(self).__name__
  93. self.validators = validators or list(self.validators)
  94. self.id = id or self.name
  95. self.label = Label(self.id, label if label is not None else self.gettext(_name.replace('_', ' ').title()))
  96. if widget is not None:
  97. self.widget = widget
  98. for v in itertools.chain(self.validators, [self.widget]):
  99. flags = getattr(v, 'field_flags', ())
  100. for f in flags:
  101. setattr(self.flags, f, True)
  102. def __unicode__(self):
  103. """
  104. Returns a HTML representation of the field. For more powerful rendering,
  105. see the `__call__` method.
  106. """
  107. return self()
  108. def __str__(self):
  109. """
  110. Returns a HTML representation of the field. For more powerful rendering,
  111. see the `__call__` method.
  112. """
  113. return self()
  114. def __html__(self):
  115. """
  116. Returns a HTML representation of the field. For more powerful rendering,
  117. see the :meth:`__call__` method.
  118. """
  119. return self()
  120. def __call__(self, **kwargs):
  121. """
  122. Render this field as HTML, using keyword args as additional attributes.
  123. This delegates rendering to
  124. :meth:`meta.render_field <wtforms.meta.DefaultMeta.render_field>`
  125. whose default behavior is to call the field's widget, passing any
  126. keyword arguments from this call along to the widget.
  127. In all of the WTForms HTML widgets, keyword arguments are turned to
  128. HTML attributes, though in theory a widget is free to do anything it
  129. wants with the supplied keyword arguments, and widgets don't have to
  130. even do anything related to HTML.
  131. """
  132. return self.meta.render_field(self, kwargs)
  133. def gettext(self, string):
  134. """
  135. Get a translation for the given message.
  136. This proxies for the internal translations object.
  137. :param string: A unicode string to be translated.
  138. :return: A unicode string which is the translated output.
  139. """
  140. return self._translations.gettext(string)
  141. def ngettext(self, singular, plural, n):
  142. """
  143. Get a translation for a message which can be pluralized.
  144. :param str singular: The singular form of the message.
  145. :param str plural: The plural form of the message.
  146. :param int n: The number of elements this message is referring to
  147. """
  148. return self._translations.ngettext(singular, plural, n)
  149. def validate(self, form, extra_validators=tuple()):
  150. """
  151. Validates the field and returns True or False. `self.errors` will
  152. contain any errors raised during validation. This is usually only
  153. called by `Form.validate`.
  154. Subfields shouldn't override this, but rather override either
  155. `pre_validate`, `post_validate` or both, depending on needs.
  156. :param form: The form the field belongs to.
  157. :param extra_validators: A sequence of extra validators to run.
  158. """
  159. self.errors = list(self.process_errors)
  160. stop_validation = False
  161. # Call pre_validate
  162. try:
  163. self.pre_validate(form)
  164. except StopValidation as e:
  165. if e.args and e.args[0]:
  166. self.errors.append(e.args[0])
  167. stop_validation = True
  168. except ValueError as e:
  169. self.errors.append(e.args[0])
  170. # Run validators
  171. if not stop_validation:
  172. chain = itertools.chain(self.validators, extra_validators)
  173. stop_validation = self._run_validation_chain(form, chain)
  174. # Call post_validate
  175. try:
  176. self.post_validate(form, stop_validation)
  177. except ValueError as e:
  178. self.errors.append(e.args[0])
  179. return len(self.errors) == 0
  180. def _run_validation_chain(self, form, validators):
  181. """
  182. Run a validation chain, stopping if any validator raises StopValidation.
  183. :param form: The Form instance this field beongs to.
  184. :param validators: a sequence or iterable of validator callables.
  185. :return: True if validation was stopped, False otherwise.
  186. """
  187. for validator in validators:
  188. try:
  189. validator(form, self)
  190. except StopValidation as e:
  191. if e.args and e.args[0]:
  192. self.errors.append(e.args[0])
  193. return True
  194. except ValueError as e:
  195. self.errors.append(e.args[0])
  196. return False
  197. def pre_validate(self, form):
  198. """
  199. Override if you need field-level validation. Runs before any other
  200. validators.
  201. :param form: The form the field belongs to.
  202. """
  203. pass
  204. def post_validate(self, form, validation_stopped):
  205. """
  206. Override if you need to run any field-level validation tasks after
  207. normal validation. This shouldn't be needed in most cases.
  208. :param form: The form the field belongs to.
  209. :param validation_stopped:
  210. `True` if any validator raised StopValidation.
  211. """
  212. pass
  213. def process(self, formdata, data=unset_value):
  214. """
  215. Process incoming data, calling process_data, process_formdata as needed,
  216. and run filters.
  217. If `data` is not provided, process_data will be called on the field's
  218. default.
  219. Field subclasses usually won't override this, instead overriding the
  220. process_formdata and process_data methods. Only override this for
  221. special advanced processing, such as when a field encapsulates many
  222. inputs.
  223. """
  224. self.process_errors = []
  225. if data is unset_value:
  226. try:
  227. data = self.default()
  228. except TypeError:
  229. data = self.default
  230. self.object_data = data
  231. try:
  232. self.process_data(data)
  233. except ValueError as e:
  234. self.process_errors.append(e.args[0])
  235. if formdata:
  236. try:
  237. if self.name in formdata:
  238. self.raw_data = formdata.getlist(self.name)
  239. else:
  240. self.raw_data = []
  241. self.process_formdata(self.raw_data)
  242. except ValueError as e:
  243. self.process_errors.append(e.args[0])
  244. try:
  245. for filter in self.filters:
  246. self.data = filter(self.data)
  247. except ValueError as e:
  248. self.process_errors.append(e.args[0])
  249. def process_data(self, value):
  250. """
  251. Process the Python data applied to this field and store the result.
  252. This will be called during form construction by the form's `kwargs` or
  253. `obj` argument.
  254. :param value: The python object containing the value to process.
  255. """
  256. self.data = value
  257. def process_formdata(self, valuelist):
  258. """
  259. Process data received over the wire from a form.
  260. This will be called during form construction with data supplied
  261. through the `formdata` argument.
  262. :param valuelist: A list of strings to process.
  263. """
  264. if valuelist:
  265. self.data = valuelist[0]
  266. def populate_obj(self, obj, name):
  267. """
  268. Populates `obj.<name>` with the field's data.
  269. :note: This is a destructive operation. If `obj.<name>` already exists,
  270. it will be overridden. Use with caution.
  271. """
  272. setattr(obj, name, self.data)
  273. class UnboundField(object):
  274. _formfield = True
  275. creation_counter = 0
  276. def __init__(self, field_class, *args, **kwargs):
  277. UnboundField.creation_counter += 1
  278. self.field_class = field_class
  279. self.args = args
  280. self.kwargs = kwargs
  281. self.creation_counter = UnboundField.creation_counter
  282. def bind(self, form, name, prefix='', translations=None, **kwargs):
  283. kw = dict(
  284. self.kwargs,
  285. _form=form,
  286. _prefix=prefix,
  287. _name=name,
  288. _translations=translations,
  289. **kwargs
  290. )
  291. return self.field_class(*self.args, **kw)
  292. def __repr__(self):
  293. return '<UnboundField(%s, %r, %r)>' % (self.field_class.__name__, self.args, self.kwargs)
  294. class Flags(object):
  295. """
  296. Holds a set of boolean flags as attributes.
  297. Accessing a non-existing attribute returns False for its value.
  298. """
  299. def __getattr__(self, name):
  300. if name.startswith('_'):
  301. return super(Flags, self).__getattr__(name)
  302. return False
  303. def __contains__(self, name):
  304. return getattr(self, name)
  305. def __repr__(self):
  306. flags = (name for name in dir(self) if not name.startswith('_'))
  307. return '<wtforms.fields.Flags: {%s}>' % ', '.join(flags)
  308. class Label(object):
  309. """
  310. An HTML form label.
  311. """
  312. def __init__(self, field_id, text):
  313. self.field_id = field_id
  314. self.text = text
  315. def __str__(self):
  316. return self()
  317. def __unicode__(self):
  318. return self()
  319. def __html__(self):
  320. return self()
  321. def __call__(self, text=None, **kwargs):
  322. if 'for_' in kwargs:
  323. kwargs['for'] = kwargs.pop('for_')
  324. else:
  325. kwargs.setdefault('for', self.field_id)
  326. attributes = widgets.html_params(**kwargs)
  327. return widgets.HTMLString('<label %s>%s</label>' % (attributes, text or self.text))
  328. def __repr__(self):
  329. return 'Label(%r, %r)' % (self.field_id, self.text)
  330. class SelectFieldBase(Field):
  331. option_widget = widgets.Option()
  332. """
  333. Base class for fields which can be iterated to produce options.
  334. This isn't a field, but an abstract base class for fields which want to
  335. provide this functionality.
  336. """
  337. def __init__(self, label=None, validators=None, option_widget=None, **kwargs):
  338. super(SelectFieldBase, self).__init__(label, validators, **kwargs)
  339. if option_widget is not None:
  340. self.option_widget = option_widget
  341. def iter_choices(self):
  342. """
  343. Provides data for choice widget rendering. Must return a sequence or
  344. iterable of (value, label, selected) tuples.
  345. """
  346. raise NotImplementedError()
  347. def __iter__(self):
  348. opts = dict(widget=self.option_widget, _name=self.name, _form=None, _meta=self.meta)
  349. for i, (value, label, checked) in enumerate(self.iter_choices()):
  350. opt = self._Option(label=label, id='%s-%d' % (self.id, i), **opts)
  351. opt.process(None, value)
  352. opt.checked = checked
  353. yield opt
  354. class _Option(Field):
  355. checked = False
  356. def _value(self):
  357. return text_type(self.data)
  358. class SelectField(SelectFieldBase):
  359. widget = widgets.Select()
  360. def __init__(self, label=None, validators=None, coerce=text_type, choices=None, **kwargs):
  361. super(SelectField, self).__init__(label, validators, **kwargs)
  362. self.coerce = coerce
  363. self.choices = choices
  364. def iter_choices(self):
  365. for value, label in self.choices:
  366. yield (value, label, self.coerce(value) == self.data)
  367. def process_data(self, value):
  368. try:
  369. self.data = self.coerce(value)
  370. except (ValueError, TypeError):
  371. self.data = None
  372. def process_formdata(self, valuelist):
  373. if valuelist:
  374. try:
  375. self.data = self.coerce(valuelist[0])
  376. except ValueError:
  377. raise ValueError(self.gettext('Invalid Choice: could not coerce'))
  378. def pre_validate(self, form):
  379. for v, _ in self.choices:
  380. if self.data == v:
  381. break
  382. else:
  383. raise ValueError(self.gettext('Not a valid choice'))
  384. class SelectMultipleField(SelectField):
  385. """
  386. No different from a normal select field, except this one can take (and
  387. validate) multiple choices. You'll need to specify the HTML `size`
  388. attribute to the select field when rendering.
  389. """
  390. widget = widgets.Select(multiple=True)
  391. def iter_choices(self):
  392. for value, label in self.choices:
  393. selected = self.data is not None and self.coerce(value) in self.data
  394. yield (value, label, selected)
  395. def process_data(self, value):
  396. try:
  397. self.data = list(self.coerce(v) for v in value)
  398. except (ValueError, TypeError):
  399. self.data = None
  400. def process_formdata(self, valuelist):
  401. try:
  402. self.data = list(self.coerce(x) for x in valuelist)
  403. except ValueError:
  404. raise ValueError(self.gettext('Invalid choice(s): one or more data inputs could not be coerced'))
  405. def pre_validate(self, form):
  406. if self.data:
  407. values = list(c[0] for c in self.choices)
  408. for d in self.data:
  409. if d not in values:
  410. raise ValueError(self.gettext("'%(value)s' is not a valid choice for this field") % dict(value=d))
  411. class RadioField(SelectField):
  412. """
  413. Like a SelectField, except displays a list of radio buttons.
  414. Iterating the field will produce subfields (each containing a label as
  415. well) in order to allow custom rendering of the individual radio fields.
  416. """
  417. widget = widgets.ListWidget(prefix_label=False)
  418. option_widget = widgets.RadioInput()
  419. class StringField(Field):
  420. """
  421. This field is the base for most of the more complicated fields, and
  422. represents an ``<input type="text">``.
  423. """
  424. widget = widgets.TextInput()
  425. def process_formdata(self, valuelist):
  426. if valuelist:
  427. self.data = valuelist[0]
  428. else:
  429. self.data = ''
  430. def _value(self):
  431. return text_type(self.data) if self.data is not None else ''
  432. class LocaleAwareNumberField(Field):
  433. """
  434. Base class for implementing locale-aware number parsing.
  435. Locale-aware numbers require the 'babel' package to be present.
  436. """
  437. def __init__(self, label=None, validators=None, use_locale=False, number_format=None, **kwargs):
  438. super(LocaleAwareNumberField, self).__init__(label, validators, **kwargs)
  439. self.use_locale = use_locale
  440. if use_locale:
  441. self.number_format = number_format
  442. self.locale = kwargs['_form'].meta.locales[0]
  443. self._init_babel()
  444. def _init_babel(self):
  445. try:
  446. from babel import numbers
  447. self.babel_numbers = numbers
  448. except ImportError:
  449. raise ImportError('Using locale-aware decimals requires the babel library.')
  450. def _parse_decimal(self, value):
  451. return self.babel_numbers.parse_decimal(value, self.locale)
  452. def _format_decimal(self, value):
  453. return self.babel_numbers.format_decimal(value, self.number_format, self.locale)
  454. class IntegerField(Field):
  455. """
  456. A text field, except all input is coerced to an integer. Erroneous input
  457. is ignored and will not be accepted as a value.
  458. """
  459. widget = widgets.TextInput()
  460. def __init__(self, label=None, validators=None, **kwargs):
  461. super(IntegerField, self).__init__(label, validators, **kwargs)
  462. def _value(self):
  463. if self.raw_data:
  464. return self.raw_data[0]
  465. elif self.data is not None:
  466. return text_type(self.data)
  467. else:
  468. return ''
  469. def process_formdata(self, valuelist):
  470. if valuelist:
  471. try:
  472. self.data = int(valuelist[0])
  473. except ValueError:
  474. self.data = None
  475. raise ValueError(self.gettext('Not a valid integer value'))
  476. class DecimalField(LocaleAwareNumberField):
  477. """
  478. A text field which displays and coerces data of the `decimal.Decimal` type.
  479. :param places:
  480. How many decimal places to quantize the value to for display on form.
  481. If None, does not quantize value.
  482. :param rounding:
  483. How to round the value during quantize, for example
  484. `decimal.ROUND_UP`. If unset, uses the rounding value from the
  485. current thread's context.
  486. :param use_locale:
  487. If True, use locale-based number formatting. Locale-based number
  488. formatting requires the 'babel' package.
  489. :param number_format:
  490. Optional number format for locale. If omitted, use the default decimal
  491. format for the locale.
  492. """
  493. widget = widgets.TextInput()
  494. def __init__(self, label=None, validators=None, places=unset_value, rounding=None, **kwargs):
  495. super(DecimalField, self).__init__(label, validators, **kwargs)
  496. if self.use_locale and (places is not unset_value or rounding is not None):
  497. raise TypeError("When using locale-aware numbers, 'places' and 'rounding' are ignored.")
  498. if places is unset_value:
  499. places = 2
  500. self.places = places
  501. self.rounding = rounding
  502. def _value(self):
  503. if self.raw_data:
  504. return self.raw_data[0]
  505. elif self.data is not None:
  506. if self.use_locale:
  507. return text_type(self._format_decimal(self.data))
  508. elif self.places is not None:
  509. if hasattr(self.data, 'quantize'):
  510. exp = decimal.Decimal('.1') ** self.places
  511. if self.rounding is None:
  512. quantized = self.data.quantize(exp)
  513. else:
  514. quantized = self.data.quantize(exp, rounding=self.rounding)
  515. return text_type(quantized)
  516. else:
  517. # If for some reason, data is a float or int, then format
  518. # as we would for floats using string formatting.
  519. format = '%%0.%df' % self.places
  520. return format % self.data
  521. else:
  522. return text_type(self.data)
  523. else:
  524. return ''
  525. def process_formdata(self, valuelist):
  526. if valuelist:
  527. try:
  528. if self.use_locale:
  529. self.data = self._parse_decimal(valuelist[0])
  530. else:
  531. self.data = decimal.Decimal(valuelist[0])
  532. except (decimal.InvalidOperation, ValueError):
  533. self.data = None
  534. raise ValueError(self.gettext('Not a valid decimal value'))
  535. class FloatField(Field):
  536. """
  537. A text field, except all input is coerced to an float. Erroneous input
  538. is ignored and will not be accepted as a value.
  539. """
  540. widget = widgets.TextInput()
  541. def __init__(self, label=None, validators=None, **kwargs):
  542. super(FloatField, self).__init__(label, validators, **kwargs)
  543. def _value(self):
  544. if self.raw_data:
  545. return self.raw_data[0]
  546. elif self.data is not None:
  547. return text_type(self.data)
  548. else:
  549. return ''
  550. def process_formdata(self, valuelist):
  551. if valuelist:
  552. try:
  553. self.data = float(valuelist[0])
  554. except ValueError:
  555. self.data = None
  556. raise ValueError(self.gettext('Not a valid float value'))
  557. class BooleanField(Field):
  558. """
  559. Represents an ``<input type="checkbox">``. Set the ``checked``-status by using the
  560. ``default``-option. Any value for ``default``, e.g. ``default="checked"`` puts
  561. ``checked`` into the html-element and sets the ``data`` to ``True``
  562. :param false_values:
  563. If provided, a sequence of strings each of which is an exact match
  564. string of what is considered a "false" value. Defaults to the tuple
  565. ``('false', '')``
  566. """
  567. widget = widgets.CheckboxInput()
  568. false_values = ('false', '')
  569. def __init__(self, label=None, validators=None, false_values=None, **kwargs):
  570. super(BooleanField, self).__init__(label, validators, **kwargs)
  571. if false_values is not None:
  572. self.false_values = false_values
  573. def process_data(self, value):
  574. self.data = bool(value)
  575. def process_formdata(self, valuelist):
  576. if not valuelist or valuelist[0] in self.false_values:
  577. self.data = False
  578. else:
  579. self.data = True
  580. def _value(self):
  581. if self.raw_data:
  582. return text_type(self.raw_data[0])
  583. else:
  584. return 'y'
  585. class DateTimeField(Field):
  586. """
  587. A text field which stores a `datetime.datetime` matching a format.
  588. """
  589. widget = widgets.TextInput()
  590. def __init__(self, label=None, validators=None, format='%Y-%m-%d %H:%M:%S', **kwargs):
  591. super(DateTimeField, self).__init__(label, validators, **kwargs)
  592. self.format = format
  593. def _value(self):
  594. if self.raw_data:
  595. return ' '.join(self.raw_data)
  596. else:
  597. return self.data and self.data.strftime(self.format) or ''
  598. def process_formdata(self, valuelist):
  599. if valuelist:
  600. date_str = ' '.join(valuelist)
  601. try:
  602. self.data = datetime.datetime.strptime(date_str, self.format)
  603. except ValueError:
  604. self.data = None
  605. raise ValueError(self.gettext('Not a valid datetime value'))
  606. class DateField(DateTimeField):
  607. """
  608. Same as DateTimeField, except stores a `datetime.date`.
  609. """
  610. def __init__(self, label=None, validators=None, format='%Y-%m-%d', **kwargs):
  611. super(DateField, self).__init__(label, validators, format, **kwargs)
  612. def process_formdata(self, valuelist):
  613. if valuelist:
  614. date_str = ' '.join(valuelist)
  615. try:
  616. self.data = datetime.datetime.strptime(date_str, self.format).date()
  617. except ValueError:
  618. self.data = None
  619. raise ValueError(self.gettext('Not a valid date value'))
  620. class FormField(Field):
  621. """
  622. Encapsulate a form as a field in another form.
  623. :param form_class:
  624. A subclass of Form that will be encapsulated.
  625. :param separator:
  626. A string which will be suffixed to this field's name to create the
  627. prefix to enclosed fields. The default is fine for most uses.
  628. """
  629. widget = widgets.TableWidget()
  630. def __init__(self, form_class, label=None, validators=None, separator='-', **kwargs):
  631. super(FormField, self).__init__(label, validators, **kwargs)
  632. self.form_class = form_class
  633. self.separator = separator
  634. self._obj = None
  635. if self.filters:
  636. raise TypeError('FormField cannot take filters, as the encapsulated data is not mutable.')
  637. if validators:
  638. raise TypeError('FormField does not accept any validators. Instead, define them on the enclosed form.')
  639. def process(self, formdata, data=unset_value):
  640. if data is unset_value:
  641. try:
  642. data = self.default()
  643. except TypeError:
  644. data = self.default
  645. self._obj = data
  646. self.object_data = data
  647. prefix = self.name + self.separator
  648. if isinstance(data, dict):
  649. self.form = self.form_class(formdata=formdata, prefix=prefix, **data)
  650. else:
  651. self.form = self.form_class(formdata=formdata, obj=data, prefix=prefix)
  652. def validate(self, form, extra_validators=tuple()):
  653. if extra_validators:
  654. raise TypeError('FormField does not accept in-line validators, as it gets errors from the enclosed form.')
  655. return self.form.validate()
  656. def populate_obj(self, obj, name):
  657. candidate = getattr(obj, name, None)
  658. if candidate is None:
  659. if self._obj is None:
  660. raise TypeError('populate_obj: cannot find a value to populate from the provided obj or input data/defaults')
  661. candidate = self._obj
  662. setattr(obj, name, candidate)
  663. self.form.populate_obj(candidate)
  664. def __iter__(self):
  665. return iter(self.form)
  666. def __getitem__(self, name):
  667. return self.form[name]
  668. def __getattr__(self, name):
  669. return getattr(self.form, name)
  670. @property
  671. def data(self):
  672. return self.form.data
  673. @property
  674. def errors(self):
  675. return self.form.errors
  676. class FieldList(Field):
  677. """
  678. Encapsulate an ordered list of multiple instances of the same field type,
  679. keeping data as a list.
  680. >>> authors = FieldList(StringField('Name', [validators.required()]))
  681. :param unbound_field:
  682. A partially-instantiated field definition, just like that would be
  683. defined on a form directly.
  684. :param min_entries:
  685. if provided, always have at least this many entries on the field,
  686. creating blank ones if the provided input does not specify a sufficient
  687. amount.
  688. :param max_entries:
  689. accept no more than this many entries as input, even if more exist in
  690. formdata.
  691. """
  692. widget = widgets.ListWidget()
  693. def __init__(self, unbound_field, label=None, validators=None, min_entries=0,
  694. max_entries=None, default=tuple(), **kwargs):
  695. super(FieldList, self).__init__(label, validators, default=default, **kwargs)
  696. if self.filters:
  697. raise TypeError('FieldList does not accept any filters. Instead, define them on the enclosed field.')
  698. assert isinstance(unbound_field, UnboundField), 'Field must be unbound, not a field class'
  699. self.unbound_field = unbound_field
  700. self.min_entries = min_entries
  701. self.max_entries = max_entries
  702. self.last_index = -1
  703. self._prefix = kwargs.get('_prefix', '')
  704. def process(self, formdata, data=unset_value):
  705. self.entries = []
  706. if data is unset_value or not data:
  707. try:
  708. data = self.default()
  709. except TypeError:
  710. data = self.default
  711. self.object_data = data
  712. if formdata:
  713. indices = sorted(set(self._extract_indices(self.name, formdata)))
  714. if self.max_entries:
  715. indices = indices[:self.max_entries]
  716. idata = iter(data)
  717. for index in indices:
  718. try:
  719. obj_data = next(idata)
  720. except StopIteration:
  721. obj_data = unset_value
  722. self._add_entry(formdata, obj_data, index=index)
  723. else:
  724. for obj_data in data:
  725. self._add_entry(formdata, obj_data)
  726. while len(self.entries) < self.min_entries:
  727. self._add_entry(formdata)
  728. def _extract_indices(self, prefix, formdata):
  729. """
  730. Yield indices of any keys with given prefix.
  731. formdata must be an object which will produce keys when iterated. For
  732. example, if field 'foo' contains keys 'foo-0-bar', 'foo-1-baz', then
  733. the numbers 0 and 1 will be yielded, but not neccesarily in order.
  734. """
  735. offset = len(prefix) + 1
  736. for k in formdata:
  737. if k.startswith(prefix):
  738. k = k[offset:].split('-', 1)[0]
  739. if k.isdigit():
  740. yield int(k)
  741. def validate(self, form, extra_validators=tuple()):
  742. """
  743. Validate this FieldList.
  744. Note that FieldList validation differs from normal field validation in
  745. that FieldList validates all its enclosed fields first before running any
  746. of its own validators.
  747. """
  748. self.errors = []
  749. # Run validators on all entries within
  750. for subfield in self.entries:
  751. if not subfield.validate(form):
  752. self.errors.append(subfield.errors)
  753. chain = itertools.chain(self.validators, extra_validators)
  754. self._run_validation_chain(form, chain)
  755. return len(self.errors) == 0
  756. def populate_obj(self, obj, name):
  757. values = getattr(obj, name, None)
  758. try:
  759. ivalues = iter(values)
  760. except TypeError:
  761. ivalues = iter([])
  762. candidates = itertools.chain(ivalues, itertools.repeat(None))
  763. _fake = type(str('_fake'), (object, ), {})
  764. output = []
  765. for field, data in izip(self.entries, candidates):
  766. fake_obj = _fake()
  767. fake_obj.data = data
  768. field.populate_obj(fake_obj, 'data')
  769. output.append(fake_obj.data)
  770. setattr(obj, name, output)
  771. def _add_entry(self, formdata=None, data=unset_value, index=None):
  772. assert not self.max_entries or len(self.entries) < self.max_entries, \
  773. 'You cannot have more than max_entries entries in this FieldList'
  774. if index is None:
  775. index = self.last_index + 1
  776. self.last_index = index
  777. name = '%s-%d' % (self.short_name, index)
  778. id = '%s-%d' % (self.id, index)
  779. field = self.unbound_field.bind(form=None, name=name, prefix=self._prefix, id=id, _meta=self.meta,
  780. translations=self._translations)
  781. field.process(formdata, data)
  782. self.entries.append(field)
  783. return field
  784. def append_entry(self, data=unset_value):
  785. """
  786. Create a new entry with optional default data.
  787. Entries added in this way will *not* receive formdata however, and can
  788. only receive object data.
  789. """
  790. return self._add_entry(data=data)
  791. def pop_entry(self):
  792. """ Removes the last entry from the list and returns it. """
  793. entry = self.entries.pop()
  794. self.last_index -= 1
  795. return entry
  796. def __iter__(self):
  797. return iter(self.entries)
  798. def __len__(self):
  799. return len(self.entries)
  800. def __getitem__(self, index):
  801. return self.entries[index]
  802. @property
  803. def data(self):
  804. return [f.data for f in self.entries]