rename form_build_context to build_context, and call it from form, not renderer stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 29 Sep 2009 12:37:47 +0200
branchstable
changeset 3510 bf746bf4a394
parent 3506 f0ec5d17f470
child 3511 581de819106f
rename form_build_context to build_context, and call it from form, not renderer
web/formfields.py
web/test/unittest_form.py
web/views/formrenderers.py
web/views/forms.py
--- a/web/formfields.py	Mon Sep 28 18:58:12 2009 +0200
+++ b/web/formfields.py	Tue Sep 29 12:37:47 2009 +0200
@@ -209,7 +209,7 @@
         return vocab
 
     def form_init(self, form):
-        """method called before by form_build_context to trigger potential field
+        """method called before by build_context to trigger potential field
         initialization requiring the form instance
         """
         pass
--- a/web/test/unittest_form.py	Mon Sep 28 18:58:12 2009 +0200
+++ b/web/test/unittest_form.py	Tue Sep 29 12:37:47 2009 +0200
@@ -82,7 +82,7 @@
         form = EntityFieldsForm(self.request(login=u'toto'), None, entity=e)
         field = StringField(name='login', eidparam=True)
         form.append_field(field)
-        form.form_build_context({})
+        form.build_context({})
         self.assertEquals(form.form_field_display_value(field, {}), 'toto')
 
 
@@ -137,7 +137,7 @@
     # fields tests ############################################################
 
     def _render_entity_field(self, name, form):
-        form.form_build_context({})
+        form.build_context({})
         renderer = FormRenderer(self.req)
         return form.field_by_name(name).render(form, renderer)
 
--- a/web/views/formrenderers.py	Mon Sep 28 18:58:12 2009 +0200
+++ b/web/views/formrenderers.py	Tue Sep 29 12:37:47 2009 +0200
@@ -171,7 +171,6 @@
                 or (field.name, field.role) in form.internal_fields)
 
     def render_fields(self, w, form, values):
-        form.form_build_context(values)
         fields = self._render_hidden_fields(w, form)
         if fields:
             self._render_fields(fields, w, form)
@@ -556,7 +555,6 @@
         return '\n'.join(data)
 
     def render_fields(self, w, form, values):
-        form.form_build_context(values)
         w(u'<fieldset id="fs-%(divid)s">' % values)
         fields = self._render_hidden_fields(w, form)
         w(u'</fieldset>')
--- a/web/views/forms.py	Mon Sep 28 18:58:12 2009 +0200
+++ b/web/views/forms.py	Tue Sep 29 12:37:47 2009 +0200
@@ -174,6 +174,7 @@
         """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()
@@ -184,7 +185,7 @@
                                                 self.req, rset=self.rset,
                                                 row=self.row, col=self.col)
 
-    def form_build_context(self, rendervalues=None):
+    def build_context(self, rendervalues=None):
         """build form context values (the .context attribute which is a
         dictionary with field instance as key associated to a dictionary
         containing field 'name' (qualified), 'id', 'value' (for display, always
@@ -193,6 +194,8 @@
         rendervalues is an optional dictionary containing extra kwargs given to
         form_render()
         """
+        if self.context is not None:
+            return # already built
         self.context = context = {}
         # ensure rendervalues is a dict
         if rendervalues is None:
@@ -372,7 +375,7 @@
             self.form_renderer_id, self.req, rset=self.rset, row=self.row,
             col=self.col, entity=self.edited_entity)
 
-    def form_build_context(self, values=None):
+    def build_context(self, values=None):
         """overriden to add edit[s|o] hidden fields and to ensure schema fields
         have eidparam set to True
 
@@ -380,6 +383,8 @@
         associated field before the (potential) modification made when
         submitting the form.
         """
+        if self.context is not None:
+            return
         eschema = self.edited_entity.e_schema
         for field in self.fields[:]:
             for field in field.actual_fields(self):
@@ -389,7 +394,7 @@
                      eschema.has_object_relation(fieldname))):
                     field.eidparam = True
                     self.fields.append(HiddenInitialValueField(field))
-        return super(EntityFieldsForm, self).form_build_context(values)
+        return super(EntityFieldsForm, self).build_context(values)
 
     def form_field_value(self, field, load_bytes=False):
         """return field's *typed* value
@@ -539,13 +544,13 @@
         return False
 
 
-class CompositeForm(FieldsForm):
+class CompositeFormMixIn(object):
     """form composed of sub-forms"""
     id = 'composite'
     form_renderer_id = id
 
     def __init__(self, *args, **kwargs):
-        super(CompositeForm, self).__init__(*args, **kwargs)
+        super(CompositeFormMixIn, self).__init__(*args, **kwargs)
         self.forms = []
 
     def form_add_subform(self, subform):
@@ -553,17 +558,14 @@
         subform.is_subform = True
         self.forms.append(subform)
 
+    def build_context(self, rendervalues=None):
+        super(CompositeFormMixIn, self).build_context(rendervalues)
+        for form in self.forms:
+            form.build_context(rendervalues)
 
-class CompositeEntityForm(EntityFieldsForm):
-    """form composed of sub-forms"""
-    id = 'composite'
-    form_renderer_id = id
 
-    def __init__(self, *args, **kwargs):
-        super(CompositeEntityForm, self).__init__(*args, **kwargs)
-        self.forms = []
+class CompositeForm(CompositeFormMixIn, FieldsForm):
+    pass
 
-    def form_add_subform(self, subform):
-        """mark given form as a subform and append it"""
-        subform.is_subform = True
-        self.forms.append(subform)
+class CompositeEntityForm(CompositeFormMixIn, EntityFieldsForm):
+    pass # XXX why is this class necessary?