# HG changeset patch # User Sylvain Thénault # Date 1248713008 -7200 # Node ID 1d245fbbeb9038b8da874d1bfad544cdc71f7983 # Parent 562f5dcf234581121a004b2818ea2803494e99f2 some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget diff -r 562f5dcf2345 -r 1d245fbbeb90 web/formfields.py --- a/web/formfields.py Mon Jul 27 18:42:49 2009 +0200 +++ b/web/formfields.py Mon Jul 27 18:43:28 2009 +0200 @@ -473,6 +473,15 @@ return value +class CompoundField(Field): + def __init__(self, fields, *args, **kwargs): + super(CompoundField, self).__init__(*args, **kwargs) + self.fields = fields + + def actual_fields(self, form): + return [self] + list(self.fields) + + def guess_field(eschema, rschema, role='subject', skip_meta_attr=True, **kwargs): """return the most adapated widget to edit the relation 'subjschema rschema objschema' according to information found in the schema diff -r 562f5dcf2345 -r 1d245fbbeb90 web/formwidgets.py --- a/web/formwidgets.py Mon Jul 27 18:42:49 2009 +0200 +++ b/web/formwidgets.py Mon Jul 27 18:43:28 2009 +0200 @@ -252,6 +252,51 @@ type = 'radio' +# compound widgets ############################################################# + +class IntervalWidget(FieldWidget): + """custom widget to display an interval composed by 2 fields. This widget + is expected to be used with a CompoundField containing the two actual + fields. + + Exemple usage:: + +from uicfg import autoform_field, autoform_section +autoform_field.tag_attribute(('Concert', 'minprice'), + CompoundField(fields=(IntField(name='minprice'), + IntField(name='maxprice')), + label=_('price'), + widget=IntervalWidget() + )) +# we've to hide the other field manually for now +autoform_section.tag_attribute(('Concert', 'maxprice'), 'generated') + """ + def render(self, form, field, renderer): + actual_fields = field.fields + assert len(actual_fields) == 2 + return u'
%s %s %s %s
' % ( + form.req._('from_interval_start'), + actual_fields[0].render(form, renderer), + form.req._('to_interval_end'), + actual_fields[1].render(form, renderer), + ) + + +class HorizontalLayoutWidget(FieldWidget): + """custom widget to display a set of fields grouped together horizontally + in a form. See `IntervalWidget` for example usage. + """ + def render(self, form, field, renderer): + if self.attrs.get('display_label', True): + subst = self.attrs.get('label_input_substitution', '%(label)s %(input)s') + fields = [subst % {'label': renderer.render_label(form, f), + 'input': f.render(form, renderer)} + for f in field.fields] + else: + fields = [f.render(form, renderer) for f in field.fields] + return u'
%s
' % ' '.join(fields) + + # javascript widgets ########################################################### class DateTimePicker(TextInput):