[form] unify form.render prototype to take a 'w' argument as other view/components render method
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 12 Oct 2010 14:10:58 +0200
changeset 6453 7fdd780d87e4
parent 6452 6b6a2fa28069
child 6454 97203d0af4cb
[form] unify form.render prototype to take a 'w' argument as other view/components render method
web/views/basetemplates.py
web/views/cwproperties.py
web/views/editforms.py
web/views/formrenderers.py
web/views/forms.py
web/views/management.py
--- a/web/views/basetemplates.py	Tue Oct 12 12:12:05 2010 +0200
+++ b/web/views/basetemplates.py	Tue Oct 12 14:10:58 2010 +0200
@@ -479,7 +479,7 @@
         else:
             label = cw.pgettext('CWUser', 'login')
         form.field_by_name('__login').label = label
-        self.w(form.render(table_class='', display_progress_div=False))
+        form.render(w=self.w, table_class='', display_progress_div=False)
         cw.html_headers.add_onload('jQuery("#__login:visible").focus()')
 
 LogFormTemplate = class_renamed('LogFormTemplate', LogFormView)
--- a/web/views/cwproperties.py	Tue Oct 12 12:12:05 2010 +0200
+++ b/web/views/cwproperties.py	Tue Oct 12 14:10:58 2010 +0200
@@ -219,7 +219,9 @@
             self.form_row(form, key, splitlabel)
         renderer = self._cw.vreg['formrenderers'].select('cwproperties', self._cw,
                                                      display_progress_div=False)
-        return form.render(renderer=renderer)
+        data = []
+        form.render(w=data.append, renderer=renderer)
+        return u'\n'.join(data)
 
     def form_row(self, form, key, splitlabel):
         entity = self.entity_for_key(key)
--- a/web/views/editforms.py	Tue Oct 12 12:12:05 2010 +0200
+++ b/web/views/editforms.py	Tue Oct 12 14:10:58 2010 +0200
@@ -90,7 +90,7 @@
             w(u'<li>%s</li>' % tags.a(entity.view('textoutofcontext'),
                                       href=entity.absolute_url()))
         w(u'</ul>\n')
-        w(form.render())
+        form.render(w=self.w)
 
 
 class EditionFormView(FormViewMixIn, EntityView):
@@ -112,7 +112,7 @@
         form = self._cw.vreg['forms'].select('edition', self._cw, entity=entity,
                                              submitmsg=self.submited_message())
         self.init_form(form, entity)
-        self.w(form.render())
+        form.render(w=self.w)
 
     def init_form(self, form, entity):
         """customize your form before rendering here"""
@@ -258,7 +258,7 @@
                                              rset=self.cw_rset,
                                              copy_nav_params=True,
                                              formvid='edition')
-        self.w(form.render())
+        form.render(w=self.w)
 
 
 # click and edit handling ('reledit') ##########################################
--- a/web/views/formrenderers.py	Tue Oct 12 12:12:05 2010 +0200
+++ b/web/views/formrenderers.py	Tue Oct 12 14:10:58 2010 +0200
@@ -104,11 +104,9 @@
 
     # renderer interface ######################################################
 
-    def render(self, form, values):
+    def render(self, w, form, values):
         self._set_options(values)
         form.add_media()
-        data = []
-        w = data.append
         w(self.open_form(form, values))
         if self.display_progress_div:
             w(u'<div id="progress">%s</div>' % self._cw._('validating...'))
@@ -120,7 +118,6 @@
         errormsg = self.error_message(form)
         if errormsg:
             data.insert(0, errormsg)
-        return '\n'.join(data)
 
     def render_label(self, form, field):
         if field.label is None:
@@ -179,29 +176,29 @@
 
     def open_form(self, form, values):
         if form.needs_multipart:
-            enctype = 'multipart/form-data'
+            enctype = u'multipart/form-data'
         else:
-            enctype = 'application/x-www-form-urlencoded'
-        tag = ('<form action="%s" method="post" enctype="%s"' % (
+            enctype = u'application/x-www-form-urlencoded'
+        tag = (u'<form action="%s" method="post" enctype="%s"' % (
             xml_escape(form.form_action() or '#'), enctype))
         if form.domid:
-            tag += ' id="%s"' % form.domid
+            tag += u' id="%s"' % form.domid
         if form.onsubmit:
-            tag += ' onsubmit="%s"' % xml_escape(form.onsubmit % dictattr(form))
+            tag += u' onsubmit="%s"' % xml_escape(form.onsubmit % dictattr(form))
         if form.cssstyle:
-            tag += ' style="%s"' % xml_escape(form.cssstyle)
+            tag += u' style="%s"' % xml_escape(form.cssstyle)
         if form.cssclass:
-            tag += ' class="%s"' % xml_escape(form.cssclass)
+            tag += u' class="%s"' % xml_escape(form.cssclass)
         if form.cwtarget:
-            tag += ' cubicweb:target="%s"' % xml_escape(form.cwtarget)
-        return tag + '>'
+            tag += u' cubicweb:target="%s"' % xml_escape(form.cwtarget)
+        return tag + u'>'
 
     def close_form(self, form, values):
         """seems dumb but important for consistency w/ close form, and necessary
         for form renderers overriding open_form to use something else or more than
         and <form>
         """
-        return '</form>'
+        return u'</form>'
 
     def render_fields(self, w, form, values):
         fields = self._render_hidden_fields(w, form)
@@ -244,9 +241,9 @@
                 w(u'<tr class="%s_%s_row">' % (field.name, field.role))
                 if self.display_label and field.label is not None:
                     w(u'<th class="labelCol">%s</th>' % self.render_label(form, field))
-                w('<td')
+                w(u'<td')
                 if field.label is None:
-                    w(' colspan="2"')
+                    w(u' colspan="2"')
                 error = form.field_error(field)
                 if error:
                     w(u' class="error"')
@@ -448,10 +445,8 @@
     """
     __regid__ = 'inline'
 
-    def render(self, form, values):
+    def render(self, w, form, values):
         form.add_media()
-        data = []
-        w = data.append
         try:
             w(u'<div id="div-%(divid)s" onclick="%(divonclick)s">' % values)
         except KeyError:
@@ -477,7 +472,6 @@
             values.pop(key, None)
         self.render_fields(w, form, values)
         w(u'</div></div>')
-        return '\n'.join(data)
 
     def render_fields(self, w, form, values):
         w(u'<fieldset id="fs-%(divid)s">' % values)
--- a/web/views/forms.py	Tue Oct 12 12:12:05 2010 +0200
+++ b/web/views/forms.py	Tue Oct 12 14:10:58 2010 +0200
@@ -168,10 +168,20 @@
             warn('[3.6] rendervalues argument is deprecated, all named arguments will be given instead',
                  DeprecationWarning, stacklevel=2)
             kwargs = rendervalues
+        w = kwargs.pop('w', None)
+        if w is None:
+            warn('[3.10] you should specify "w" to form.render() named arguments',
+                 DeprecationWarning, stacklevel=2)
+            data = []
+            w = data.append
+        else:
+            data = None
         self.build_context(formvalues)
         if renderer is None:
             renderer = self.default_renderer()
-        return renderer.render(self, kwargs)
+        renderer.render(w, self, kwargs)
+        if data is not None:
+            return '\n'.join(data)
 
     def default_renderer(self):
         return self._cw.vreg['formrenderers'].select(
--- a/web/views/management.py	Tue Oct 12 12:12:05 2010 +0200
+++ b/web/views/management.py	Tue Oct 12 14:10:58 2010 +0200
@@ -84,7 +84,7 @@
                                          __redirectpath=entity.rest_path())
         field = guess_field(entity.e_schema, self._cw.vreg.schema.rschema('owned_by'))
         form.append_field(field)
-        self.w(form.render(display_progress_div=False))
+        form.render(w=self.w, display_progress_div=False)
 
     def owned_by_information(self, entity):
         ownersrset = entity.related('owned_by')
@@ -154,7 +154,7 @@
         form.append_field(field)
         renderer = self._cw.vreg['formrenderers'].select(
             'htable', self._cw, rset=None, display_progress_div=False)
-        self.w(form.render(renderer=renderer))
+        form.render(w=self.w, renderer=renderer)
 
 
 class ErrorView(AnyRsetView):
@@ -217,7 +217,7 @@
             form.add_hidden('__bugreporting', '1')
             form.form_buttons = [wdgs.SubmitButton(MAIL_SUBMIT_MSGID)]
             form.action = req.build_url('reportbug')
-            w(form.render())
+            form.render(w=w)
 
 
 def exc_message(ex, encoding):