Skip to content

Commit b79b0b5

Browse files
committed
Fix issue with field label and desc overrides
We had an issue where our overrides to a field's label or description applied to all instances of that field forever. This fixes that so we can actually have different labels for the same field in child form classes.
1 parent 4252ece commit b79b0b5

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

uber/forms/__init__.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import cherrypy
44

55
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
77
import wtforms.widgets.core as wtforms_widgets
88
from wtforms.validators import ValidationError
99
from pockets.autolog import log
@@ -287,36 +287,34 @@ def get_field_type(self, field):
287287

288288
def bind_field(self, form, unbound_field, options):
289289
"""
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:
291291
- Get a label and description override from a function on the form class, if there is one
292292
- Format label and description text to process common variables
293293
- 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
296294
"""
295+
296+
bound_field = unbound_field.bind(form=form, **options)
297+
297298
field_name = options.get('name', '')
299+
298300
if hasattr(form, field_name + '_label'):
299301
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)
306304

307305
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')
309307

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)
313311

314312
# Allow overriding the default kwargs via kwarg_overrides
315313
if field_name in form.kwarg_overrides:
316314
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
318316

319-
return unbound_field.bind(form=form, **options)
317+
return bound_field
320318

321319
def set_keyword_defaults(self, ufield, render_kw, field_name):
322320
# Changes the render_kw dictionary to implement some high-level defaults

0 commit comments

Comments
 (0)