Extend parent_and_container function to consider 'eid' in form params
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 14 Oct 2016 11:17:50 +0200
changeset 1734 fff22f766b10
parent 1733 15c2c768d215
child 1735 4e36b39d78fa
Extend parent_and_container function to consider 'eid' in form params When an inlined form is displayed through a direct call instead of an ajax call, 'eid' is specified in req.form and we may get the container's from that. Also, add test for this function - a bit too much low-level but that's a start.
entities/__init__.py
test/test_entities.py
--- a/entities/__init__.py	Fri Oct 14 11:19:02 2016 +0200
+++ b/entities/__init__.py	Fri Oct 14 11:17:50 2016 +0200
@@ -38,18 +38,36 @@
         container = entity.cw_adapt_to('IContained').container
         parent = entity.cw_adapt_to('IContained').parent
     else:
+        req = entity._cw
         # but parent entity, retrieved through linkto, may be the container itself or a
         # contained entity
         try:
-            parent_eid = int(entity._cw.form['__linkto'].split(':')[1])
+            parent_eid = int(req.form['__linkto'].split(':')[1])
         except KeyError:
             # ajax created form
             try:
-                parent_eid = int(json.loads(entity._cw.form['arg'][0]))
+                parent_eid = int(json.loads(req.form['arg'][0]))
+            except KeyError:
+                # direct inlined form (not through ajax) - eid may not be the direct parent, but
+                # this is enough to get the container
+                try:
+                    eid = int(req.form['eid'])
+                except KeyError:
+                    # unable to get parent eid for now :(
+                    return None, None
+                else:
+                    ancestor = req.entity_from_eid(eid)
+                    icontainer = ancestor.cw_adapt_to('IContainer')
+                    if icontainer is None:
+                        container = ancestor.cw_adapt_to('IContained').container
+                    else:
+                        container = icontainer.container
+                    # unable to get parent eid for now :(
+                    return None, container
             except ValueError:
-                # unable to get parent eid for now :()
+                # unable to get parent eid for now :(
                 return None, None
-        parent = entity._cw.entity_from_eid(parent_eid)
+        parent = req.entity_from_eid(parent_eid)
         icontainer = parent.cw_adapt_to('IContainer')
         if icontainer is None:
             container = parent.cw_adapt_to('IContained').container
--- a/test/test_entities.py	Fri Oct 14 11:19:02 2016 +0200
+++ b/test/test_entities.py	Fri Oct 14 11:17:50 2016 +0200
@@ -15,11 +15,17 @@
 # with this program. If not, see <http://www.gnu.org/licenses/>.
 """cubicweb-saem_ref unit tests for entities.container"""
 
+import json
+import unittest
+
 from six import text_type
 
+from logilab.common import attrdict
+
 from cubicweb.devtools.testlib import CubicWebTC
 
-from cubes.seda.entities import seda_profile_container_def, simplified_profile, full_seda2_profile
+from cubes.seda.entities import (seda_profile_container_def, simplified_profile, full_seda2_profile,
+                                 parent_and_container)
 
 from testutils import create_archive_unit, create_data_object
 
@@ -149,6 +155,37 @@
             self.assertParent(bdo, au.eid)
 
 
+class ParentAndContainerTC(CubicWebTC):
+
+    def test_nodata(self):
+        with self.admin_access.web_request() as req:
+            parent, container = parent_and_container(attrdict(_cw=req, has_eid=lambda: False))
+            self.assertIsNone(parent)
+            self.assertIsNone(container)
+
+    def test_linkto(self):
+        with self.admin_access.web_request() as req:
+            transfer = req.cnx.create_entity('SEDAArchiveTransfer', title=u'test profile')
+            req.form['__linkto'] = 'x:{0}:y'.format(transfer.eid)
+            parent, container = parent_and_container(attrdict(_cw=req, has_eid=lambda: False))
+            self.assertEqual(parent.eid, transfer.eid)
+            self.assertEqual(container.eid, transfer.eid)
+
+    def test_arg(self):
+        with self.admin_access.web_request() as req:
+            transfer = req.cnx.create_entity('SEDAArchiveTransfer', title=u'test profile')
+            req.form['arg'] = [json.dumps(transfer.eid)]
+            parent, container = parent_and_container(attrdict(_cw=req, has_eid=lambda: False))
+            self.assertEqual(parent.eid, transfer.eid)
+            self.assertEqual(container.eid, transfer.eid)
+
+    def test_eid(self):
+        with self.admin_access.web_request() as req:
+            transfer = req.cnx.create_entity('SEDAArchiveTransfer', title=u'test profile')
+            req.form['eid'] = text_type(transfer.eid)
+            parent, container = parent_and_container(attrdict(_cw=req, has_eid=lambda: False))
+            self.assertIsNone(parent)
+            self.assertEqual(container.eid, transfer.eid)
+
 if __name__ == '__main__':
-    import unittest
     unittest.main()