refactor session_key (__errorurl) to handle cases where the form is generated throuhg an ajax call: in case of an entity form, return the entity's absolute url. Also allow to force session key value stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 27 Nov 2009 09:14:31 +0100
branchstable
changeset 3922 69020a7c234a
parent 3921 4f24b6ef3da9
child 3923 be05eeeb2db1
refactor session_key (__errorurl) to handle cases where the form is generated throuhg an ajax call: in case of an entity form, return the entity's absolute url. Also allow to force session key value
web/form.py
web/views/forms.py
--- a/web/form.py	Fri Nov 27 09:10:40 2009 +0100
+++ b/web/form.py	Fri Nov 27 09:14:31 2009 +0100
@@ -42,16 +42,20 @@
     """abstract form mix-in
     XXX: you should inherit from this FIRST (obscure pb with super call)
     """
+    force_session_key = None
 
     def session_key(self):
         """return the key that may be used to store / retreive data about a
         previous post which failed because of a validation error
         """
-        return '%s#%s' % (self.req.url(), self.domid)
 
     def __init__(self, req, rset, **kwargs):
         super(FormMixIn, self).__init__(req, rset, **kwargs)
         self.restore_previous_post(self.session_key())
+        try:
+            return self.force_session_key
+        except AttributeError:
+            return '%s#%s' % (self.req.url(), self.domid)
 
     def restore_previous_post(self, sessionkey):
         # get validation session data which may have been previously set.
--- a/web/views/forms.py	Fri Nov 27 09:10:40 2009 +0100
+++ b/web/views/forms.py	Fri Nov 27 09:14:31 2009 +0100
@@ -276,6 +276,19 @@
         if msg:
             self.form_add_hidden('__message', msg)
 
+    def session_key(self):
+        """return the key that may be used to store / retreive data about a
+        previous post which failed because of a validation error
+        """
+        try:
+            return self.force_session_key
+        except AttributeError:
+            # XXX if this is a json request, suppose we should redirect to the
+            # entity primary view
+            if self.req.json_request:
+                return '%s#%s' % (self.edited_entity.absolute_url(), self.domid)
+            return '%s#%s' % (self.req.url(), self.domid)
+
     def _field_has_error(self, field):
         """return true if the field has some error in given validation exception
         """