Enhance parent detection of rule_ref_vocabulary
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 12 Oct 2016 10:57:05 +0200
changeset 1736 e6c7685a6b3b
parent 1735 4e36b39d78fa
child 1737 8d1f91ac86d1
Enhance parent detection of rule_ref_vocabulary Consider that: * container may be find while parent isn't (see previous cset) * the etype we're interested in may be found on form(.edited_entity) * if not, there is still some chance to find in req.form['arg] (json encoded) Refactor _rule_ref_vocabulary accordingly.
test/test_views.py
views/mgmt_rules.py
--- a/test/test_views.py	Wed Oct 12 10:53:17 2016 +0200
+++ b/test/test_views.py	Wed Oct 12 10:57:05 2016 +0200
@@ -47,10 +47,10 @@
             cnx.create_entity('SEDAPreventInheritance', seda_prevent_inheritance=rule_alt)
             cnx.commit()
 
-            self.assertEqual(_rule_ref_vocabulary(rule_base, transfer),
+            self.assertEqual(_rule_ref_vocabulary(transfer, rule_base.cw_etype),
                              [('you must specify a scheme for seda_access_rule_code_list_version_'
                                'from_object to select a value', INTERNAL_FIELD_VALUE)])
-            self.assertEqual(_rule_ref_vocabulary(rule_alt, transfer),
+            self.assertEqual(_rule_ref_vocabulary(transfer, rule_alt.cw_etype),
                              [('you must specify a scheme for seda_access_rule_code_list_version_'
                                'from_object to select a value', INTERNAL_FIELD_VALUE)])
 
@@ -59,9 +59,9 @@
                    seda_access_rule_code_list_version_to=access_scheme)
             cnx.commit()
 
-            self.assertEqual(_rule_ref_vocabulary(rule_base, transfer),
+            self.assertEqual(_rule_ref_vocabulary(transfer, rule_base.cw_etype),
                              [(access_concept.label(), text_type(access_concept.eid))])
-            self.assertEqual(_rule_ref_vocabulary(rule_alt, transfer),
+            self.assertEqual(_rule_ref_vocabulary(transfer, rule_alt.cw_etype),
                              [(access_concept.label(), text_type(access_concept.eid))])
 
 
--- a/views/mgmt_rules.py	Wed Oct 12 10:53:17 2016 +0200
+++ b/views/mgmt_rules.py	Wed Oct 12 10:57:05 2016 +0200
@@ -15,6 +15,8 @@
 # with this program. If not, see <http://www.gnu.org/licenses/>.
 """cubicweb-seda views for management rules"""
 
+import json
+
 from six import text_type
 
 from logilab.mtconverter import xml_escape
@@ -41,12 +43,18 @@
         # missing parent information
         msg = req._('you must validate first to select a possible value')
         return [(msg, INTERNAL_FIELD_VALUE)]
-    return _rule_ref_vocabulary(parent, container)
+    parent_etype = form.edited_entity.cw_etype
+    if parent_etype == 'SEDARefNonRuleId':
+        if parent is not None:
+            parent_etype = parent.cw_etype
+        else:
+            parent_etype = json.loads(req.form['arg'][1])
+    return _rule_ref_vocabulary(container, parent_etype)
 
 
-def _rule_ref_vocabulary(parent, container):
-    req = parent._cw
-    rule_type = rule_type_from_etype(parent.cw_etype)
+def _rule_ref_vocabulary(container, parent_etype):
+    req = container._cw
+    rule_type = rule_type_from_etype(parent_etype)
     rql = ('Any C WHERE C in_scheme CS, AT eid %(at)s, '
            'CACLV seda_{0}_rule_code_list_version_from AT, '
            'CACLV seda_{0}_rule_code_list_version_to CS'.format(rule_type))