merge stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 23 Feb 2010 12:52:57 +0100
branchstable
changeset 4663 ebbe62209f4e
parent 4662 79c0788ba7f6 (diff)
parent 4657 79005a48b221 (current diff)
child 4664 bd5ecd5b9494
merge
--- a/web/formfields.py	Tue Feb 23 12:43:16 2010 +0100
+++ b/web/formfields.py	Tue Feb 23 12:52:57 2010 +0100
@@ -91,7 +91,10 @@
        optional fieldset to which this field belongs to
     :order:
        key used by automatic forms to sort fields
-
+    :ignore_req_params:
+       when true, this field won't consider value potentialy specified using
+       request's form parameters (eg you won't be able to specify a value using for
+       instance url like http://mywebsite.com/form?field=value)
     """
     # default widget associated to this class of fields. May be overriden per
     # instance
@@ -113,6 +116,7 @@
     order = None
     value = _MARKER
     fallback_on_none_attribute = False
+    ignore_req_params = False
 
     def __init__(self, name=None, label=_MARKER, widget=None, **kwargs):
         for key, val in kwargs.items():
@@ -348,9 +352,9 @@
     def process_form_value(self, form):
         """process posted form and return correctly typed value"""
         try:
-            return form.formvalues[self]
+            return form.formvalues[(self, form)]
         except KeyError:
-            value = form.formvalues[self] = self._process_form_value(form)
+            value = form.formvalues[(self, form)] = self._process_form_value(form)
             return value
 
     def _process_form_value(self, form):
@@ -596,7 +600,7 @@
             if data:
                 encoding = self.encoding(form)
                 try:
-                    form.formvalues[self] = unicode(data.getvalue(), encoding)
+                    form.formvalues[(self, form)] = unicode(data.getvalue(), encoding)
                 except UnicodeError:
                     pass
                 else:
@@ -816,7 +820,7 @@
             for field in form.root_form.fields_by_name('__linkto'):
                 if field.value in searchedvalues:
                     form.root_form.remove_field(field)
-            form.formvalues[self] = value
+            form.formvalues[(self, form)] = value
 
     def format_single_value(self, req, value):
         return value
@@ -824,13 +828,13 @@
     def process_form_value(self, form):
         """process posted form and return correctly typed value"""
         try:
-            return form.formvalues[self]
+            return form.formvalues[(self, form)]
         except KeyError:
             value = self._process_form_value(form)
             # if value is None, there are some remaining pending fields, we'll
             # have to recompute this later -> don't cache in formvalues
             if value is not None:
-                form.formvalues[self] = value
+                form.formvalues[(self, form)] = value
             return value
 
     def _process_form_value(self, form):
--- a/web/formwidgets.py	Tue Feb 23 12:43:16 2010 +0100
+++ b/web/formwidgets.py	Tue Feb 23 12:52:57 2010 +0100
@@ -59,21 +59,6 @@
     def _render(self, form, field, renderer):
         raise NotImplementedError()
 
-    def typed_value(self, form, field):
-        """return field's *typed* value specified in:
-        3. extra form values given to render()
-        4. field's typed value
-        """
-        qname = field.input_name(form)
-        for key in (field, qname):
-            try:
-                return form.formvalues[key]
-            except KeyError:
-                continue
-        if field.name != qname and field.name in form.formvalues:
-            return form.formvalues[field.name]
-        return field.typed_value(form)
-
     def format_value(self, form, field, value):
         return field.format_value(form._cw, value)
 
@@ -97,15 +82,20 @@
         return self.values(form, field), attrs
 
     def values(self, form, field):
-        qname = field.input_name(form, self.suffix)
-        if qname in form.form_previous_values:
-            values = form.form_previous_values[qname]
-        elif qname in form._cw.form:
-            values = form._cw.form[qname]
-        elif field.name != qname and field.name in form._cw.form:
-            # compat: accept attr=value in req.form to specify value of attr-subject
-            values = form._cw.form[field.name]
-        else:
+        values = None
+        if not field.ignore_req_params:
+            qname = field.input_name(form, self.suffix)
+            # value from a previous post that has raised a validation error
+            if qname in form.form_previous_values:
+                values = form.form_previous_values[qname]
+            # value specified using form parameters
+            elif qname in form._cw.form:
+                values = form._cw.form[qname]
+            elif field.name != qname and field.name in form._cw.form:
+                # XXX compat: accept attr=value in req.form to specify value of
+                # attr-subject
+                values = form._cw.form[field.name]
+        if values is None:
             values = self.typed_value(form, field)
             if values != INTERNAL_FIELD_VALUE:
                 values = self.format_value(form, field, values)
@@ -113,6 +103,21 @@
             values = (values,)
         return values
 
+    def typed_value(self, form, field):
+        """return field's *typed* value specified in:
+        3. extra form values given to render()
+        4. field's typed value
+        """
+        qname = field.input_name(form)
+        for key in ((field, form), qname):
+            try:
+                return form.formvalues[key]
+            except KeyError:
+                continue
+        if field.name != qname and field.name in form.formvalues:
+            return form.formvalues[field.name]
+        return field.typed_value(form)
+
     def process_field_data(self, form, field):
         posted = form._cw.form
         val = posted.get(field.input_name(form, self.suffix))
--- a/web/views/forms.py	Tue Feb 23 12:43:16 2010 +0100
+++ b/web/views/forms.py	Tue Feb 23 12:52:57 2010 +0100
@@ -113,6 +113,7 @@
 
     def add_hidden(self, name, value=None, **kwargs):
         """add an hidden field to the form"""
+        kwargs.setdefault('ignore_req_params', True)
         kwargs.setdefault('widget', fwdgs.HiddenInput)
         field = StringField(name=name, value=value, **kwargs)
         if 'id' in kwargs:
@@ -178,6 +179,7 @@
             renderer = self.default_renderer()
         return renderer.render(self, values)
 
+
 _AFF = uicfg.autoform_field
 _AFF_KWARGS = uicfg.autoform_field_kwargs
 
@@ -248,6 +250,8 @@
         # entity primary view
         if self._cw.json_request and self.edited_entity.has_eid():
             return '%s#%s' % (self.edited_entity.absolute_url(), self.domid)
+        # XXX we should not consider some url parameters that may lead to
+        # different url after a validation error
         return '%s#%s' % (self._cw.url(), self.domid)
 
     def build_context(self, formvalues=None):
@@ -258,8 +262,7 @@
         for field in self.fields:
             if field.eidparam:
                 edited.add(field.role_name())
-        self.add_hidden('_cw_edited_fields', u','.join(edited),
-                        eidparam=True)
+        self.add_hidden('_cw_edited_fields', u','.join(edited), eidparam=True)
 
     def default_renderer(self):
         return self._cw.vreg['formrenderers'].select(