some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 27 Jul 2009 18:43:28 +0200
changeset 2523 1d245fbbeb90
parent 2522 562f5dcf2345
child 2525 19eaaf9aebf2
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
web/formfields.py
web/formwidgets.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
--- 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'<div>%s %s %s %s</div>' % (
+            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'<div>%s</div>' % ' '.join(fields)
+
+
 # javascript widgets ###########################################################
 
 class DateTimePicker(TextInput):