introduce a default render implementation on the base widget, which
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 26 Jan 2010 16:46:12 +0100
changeset 4372 0c44af150a49
parent 4371 0d337feb5374
child 4373 972143183ea3
introduce a default render implementation on the base widget, which add_media and then call the newly introduced method _render(form, field, formrenderer), abstract of the base widget class. This allow a litle code factorisation and avoid to forget the call to add_media in subclasses.
web/formwidgets.py
--- a/web/formwidgets.py	Tue Jan 26 16:43:51 2010 +0100
+++ b/web/formwidgets.py	Tue Jan 26 16:46:12 2010 +0100
@@ -49,11 +49,13 @@
         if self.needs_css:
             form._cw.add_css(self.needs_css)
 
-    def render(self, form, field, renderer):
-        """render the widget for the given `field` of `form`.
-        To override in concrete class
-        """
-        raise NotImplementedError
+
+    def render(self, form, field, renderer=None):
+        self.add_media(form)
+        return self._render(form, field, renderer)
+
+    def _render(self, form, field, renderer):
+        raise NotImplementedError()
 
     def typed_value(self, form, field):
         """return field's *typed* value specified in:
@@ -128,12 +130,11 @@
     """abstract widget class for <input> tag based widgets"""
     type = None
 
-    def render(self, form, field, renderer):
+    def _render(self, form, field, renderer):
         """render the widget for the given `field` of `form`.
 
         Generate one <input> tag for each field's value
         """
-        self.add_media(form)
         values, attrs = self.values_and_attributes(form, field)
         # ensure something is rendered
         if not values:
@@ -157,8 +158,8 @@
     """
     type = 'password'
 
-    def render(self, form, field, renderer):
-        self.add_media(form)
+    def _render(self, form, field, renderer):
+        assert self.suffix is None, 'suffix not supported'
         values, attrs = self.values_and_attributes(form, field)
         assert len(values) == 1
         id = attrs.pop('id')
@@ -221,7 +222,7 @@
 class TextArea(FieldWidget):
     """<textarea>"""
 
-    def render(self, form, field, renderer):
+    def _render(self, form, field, renderer):
         values, attrs = self.values_and_attributes(form, field)
         attrs.setdefault('onkeyup', 'autogrow(this)')
         if not values:
@@ -246,9 +247,9 @@
         super(FCKEditor, self).__init__(*args, **kwargs)
         self.attrs['cubicweb:type'] = 'wysiwyg'
 
-    def render(self, form, field, renderer):
+    def _render(self, form, field, renderer):
         form._cw.fckeditor_config()
-        return super(FCKEditor, self).render(form, field, renderer)
+        return super(FCKEditor, self)._render(form, field, renderer)
 
 
 class Select(FieldWidget):
@@ -436,8 +437,7 @@
         if inputid is not None:
             self.attrs['cubicweb:inputid'] = inputid
 
-    def render(self, form, field, renderer):
-        self.add_media(form)
+    def _render(self, form, field, renderer):
         attrs = self.values_and_attributes(form, field)[-1]
         return tags.div(**attrs)
 
@@ -608,12 +608,12 @@
     """
     type = 'text'
 
-    def render(self, form, field, renderer):
+    def _render(self, form, field, renderer):
         """render the widget for the given `field` of `form`.
 
         Generate one <input> tag for each field's value
         """
-        self.add_media(form)
+        assert self.suffix is None, 'not supported'
         req = form._cw
         pathqname = field.input_name(form, 'path')
         fqsqname = field.input_name(form, 'fqs') # formatted query string