[forms] deprecate form_render(**kwargs) in favor of render(formvalues=None, rendervalues=None, renderer=None) stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 01 Dec 2009 08:43:37 +0100
branchstable
changeset 3953 19aefd78f61b
parent 3952 fbd77bda27c1
child 3954 56a8b14d68aa
[forms] deprecate form_render(**kwargs) in favor of render(formvalues=None, rendervalues=None, renderer=None) to fix a pitfall in form_render prototype: there is no way to distinguish between arguments that should be given to build_context from arguments that should be given to the renderer. This was problematic for instance with inlined form where a 'title' argument is given for the renderer, but is also used as default value for field whose name is 'title'...
web/test/unittest_form.py
web/views/cwproperties.py
web/views/editforms.py
web/views/forms.py
web/views/management.py
web/views/massmailing.py
web/views/sparql.py
web/views/workflow.py
--- a/web/test/unittest_form.py	Tue Dec 01 08:38:02 2009 +0100
+++ b/web/test/unittest_form.py	Tue Dec 01 08:43:37 2009 +0100
@@ -95,7 +95,7 @@
         self.req.form['__linkto'] = 'in_group:%s:subject' % geid
         form = self.vreg['forms'].select('edition', self.req, entity=e)
         form.content_type = 'text/html'
-        pageinfo = self._check_html(form.form_render(), form, template=None)
+        pageinfo = self._check_html(form.render(), form, template=None)
         inputs = pageinfo.find_tag('select', False)
         self.failUnless(any(attrs for t, attrs in inputs if attrs.get('name') == 'in_group:A'))
         inputs = pageinfo.find_tag('input', False)
@@ -126,14 +126,14 @@
             creation_date = DateTimeField(widget=DateTimePicker)
         form = CustomChangeStateForm(self.req, redirect_path='perdu.com',
                                      entity=self.entity)
-        form.form_render(state=123, trcomment=u'',
-                         trcomment_format=u'text/plain')
+        form.render(formvalues=dict(state=123, trcomment=u'',
+                                    trcomment_format=u'text/plain'))
 
     def test_change_state_form(self):
         form = ChangeStateForm(self.req, redirect_path='perdu.com',
                                entity=self.entity)
-        form.form_render(state=123, trcomment=u'',
-                         trcomment_format=u'text/plain')
+        form.render(formvalues=dict(state=123, trcomment=u'',
+                                    trcomment_format=u'text/plain'))
 
     # fields tests ############################################################
 
--- a/web/views/cwproperties.py	Tue Dec 01 08:38:02 2009 +0100
+++ b/web/views/cwproperties.py	Tue Dec 01 08:43:37 2009 +0100
@@ -202,7 +202,7 @@
             self.form_row(form, key, splitlabel)
         renderer = self.vreg['formrenderers'].select('cwproperties', self.req,
                                                      display_progress_div=False)
-        return form.form_render(renderer=renderer)
+        return form.render(renderer=renderer)
 
     def form_row(self, form, key, splitlabel):
         entity = self.entity_for_key(key)
--- a/web/views/editforms.py	Tue Dec 01 08:38:02 2009 +0100
+++ b/web/views/editforms.py	Tue Dec 01 08:43:37 2009 +0100
@@ -94,7 +94,7 @@
             w(u'<li>%s</li>' % tags.a(entity.view('textoutofcontext'),
                                       href=entity.absolute_url()))
         w(u'</ul>\n')
-        w(form.form_render())
+        w(form.render())
 
 
 class ClickAndEditFormView(FormViewMixIn, EntityView):
@@ -203,7 +203,7 @@
           u'onmouseover="removeElementClass(jQuery(\'#%s\'), \'hidden\')">'
           % (divid, divid, divid))
         w(u'<div id="%s-value" class="editableFieldValue">%s</div>' % (divid, value))
-        w(form.form_render(renderer=renderer))
+        w(form.render(renderer=renderer))
         w(u'<div id="%s" class="editableField hidden" onclick="%s" title="%s">' % (
                 divid, xml_escape(self._onclick % form.event_args),
                 self.req._(self._landingzonemsg)))
@@ -257,6 +257,8 @@
     __slots__ = ('event_args',)
     def form_render(self, **_args):
         return u''
+    def render(self, **_args):
+        return u''
     def append_field(self, *args):
         pass
 
@@ -320,7 +322,7 @@
                                          row=entity.row, col=entity.col, entity=entity,
                                          submitmsg=self.submited_message())
         self.init_form(form, entity)
-        self.w(form.form_render(formvid=u'edition'))
+        self.w(form.render(rendervalues=dict(formvid=u'edition')))
 
     def init_form(self, form, entity):
         """customize your form before rendering here"""
@@ -458,7 +460,7 @@
         #self.form_title(entity)
         form = self.vreg['forms'].select(self.id, self.req, rset=self.rset,
                                          copy_nav_params=True)
-        self.w(form.form_render())
+        self.w(form.render())
 
 
 class InlineEntityEditionFormView(FormViewMixIn, EntityView):
@@ -523,9 +525,11 @@
             self.req.data[countkey] += 1
         except KeyError:
             self.req.data[countkey] = 1
-        self.w(self.form.form_render(
-            divid=divid, title=title, removejs=removejs, i18nctx=i18nctx,
-            counter=self.req.data[countkey], **kwargs))
+        # XXX split kwargs into additional rendervalues / formvalues
+        self.w(self.form.render(
+            rendervalues=dict(divid=divid, title=title, removejs=removejs,
+                              i18nctx=i18nctx, counter=self.req.data[countkey]),
+            formvalues=kwargs))
 
     def form_title(self, entity, i18nctx):
         return self.req.pgettext(i18nctx, 'This %s' % entity.e_schema)
--- a/web/views/forms.py	Tue Dec 01 08:38:02 2009 +0100
+++ b/web/views/forms.py	Tue Dec 01 08:43:37 2009 +0100
@@ -10,6 +10,7 @@
 from warnings import warn
 
 from logilab.common.compat import any
+from logilab.common.deprecation import deprecated
 
 from cubicweb.selectors import non_final_entity, match_kwargs, one_line_rset
 from cubicweb.web import INTERNAL_FIELD_VALUE, eid_param
@@ -127,15 +128,14 @@
         if self.needs_css:
             self.req.add_css(self.needs_css)
 
-    def form_render(self, **values):
+    def render(self, formvalues=None, rendervalues=None, renderer=None):
         """render this form, using the renderer given in args or the default
         FormRenderer()
         """
-        self.build_context(values)
-        renderer = values.pop('renderer', None)
+        self.build_context(formvalues or {})
         if renderer is None:
             renderer = self.form_default_renderer()
-        return renderer.render(self, values)
+        return renderer.render(self, rendervalues or {})
 
     def form_default_renderer(self):
         return self.vreg['formrenderers'].select(self.form_renderer_id,
@@ -148,8 +148,8 @@
         containing field 'name' (qualified), 'id', 'value' (for display, always
         a string).
 
-        rendervalues is an optional dictionary containing extra kwargs given to
-        form_render()
+        rendervalues is an optional dictionary containing extra form values
+        given to render()
         """
         if self.context is not None:
             return # already built
@@ -248,6 +248,17 @@
         """
         return self.form_valerror and field.name in self.form_valerror.errors
 
+    @deprecated('use .render(formvalues, rendervalues)')
+    def form_render(self, **values):
+        """render this form, using the renderer given in args or the default
+        FormRenderer()
+        """
+        self.build_context(values)
+        renderer = values.pop('renderer', None)
+        if renderer is None:
+            renderer = self.form_default_renderer()
+        return renderer.render(self, values)
+
 
 class EntityFieldsForm(FieldsForm):
     id = 'base'
@@ -442,6 +453,8 @@
             return eid_param(field.id, self.edited_entity.eid)
         return field.id
 
+    # XXX all this vocabulary handling should be on the field, no?
+
     def form_field_vocabulary(self, field, limit=None):
         """return vocabulary for the given field"""
         role, rtype = field.role, field.name
@@ -464,7 +477,6 @@
         #       cases, it doesn't make sense to sort results afterwards.
         return vocabfunc(rtype, limit)
 
-    # XXX should be on the field, no?
     def subject_relation_vocabulary(self, rtype, limit=None):
         """defaut vocabulary method for the given relation, looking for
         relation's object entities (i.e. self is the subject)
--- a/web/views/management.py	Tue Dec 01 08:38:02 2009 +0100
+++ b/web/views/management.py	Tue Dec 01 08:43:37 2009 +0100
@@ -115,7 +115,7 @@
                                          __redirectpath=entity.rest_path())
         field = guess_field(entity.e_schema, self.schema.rschema('owned_by'))
         form.append_field(field)
-        self.w(form.form_render(display_progress_div=False))
+        self.w(form.render(rendervalues=dict(display_progress_div=False)))
 
     def owned_by_information(self, entity):
         ownersrset = entity.related('owned_by')
@@ -185,7 +185,7 @@
         form.append_field(field)
         renderer = self.vreg['formrenderers'].select(
             'htable', self.req, rset=None, display_progress_div=False)
-        self.w(form.form_render(renderer=renderer))
+        self.w(form.render(renderer=renderer))
 
 
 class ErrorView(AnyRsetView):
@@ -248,7 +248,7 @@
             form.form_add_hidden('__bugreporting', '1')
             form.form_buttons = [wdgs.SubmitButton(MAIL_SUBMIT_MSGID)]
             form.action = req.build_url('reportbug')
-            w(form.form_render())
+            w(form.render())
 
 
 def exc_message(ex, encoding):
--- a/web/views/massmailing.py	Tue Dec 01 08:38:02 2009 +0100
+++ b/web/views/massmailing.py	Tue Dec 01 08:43:37 2009 +0100
@@ -127,4 +127,4 @@
         from_addr = '%s <%s>' % (req.user.dc_title(), req.user.get_email())
         form = self.vreg['forms'].select('massmailing', self.req, rset=self.rset,
                                 action='sendmail', domid='sendmail')
-        self.w(form.form_render(sender=from_addr))
+        self.w(form.render(formvalues=dict(sender=from_addr)))
--- a/web/views/sparql.py	Tue Dec 01 08:38:02 2009 +0100
+++ b/web/views/sparql.py	Tue Dec 01 08:43:37 2009 +0100
@@ -39,7 +39,7 @@
     id = 'sparql'
     def call(self):
         form = self.vreg.select('forms', 'sparql', self.req)
-        self.w(form.form_render())
+        self.w(form.render())
         sparql = self.req.form.get('sparql')
         vid = self.req.form.get('resultvid', 'table')
         if sparql:
--- a/web/views/workflow.py	Tue Dec 01 08:38:02 2009 +0100
+++ b/web/views/workflow.py	Tue Dec 01 08:43:37 2009 +0100
@@ -53,8 +53,8 @@
             'st1': entity.printable_state,
             'st2': self.req._(transition.destination().name)}
         self.w(u'<p>%s</p>\n' % msg)
-        self.w(form.form_render(wf_info_for=entity.eid,
-                                by_transition=transition.eid))
+        self.w(form.render(formvalues=dict(wf_info_for=entity.eid,
+                                           by_transition=transition.eid)))
 
     def redirectpath(self, entity):
         return entity.rest_path()