[views] remove breadcrumbs usage to retrieve last visited page
authorRabah Meradi <rabah.meradi@logilab.fr>
Wed, 25 Nov 2015 18:31:48 +0100
changeset 10932 cb217b2b3463
parent 10931 6168a45f62df
child 10933 830f1ea52789
[views] remove breadcrumbs usage to retrieve last visited page Use JavaScript history.back to go to the last visited page. Rename _return_to_lastpage to _redirect as it becomes misnamed. Drop `test_redirect_default` test in `unittest_views_basecontrollers` which essentially tested this (now removed) breadcrumbs "feature". Closes #5456850.
web/controller.py
web/data/cubicweb.edition.js
web/request.py
web/test/unittest_views_basecontrollers.py
web/views/autoform.py
web/views/basecontrollers.py
web/views/editcontroller.py
--- a/web/controller.py	Wed Nov 25 16:22:42 2015 +0100
+++ b/web/controller.py	Wed Nov 25 18:31:48 2015 +0100
@@ -134,8 +134,6 @@
             newparams['_cwmsgid'] = self._cw.set_redirect_message(msg)
         if '__action_apply' in self._cw.form:
             self._return_to_edition_view(newparams)
-        if '__action_cancel' in self._cw.form:
-            self._return_to_lastpage(newparams)
         else:
             self._return_to_original_view(newparams)
 
@@ -203,11 +201,9 @@
         raise Redirect(self._cw.build_url(path, **newparams))
 
 
-    def _return_to_lastpage(self, newparams):
-        """cancel-button case: in this case we are always expecting to go back
-        where we came from, and this is not easy. Currently we suppose that
-        __redirectpath is specifying that place if found, else we look in the
-        request breadcrumbs for the last visited page.
+    def _redirect(self, newparams):
+        """Raise a redirect. We use __redirectpath if it specified, else we
+        return to the home page.
         """
         if '__redirectpath' in self._cw.form:
             # if redirect path was explicitly specified in the form, use it
@@ -215,7 +211,7 @@
             url = self._cw.build_url(path)
             url = append_url_params(url, self._cw.form.get('__redirectparams'))
         else:
-            url = self._cw.last_visited_page()
+            url = self._cw.base_url()
         # The newparams must update the params in all cases
         url = self._cw.rebuild_url(url, **newparams)
         raise Redirect(url)
--- a/web/data/cubicweb.edition.js	Wed Nov 25 16:22:42 2015 +0100
+++ b/web/data/cubicweb.edition.js	Wed Nov 25 18:31:48 2015 +0100
@@ -537,6 +537,24 @@
 }
 
 /**
+ * Cancel the operations done on the given form.
+ *
+ */
+$(function () {
+    $(document).on('click', '.cwjs-edition-cancel', function (evt) {
+        var $mynode = $(evt.currentTarget),
+            $form = $mynode.closest('form'),
+            $error = $form.find(':input[name="__errorurl"]'),
+            errorurl = $error.attr('value'),
+            args = ajaxFuncArgs('cancel_edition', null, errorurl);
+        loadRemote(AJAX_BASE_URL, args, 'POST', true);
+        history.back();
+        return false;
+    });
+});
+
+
+/**
  * .. function:: validateForm(formid, action, onsuccess, onfailure)
  *
  * called on traditionnal form submission : the idea is to try
--- a/web/request.py	Wed Nov 25 16:22:42 2015 +0100
+++ b/web/request.py	Wed Nov 25 18:31:48 2015 +0100
@@ -42,7 +42,7 @@
 from cubicweb import AuthenticationError
 from cubicweb.req import RequestSessionBase
 from cubicweb.uilib import remove_html_tags, js
-from cubicweb.utils import SizeConstrainedList, HTMLHead, make_uid
+from cubicweb.utils import HTMLHead, make_uid
 from cubicweb.view import TRANSITIONAL_DOCTYPE_NOEXT
 from cubicweb.web import (INTERNAL_FIELD_VALUE, LOGGER, NothingToEdit,
                           RequestError, StatusResponse)
@@ -398,26 +398,6 @@
                 return False
         return True
 
-    def update_breadcrumbs(self):
-        """stores the last visisted page in session data"""
-        searchstate = self.search_state[0]
-        if searchstate == 'normal':
-            breadcrumbs = self.session.data.get('breadcrumbs')
-            if breadcrumbs is None:
-                breadcrumbs = SizeConstrainedList(10)
-                self.session.data['breadcrumbs'] = breadcrumbs
-                breadcrumbs.append(self.url())
-            else:
-                url = self.url()
-                if breadcrumbs and breadcrumbs[-1] != url:
-                    breadcrumbs.append(url)
-
-    def last_visited_page(self):
-        breadcrumbs = self.session.data.get('breadcrumbs')
-        if breadcrumbs:
-            return breadcrumbs.pop()
-        return self.base_url()
-
     # web edition helpers #####################################################
 
     @cached # so it's writed only once
--- a/web/test/unittest_views_basecontrollers.py	Wed Nov 25 16:22:42 2015 +0100
+++ b/web/test/unittest_views_basecontrollers.py	Wed Nov 25 18:31:48 2015 +0100
@@ -1054,17 +1054,6 @@
                 result = controller.publish(rset=None)
             self.assertURLPath(cm.exception.location, rpath)
 
-    def test_redirect_default(self):
-        with self.admin_access.web_request() as req:
-            txuuid = self.txuuid_toto_email
-            req.form['txuuid'] = txuuid
-            req.session.data['breadcrumbs'] = [ urljoin(req.base_url(), path)
-                                                for path in ('tata', 'toto',)]
-            controller = self.vreg['controllers'].select('undo', req)
-            with self.assertRaises(Redirect) as cm:
-                result = controller.publish(rset=None)
-            self.assertURLPath(cm.exception.location, 'toto')
-
 
 class LoginControllerTC(CubicWebTC):
 
--- a/web/views/autoform.py	Wed Nov 25 16:22:42 2015 +0100
+++ b/web/views/autoform.py	Wed Nov 25 18:31:48 2015 +0100
@@ -742,7 +742,8 @@
     copy_nav_params = True
     form_buttons = [fw.SubmitButton(),
                     fw.Button(stdmsgs.BUTTON_APPLY, cwaction='apply'),
-                    fw.Button(stdmsgs.BUTTON_CANCEL, cwaction='cancel')]
+                    fw.Button(stdmsgs.BUTTON_CANCEL,
+                              {'class': fw.Button.css_class + ' cwjs-edition-cancel'})]
     # for attributes selection when searching in uicfg.autoform_section
     formtype = 'main'
     # set this to a list of [(relation, role)] if you want to explictily tell
--- a/web/views/basecontrollers.py	Wed Nov 25 16:22:42 2015 +0100
+++ b/web/views/basecontrollers.py	Wed Nov 25 18:31:48 2015 +0100
@@ -126,7 +126,6 @@
     def publish(self, rset=None):
         """publish a request, returning an encoded string"""
         view, rset = self._select_view_and_rset(rset)
-        self.add_to_breadcrumbs(view)
         view.set_http_cache_headers()
         if self._cw.is_client_cache_valid():
             return ''
@@ -160,13 +159,6 @@
             view = req.vreg['views'].select(vid, req, rset=rset)
         return view, rset
 
-    def add_to_breadcrumbs(self, view):
-        # update breadcrumbs **before** validating cache, unless the view
-        # specifies explicitly it should not be added to breadcrumb or the
-        # view is a binary view
-        if view.add_to_breadcrumbs and not view.binary:
-            self._cw.update_breadcrumbs()
-
     def execute_linkto(self, eid=None):
         """XXX __linkto parameter may cause security issue
 
@@ -307,4 +299,4 @@
     def redirect(self, msg=None):
         req = self._cw
         msg = msg or req._("transaction undone")
-        self._return_to_lastpage( dict(_cwmsgid= req.set_redirect_message(msg)) )
+        self._redirect({'_cwmsgid': req.set_redirect_message(msg)})
--- a/web/views/editcontroller.py	Wed Nov 25 16:22:42 2015 +0100
+++ b/web/views/editcontroller.py	Wed Nov 25 18:31:48 2015 +0100
@@ -390,13 +390,6 @@
         self._default_publish()
         self.reset()
 
-    def _action_cancel(self):
-        errorurl = self._cw.form.get('__errorurl')
-        if errorurl:
-            self._cw.cancel_edition(errorurl)
-        self._cw.set_message(self._cw._('edit canceled'))
-        return self.reset()
-
     def _action_delete(self):
         self.delete_entities(self._cw.edited_eids(withtype=True))
         return self.reset()