nicer renderer options handling: can be passed to __init__ or to render tls-sprint
authorsylvain.thenault@logilab.fr
Wed, 15 Apr 2009 17:22:28 +0200
branchtls-sprint
changeset 1362 decb7507fa91
parent 1361 c558a88bb85d
child 1363 d8f2c3953eb5
nicer renderer options handling: can be passed to __init__ or to render
web/formrenderers.py
--- a/web/formrenderers.py	Wed Apr 15 14:41:38 2009 +0200
+++ b/web/formrenderers.py	Wed Apr 15 17:22:28 2009 +0200
@@ -20,24 +20,35 @@
 class FormRenderer(object):
     """basic renderer displaying fields in a two columns table label | value
     """
+    display_fields = None # None -> all fields
+    display_label = True
+    display_help = True
+    display_progress_div = True
     button_bar_class = u'formButtonBar'
     
-    def __init__(self, display_fields=None, display_label=True,
-                 display_help=True, button_bar_class=None):
-        self.display_fields = display_fields # None -> all fields
-        self.display_label = display_label
-        self.display_help = display_help
-        if button_bar_class is not None:
-            self.button_bar_class = button_bar_class
-            
+    def __init__(self, **kwargs):
+        if self._set_options(kwargs):
+            raise ValueError('unconsumed arguments %s' % kwargs)
+
+    def _set_options(self, kwargs):
+        for key in ('display_fields', 'display_label', 'display_help',
+                    'display_progress_div', 'button_bar_class'):
+            try:
+                setattr(self, key, kwargs.pop(key))
+            except KeyError:
+                continue
+        return kwargs
+    
     # renderer interface ######################################################
     
     def render(self, form, values):
+        self._set_options(values)
         form.add_media()
         data = []
         w = data.append
         w(self.open_form(form, values))
-        w(u'<div id="progress">%s</div>' % form.req._('validating...'))
+        if self.display_progress_div:
+            w(u'<div id="progress">%s</div>' % form.req._('validating...'))
         w(u'<fieldset>')
         w(tags.input(type=u'hidden', name=u'__form_id',
                      value=values.get('formvid', form.id)))