[forms] refactor action handling to ease overriding while keeping action overrideable by instance (closes #969167) stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 26 May 2010 12:31:43 +0200
branchstable
changeset 5588 377c9adfe81e
parent 5587 72679e450f6d
child 5589 25bf54dab82b
[forms] refactor action handling to ease overriding while keeping action overrideable by instance (closes #969167)
web/form.py
web/views/autoform.py
web/views/basetemplates.py
web/views/formrenderers.py
web/views/forms.py
--- a/web/form.py	Wed May 26 12:31:40 2010 +0200
+++ b/web/form.py	Wed May 26 12:31:43 2010 +0200
@@ -15,9 +15,7 @@
 #
 # You should have received a copy of the GNU Lesser General Public License along
 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
-"""abstract form classes for CubicWeb web client
-
-"""
+"""abstract form classes for CubicWeb web client"""
 __docformat__ = "restructuredtext en"
 
 from warnings import warn
--- a/web/views/autoform.py	Wed May 26 12:31:40 2010 +0200
+++ b/web/views/autoform.py	Wed May 26 12:31:43 2010 +0200
@@ -643,6 +643,8 @@
     # set this to a list of [(relation, role)] if you want to explictily tell
     # which relations should be edited
     display_fields = None
+    # action on the form tag
+    _default_form_action_path = 'validateform'
 
     @iclassmethod
     def field_by_name(cls_or_self, name, role=None, eschema=None):
@@ -713,21 +715,6 @@
             return None
         return self.maxrelitems + 1
 
-    def action(self):
-        """return the form's action attribute. Default to validateform if not
-        explicitly overriden.
-        """
-        try:
-            return self._action
-        except AttributeError:
-            return self._cw.build_url('validateform')
-
-    def set_action(self, value):
-        """override default action"""
-        self._action = value
-
-    action = property(action, set_action)
-
     # autoform specific fields #################################################
 
     def _generic_relations_field(self):
--- a/web/views/basetemplates.py	Wed May 26 12:31:40 2010 +0200
+++ b/web/views/basetemplates.py	Wed May 26 12:31:43 2010 +0200
@@ -446,9 +446,10 @@
     form_buttons = [fw.SubmitButton(label=_('log in'),
                                     attrs={'class': 'loginButton'})]
 
-    @property
-    def action(self):
-        return xml_escape(login_form_url(self._cw))
+    def form_action(self):
+        if self.action is None:
+            return login_form_url(self._cw)
+        return super(LogForm, self).form_action()
 
 
 class LogFormView(View):
--- a/web/views/formrenderers.py	Wed May 26 12:31:40 2010 +0200
+++ b/web/views/formrenderers.py	Wed May 26 12:31:43 2010 +0200
@@ -174,12 +174,8 @@
             enctype = 'multipart/form-data'
         else:
             enctype = 'application/x-www-form-urlencoded'
-        if form.action is None:
-            action = self._cw.build_url('edit')
-        else:
-            action = form.action
         tag = ('<form action="%s" method="post" enctype="%s"' % (
-            xml_escape(action or '#'), enctype))
+            xml_escape(form.form_action() or '#'), enctype))
         if form.domid:
             tag += ' id="%s"' % form.domid
         if form.onsubmit:
--- a/web/views/forms.py	Wed May 26 12:31:40 2010 +0200
+++ b/web/views/forms.py	Wed May 26 12:31:43 2010 +0200
@@ -194,6 +194,12 @@
             for field in field.actual_fields(self):
                 field.form_init(self)
 
+    _default_form_action_path = 'edit'
+    def form_action(self):
+        if self.action is None:
+            self._cw.build_url(self._default_form_action_path)
+        return self.action
+
     @deprecated('[3.6] use .add_hidden(name, value, **kwargs)')
     def form_add_hidden(self, name, value=None, **kwargs):
         return self.add_hidden(name, value, **kwargs)