|
3 | 3 | import cherrypy
|
4 | 4 |
|
5 | 5 | from collections import defaultdict, OrderedDict
|
6 |
| -from wtforms import Form, StringField, SelectField, SelectMultipleField, IntegerField, BooleanField, validators |
| 6 | +from wtforms import Form, StringField, SelectField, SelectMultipleField, IntegerField, BooleanField, validators, Label |
7 | 7 | import wtforms.widgets.core as wtforms_widgets
|
8 | 8 | from wtforms.validators import ValidationError
|
9 | 9 | from pockets.autolog import log
|
@@ -287,36 +287,34 @@ def get_field_type(self, field):
|
287 | 287 |
|
288 | 288 | def bind_field(self, form, unbound_field, options):
|
289 | 289 | """
|
290 |
| - This function implements all our custom logic to apply to fields upon init. Currently, we: |
| 290 | + This function implements all our custom logic to apply when initializing a form. Currently, we: |
291 | 291 | - Get a label and description override from a function on the form class, if there is one
|
292 | 292 | - Format label and description text to process common variables
|
293 | 293 | - Add default rendering keywords to make fields function better in our forms
|
294 |
| -
|
295 |
| - TODO: Changes to field attributes are permanent, so this code only needs to run once per field |
296 | 294 | """
|
| 295 | + |
| 296 | + bound_field = unbound_field.bind(form=form, **options) |
| 297 | + |
297 | 298 | field_name = options.get('name', '')
|
| 299 | + |
298 | 300 | if hasattr(form, field_name + '_label'):
|
299 | 301 | field_label = get_override_attr(form, field_name, '_label')
|
300 |
| - if 'label' in unbound_field.kwargs: |
301 |
| - unbound_field.kwargs['label'] = field_label |
302 |
| - elif unbound_field.args and isinstance(unbound_field.args[0], str): |
303 |
| - args_list = list(unbound_field.args) |
304 |
| - args_list[0] = field_label |
305 |
| - unbound_field.args = tuple(args_list) |
| 302 | + |
| 303 | + bound_field.label = Label(bound_field.id, field_label) |
306 | 304 |
|
307 | 305 | if hasattr(form, field_name + '_desc'):
|
308 |
| - unbound_field.kwargs['description'] = get_override_attr(form, field_name, '_desc') |
| 306 | + bound_field.description = get_override_attr(form, field_name, '_desc') |
309 | 307 |
|
310 |
| - unbound_field.kwargs['render_kw'] = self.set_keyword_defaults(unbound_field, |
311 |
| - unbound_field.kwargs.get('render_kw', {}), |
312 |
| - field_name) |
| 308 | + bound_field.render_kw = self.set_keyword_defaults(unbound_field, |
| 309 | + unbound_field.kwargs.get('render_kw', {}), |
| 310 | + field_name) |
313 | 311 |
|
314 | 312 | # Allow overriding the default kwargs via kwarg_overrides
|
315 | 313 | if field_name in form.kwarg_overrides:
|
316 | 314 | for kw, val in form.kwarg_overrides[field_name].items():
|
317 |
| - unbound_field.kwargs['render_kw'][kw] = val |
| 315 | + bound_field.render_kw[kw] = val |
318 | 316 |
|
319 |
| - return unbound_field.bind(form=form, **options) |
| 317 | + return bound_field |
320 | 318 |
|
321 | 319 | def set_keyword_defaults(self, ufield, render_kw, field_name):
|
322 | 320 | # Changes the render_kw dictionary to implement some high-level defaults
|
|
0 commit comments