[ui] Move parent_and_container function from views to entities
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 21 Jul 2016 12:04:00 +0200
changeset 1474 aebba811c46a
parent 1473 9a2147273199
child 1484 56a15e820988
[ui] Move parent_and_container function from views to entities We'll need that here soon!
entities/__init__.py
views/__init__.py
views/dataobject.py
views/mgmt_rules.py
--- a/entities/__init__.py	Thu Jul 21 09:24:08 2016 +0200
+++ b/entities/__init__.py	Thu Jul 21 12:04:00 2016 +0200
@@ -15,6 +15,8 @@
 # with this program. If not, see <http://www.gnu.org/licenses/>.
 """Custom logic (eg entities and adapters) for cubicweb-seda."""
 
+import json
+
 from cubes.compound.entities import IContainer, IContained
 
 from cubes.seda import seda_profile_container_def
@@ -24,6 +26,36 @@
 XSDM_MAPPING = XSDMMapping('ArchiveTransfer')
 
 
+def parent_and_container(entity):
+    """Attempt to return the direct parent and container from the entity, handling case where entity
+    is being created and container information will be found through linkto or parent form
+    (partially supported)
+    """
+    if entity.has_eid():
+        # entity is expected to be a contained entity, not the container itself
+        container = entity.cw_adapt_to('IContained').container
+        parent = entity.cw_adapt_to('IContained').parent
+    else:
+        # 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])
+        except KeyError:
+            # ajax created form
+            try:
+                parent_eid = int(json.loads(entity._cw.form['arg'][0]))
+            except ValueError:
+                # unable to get parent eid for now :()
+                return None, None
+        parent = entity._cw.entity_from_eid(parent_eid)
+        icontainer = parent.cw_adapt_to('IContainer')
+        if icontainer is None:
+            container = parent.cw_adapt_to('IContained').container
+        else:
+            container = icontainer.container
+    return parent, container
+
+
 def rule_type_from_etype(etype):
     """Return the rule type from an etype like SEDAAccessRule or SEDAAltAccessRulePreventInheritance
     """
--- a/views/__init__.py	Thu Jul 21 09:24:08 2016 +0200
+++ b/views/__init__.py	Thu Jul 21 12:04:00 2016 +0200
@@ -14,8 +14,6 @@
 # You should have received a copy of the GNU Lesser General Public License
 # along with this program. If not, see <http://www.gnu.org/licenses/>.
 
-import json
-
 from yams import BASE_TYPES
 
 from cubicweb import tags, neg_role
@@ -180,33 +178,3 @@
     w(u'</ul>')
     w(u'</div>')
     return data.getvalue()
-
-
-def parent_and_container(entity):
-    """Attempt to return the direct parent and container from the entity, handling case where entity
-    is being created and container information will be found through linkto or parent form
-    (partially supported)
-    """
-    if entity.has_eid():
-        # entity is expected to be a contained entity, not the container itself
-        container = entity.cw_adapt_to('IContained').container
-        parent = entity.cw_adapt_to('IContained').parent
-    else:
-        # 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])
-        except KeyError:
-            # ajax created form
-            try:
-                parent_eid = int(json.loads(entity._cw.form['arg'][0]))
-            except ValueError:
-                # unable to get parent eid for now :()
-                return None, None
-        parent = entity._cw.entity_from_eid(parent_eid)
-        icontainer = parent.cw_adapt_to('IContainer')
-        if icontainer is None:
-            container = parent.cw_adapt_to('IContained').container
-        else:
-            container = icontainer.container
-    return parent, container
--- a/views/dataobject.py	Thu Jul 21 09:24:08 2016 +0200
+++ b/views/dataobject.py	Thu Jul 21 12:04:00 2016 +0200
@@ -27,7 +27,8 @@
 from cubes.skos.views import widgets as skos
 
 from cubes.seda.xsd2yams import RDEF_CONSTRAINTS
-from cubes.seda.views import rtags_from_xsd_element, add_subobject_link, parent_and_container
+from cubes.seda.entities import parent_and_container
+from cubes.seda.views import rtags_from_xsd_element, add_subobject_link
 from cubes.seda.views import viewlib
 from cubes.seda.views import uicfg as sedauicfg  # noqa - ensure those rules are defined first
 
--- a/views/mgmt_rules.py	Thu Jul 21 09:24:08 2016 +0200
+++ b/views/mgmt_rules.py	Thu Jul 21 12:04:00 2016 +0200
@@ -21,8 +21,8 @@
 from cubicweb.web import INTERNAL_FIELD_VALUE
 from cubicweb.web.views import uicfg
 
-from cubes.seda.entities import rule_type_from_etype
-from cubes.seda.views import parent_and_container, viewlib
+from cubes.seda.entities import parent_and_container, rule_type_from_etype
+from cubes.seda.views import viewlib
 from cubes.seda.views import uicfg as sedauicfg  # noqa - ensure those rules are defined first