[simplified ui] Introduce predicates to distinguish between simplified and full seda2 profiles
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 21 Jul 2016 14:36:31 +0200
changeset 1485 9d9cdfe9d85b
parent 1484 56a15e820988
child 1486 a29e9d30e63b
[simplified ui] Introduce predicates to distinguish between simplified and full seda2 profiles + related tests, including tests for features provided by underlying's `parent_and_container` function. Related to #14066262
entities/__init__.py
test/test_entities.py
--- a/entities/__init__.py	Fri Jul 22 11:24:07 2016 +0200
+++ b/entities/__init__.py	Thu Jul 21 14:36:31 2016 +0200
@@ -17,6 +17,8 @@
 
 import json
 
+from logilab.common.registry import objectify_predicate
+
 from cubes.compound.entities import IContainer, IContained
 
 from cubes.seda import seda_profile_container_def
@@ -56,6 +58,28 @@
     return parent, container
 
 
+def _transfer_from_context(rset, entity):
+    if entity is None:
+        entity = rset.one()
+    if entity.cw_etype != 'SEDAArchiveTransfer':
+        entity = parent_and_container(entity)[1]
+    return entity
+
+
+@objectify_predicate
+def simplified_profile(cls, req, rset=None, entity=None, **kwargs):
+    """Predicate returning 1 score if context entity is within a simplified profile."""
+    entity = _transfer_from_context(rset, entity)
+    return int(entity.simplified_profile)
+
+
+@objectify_predicate
+def full_seda2_profile(cls, req, rset=None, entity=None, **kwargs):
+    """Predicate returning 1 score if context entity is within a full seda2 profile."""
+    entity = _transfer_from_context(rset, entity)
+    return int(not entity.simplified_profile)
+
+
 def rule_type_from_etype(etype):
     """Return the rule type from an etype like SEDAAccessRule or SEDAAltAccessRulePreventInheritance
     """
--- a/test/test_entities.py	Fri Jul 22 11:24:07 2016 +0200
+++ b/test/test_entities.py	Thu Jul 21 14:36:31 2016 +0200
@@ -17,7 +17,7 @@
 
 from cubicweb.devtools.testlib import CubicWebTC
 
-from cubes.seda.entities import seda_profile_container_def
+from cubes.seda.entities import seda_profile_container_def, simplified_profile, full_seda2_profile
 
 
 def sort_container(container_def):
@@ -60,6 +60,43 @@
                 self.assertEqual(entity.cw_adapt_to('IContained').container.eid, transfer.eid)
 
 
+class FakeEntity(object):
+    cw_etype = 'Whatever'
+
+    def __init__(self, _cw):
+        self._cw = _cw
+
+    def has_eid(self):
+        return False
+
+
+class PredicatesTC(CubicWebTC):
+
+    def test_simplified_profile(self):
+        simplified_profile_pred = simplified_profile()
+        full_seda2_profile_pred = full_seda2_profile()
+        with self.admin_access.web_request() as req:
+            transfer = req.create_entity('SEDAArchiveTransfer', title=u'test profile')
+            self.assertEqual(simplified_profile_pred(None, req, entity=transfer), 0)
+            self.assertEqual(full_seda2_profile_pred(None, req, entity=transfer), 1)
+            self.assertEqual(simplified_profile_pred(None, req, rset=transfer.as_rset()), 0)
+            self.assertEqual(full_seda2_profile_pred(None, req, rset=transfer.as_rset()), 1)
+            access_rule = req.create_entity('SEDAAccessRule', seda_access_rule=transfer)
+            req.cnx.commit()  # needed to set the container relation
+            self.assertEqual(simplified_profile_pred(None, req, entity=access_rule), 0)
+            self.assertEqual(full_seda2_profile_pred(None, req, entity=access_rule), 1)
+            req.form['__linkto'] = 'whatever:%s:whatever' % transfer.eid
+            being_created = FakeEntity(req)
+            self.assertEqual(simplified_profile_pred(None, req, entity=being_created), 0)
+            self.assertEqual(full_seda2_profile_pred(None, req, entity=being_created), 1)
+            del req.form['__linkto']
+            req.form['arg'] = [unicode(transfer.eid)]
+            self.assertEqual(simplified_profile_pred(None, req, entity=being_created), 0)
+            self.assertEqual(full_seda2_profile_pred(None, req, entity=being_created), 1)
+            transfer.cw_set(simplified_profile=True)
+            self.assertEqual(simplified_profile_pred(None, req, entity=transfer), 1)
+            self.assertEqual(full_seda2_profile_pred(None, req, entity=transfer), 0)
+
 if __name__ == '__main__':
     import unittest
     unittest.main()