[PATCH 2 of 2 saem_ref v2] Avoid 404 on /<etype> when there's no entity yet (CONSEJIRA-511)

Denis Laxalde denis.laxalde at logilab.fr
Wed Sep 19 11:58:05 CEST 2018


# HG changeset patch
# User Denis Laxalde <denis.laxalde at logilab.fr>
# Date 1537350009 -7200
#      Wed Sep 19 11:40:09 2018 +0200
# Node ID 40f49de273745dac84f6984d9fe0286437a8beb8
# Parent  330d7061dda4171e520e366f42a00938798ebac7
# Available At http://hg.logilab.org/review/cubes/saem_ref
#              hg pull http://hg.logilab.org/review/cubes/saem_ref -r 40f49de27374
# EXP-Topic CONSEJIRA-511
Avoid 404 on /<etype> when there's no entity yet (CONSEJIRA-511)

The default behavior of CubicWeb is to raise a NotFound exception when
the result set is empty on a /<etype> route. This is not very user
friendly (not even sure this is semantically correct). So we override
this behavior by selecting the "noresult" view in such cases through
RestPathEvaluator.set_vid_for_rset() (where the NotFound exception is
raised). Since we added a custom version of this view in the previous
changeset, the resulting page will contain an invite to create the first
entity of <etype>.

diff --git a/cubicweb_saem_ref/views/__init__.py b/cubicweb_saem_ref/views/__init__.py
--- a/cubicweb_saem_ref/views/__init__.py
+++ b/cubicweb_saem_ref/views/__init__.py
@@ -35,6 +35,7 @@ from cubicweb.web.views import (
     primary,
     tabs,
     uicfg,
+    urlpublishing,
     urlrewrite,
 )
 
@@ -465,13 +466,24 @@ class SAEMNoResultView(baseviews.NoResul
         self.w(u'</div>')
 
 
+class RestPathEvaluator(urlpublishing.RestPathEvaluator):
+
+    def set_vid_for_rset(self, req, cls, rset):
+        if rset.rowcount == 0:
+            req.form['vid'] = 'noresult'
+            return
+        return super(RestPathEvaluator, self).set_vid_for_rset(req, cls, rset)
+
+
 def registration_callback(vreg):
     from cubicweb.web.views import actions, cwuser, tableview, undohistory
     vreg.register_all(globals().values(), __name__, (
+        RestPathEvaluator,
         SAEMHTMLPageFooter,
         SAEMNoResultView,
         URLAttributeView,
     ))
+    vreg.register_and_replace(RestPathEvaluator, urlpublishing.RestPathEvaluator)
     vreg.register_and_replace(URLAttributeView, primary.URLAttributeView)
     vreg.register_and_replace(SAEMHTMLPageFooter, basetemplates.HTMLPageFooter)
     vreg.register_and_replace(SAEMNoResultView, baseviews.NoResultView)
diff --git a/test/test_views.py b/test/test_views.py
--- a/test/test_views.py
+++ b/test/test_views.py
@@ -648,6 +648,24 @@ class BaseViewsTC(CubicWebTC):
             result = self.app_handle_request(req, path='/sedalib')
             self.assertIn(self.expected_notfound_html('SEDAArchiveUnit'), result)
 
+    def test_no_notfound_when_no_entity(self):
+        """Check that we do not get 404 at /<etype> when no entity of <etype>
+        exists yet.
+        """
+        with self.admin_access.web_request() as req:
+            # Request /bookmark, usually there's none on a fresh instance.
+            result = self.app_handle_request(req, path='/bookmark')
+            self.assertIn(self.expected_notfound_html('Bookmark', True), result)
+        with self.new_access('anon').web_request() as req:
+            result = self.app_handle_request(req, path='/bookmark')
+            self.assertIn(self.expected_notfound_html('Bookmark'), result)
+
+    def test_true_404(self):
+        """Check 404 is returned on unknown entity type."""
+        with self.admin_access.web_request() as req:
+            self.app_handle_request(req, path='/zorglub')
+            self.assertEqual(req.status_out, 404)
+
 
 if __name__ == '__main__':
     unittest.main()



More information about the saem-devel mailing list