[entities] cw_rest_attr_info() should only consider required attributes (closes #3766717)
authorRémi Cardona <remi.cardona@logilab.fr>
Mon, 28 Jul 2014 16:05:19 +0200
changeset 9978 8f4b15e5b300
parent 9977 e48e5a597ccc
child 9979 9ccdb3751fbe
[entities] cw_rest_attr_info() should only consider required attributes (closes #3766717) This prevents CW from choosing unique but non-required attributes. None/NULL is a poor choice for RESTful URIs.
entity.py
test/data/schema.py
test/unittest_entity.py
test/unittest_schema.py
--- a/entity.py	Fri Aug 08 13:05:07 2014 +0200
+++ b/entity.py	Mon Jul 28 16:05:19 2014 +0200
@@ -425,8 +425,10 @@
             needcheck = not cls.e_schema.has_unique_values(mainattr)
         else:
             for rschema in cls.e_schema.subject_relations():
-                if rschema.final and rschema != 'eid' \
-                        and cls.e_schema.has_unique_values(rschema):
+                if (rschema.final
+                    and rschema != 'eid'
+                    and cls.e_schema.has_unique_values(rschema)
+                    and cls.e_schema.rdef(rschema.type).cardinality[0] == '1'):
                     mainattr = str(rschema)
                     needcheck = False
                     break
--- a/test/data/schema.py	Fri Aug 08 13:05:07 2014 +0200
+++ b/test/data/schema.py	Mon Jul 28 16:05:19 2014 +0200
@@ -89,3 +89,8 @@
 
 class StateFull(WorkflowableEntityType):
     name = String()
+
+
+class Reference(EntityType):
+    nom = String(unique=True)
+    ean = String(unique=True, required=True)
--- a/test/unittest_entity.py	Fri Aug 08 13:05:07 2014 +0200
+++ b/test/unittest_entity.py	Mon Jul 28 16:05:19 2014 +0200
@@ -754,6 +754,11 @@
             # unique attr with None value (nom in this case)
             friend = req.create_entity('Ami', prenom=u'bob')
             self.assertEqual(friend.rest_path(), unicode(friend.eid))
+            # 'ref' below is created without the unique but not required
+            # attribute, make sur that the unique _and_ required 'ean' is used
+            # as the rest attribute
+            ref = req.create_entity('Reference', ean=u'42-1337-42')
+            self.assertEqual(ref.rest_path(), 'reference/42-1337-42')
 
     def test_can_use_rest_path(self):
         self.assertTrue(can_use_rest_path(u'zobi'))
--- a/test/unittest_schema.py	Fri Aug 08 13:05:07 2014 +0200
+++ b/test/unittest_schema.py	Mon Jul 28 16:05:19 2014 +0200
@@ -168,7 +168,7 @@
                              'CWUniqueTogetherConstraint', 'CWUser',
                              'ExternalUri', 'File', 'Float', 'Int', 'Interval', 'Note',
                              'Password', 'Personne', 'Produit',
-                             'RQLExpression',
+                             'RQLExpression', 'Reference',
                              'Service', 'Societe', 'State', 'StateFull', 'String', 'SubNote', 'SubWorkflowExitPoint',
                              'Tag', 'TZDatetime', 'TZTime', 'Time', 'Transition', 'TrInfo',
                              'Usine',
@@ -417,6 +417,7 @@
                      ('cw_source', 'Personne', 'CWSource', 'object'),
                      ('cw_source', 'Produit', 'CWSource', 'object'),
                      ('cw_source', 'RQLExpression', 'CWSource', 'object'),
+                     ('cw_source', 'Reference', 'CWSource', 'object'),
                      ('cw_source', 'Service', 'CWSource', 'object'),
                      ('cw_source', 'Societe', 'CWSource', 'object'),
                      ('cw_source', 'State', 'CWSource', 'object'),