Set sensible default user_cardinality on some entities
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 13 Oct 2016 12:56:28 +0200
changeset 1739 e0d4e2917137
parent 1738 508d1fb489a7
child 1740 d53885d29c38
Set sensible default user_cardinality on some entities For those entities, user cardinality will be hidden for simplified profile because it's expected to be '1'. Changing the default in the schema will ensure that entities created on a simplified profile will have the proper cardinality, and that default values displayed on a full profile will be compatible.
schema/seda2.py
test/test_profile_generation.py
test/test_schema.py
xsd2yams.py
--- a/schema/seda2.py	Fri Oct 14 11:17:35 2016 +0200
+++ b/schema/seda2.py	Thu Oct 13 12:56:28 2016 +0200
@@ -284,7 +284,7 @@
     inlined = True
     constraints = []
 
-@seda_profile_element(cardinalities=['0..1', '0..n', '1', '1..n'], default_cardinality='0..1',
+@seda_profile_element(cardinalities=['0..1', '0..n', '1', '1..n'], default_cardinality='1',
                       annotable=True)
 class SEDASeqClassificationRuleRule(EntityType):
     u""""""
@@ -403,7 +403,7 @@
     inlined = True
     constraints = [RQLConstraint('O in_scheme CS, CACLV seda_classification_rule_code_list_version_from AT, CACLV seda_classification_rule_code_list_version_to CS,S container AT')]
 
-@seda_profile_element(cardinalities=['0..1', '1'], default_cardinality='0..1',
+@seda_profile_element(cardinalities=['0..1', '1'], default_cardinality='1',
                       annotable=True)
 class SEDAStartDate(EntityType):
     u""""""
@@ -418,7 +418,7 @@
     inlined = True
     constraints = []
 
-@seda_profile_element(cardinalities=['0..1', '0..n', '1', '1..n'], default_cardinality='0..1',
+@seda_profile_element(cardinalities=['0..1', '0..n', '1', '1..n'], default_cardinality='1',
                       annotable=True)
 class SEDASeqReuseRuleRule(EntityType):
     u""""""
@@ -457,7 +457,7 @@
     inlined = True
     constraints = [RQLConstraint('O in_scheme CS, CACLV seda_reuse_rule_code_list_version_from AT, CACLV seda_reuse_rule_code_list_version_to CS,S container AT')]
 
-@seda_profile_element(cardinalities=['0..1', '0..n', '1', '1..n'], default_cardinality='0..1',
+@seda_profile_element(cardinalities=['0..1', '0..n', '1', '1..n'], default_cardinality='1',
                       annotable=True)
 class SEDASeqDisseminationRuleRule(EntityType):
     u""""""
@@ -496,7 +496,7 @@
     inlined = True
     constraints = [RQLConstraint('O in_scheme CS, CACLV seda_dissemination_rule_code_list_version_from AT, CACLV seda_dissemination_rule_code_list_version_to CS,S container AT')]
 
-@seda_profile_element(cardinalities=['0..1', '0..n', '1', '1..n'], default_cardinality='0..1',
+@seda_profile_element(cardinalities=['0..1', '0..n', '1', '1..n'], default_cardinality='1',
                       annotable=True)
 class SEDASeqAccessRuleRule(EntityType):
     u""""""
@@ -535,7 +535,7 @@
     inlined = True
     constraints = [RQLConstraint('O in_scheme CS, CACLV seda_access_rule_code_list_version_from AT, CACLV seda_access_rule_code_list_version_to CS,S container AT')]
 
-@seda_profile_element(cardinalities=['0..1', '0..n', '1', '1..n'], default_cardinality='0..1',
+@seda_profile_element(cardinalities=['0..1', '0..n', '1', '1..n'], default_cardinality='1',
                       annotable=True)
 class SEDASeqAppraisalRuleRule(EntityType):
     u""""""
@@ -583,7 +583,7 @@
     inlined = True
     constraints = [RQLConstraint('O in_scheme CS, CACLV seda_appraisal_rule_code_list_version_from AT, CACLV seda_appraisal_rule_code_list_version_to CS,S container AT')]
 
-@seda_profile_element(cardinalities=['0..1', '0..n', '1', '1..n'], default_cardinality='0..1',
+@seda_profile_element(cardinalities=['0..1', '0..n', '1', '1..n'], default_cardinality='1',
                       annotable=True)
 class SEDASeqStorageRuleRule(EntityType):
     u""""""
--- a/test/test_profile_generation.py	Fri Oct 14 11:17:35 2016 +0200
+++ b/test/test_profile_generation.py	Thu Oct 13 12:56:28 2016 +0200
@@ -537,7 +537,7 @@
             access_rule = create('SEDAAccessRule', seda_access_rule=transfer)
             access_rule_seq = create('SEDASeqAccessRuleRule',
                                      reverse_seda_seq_access_rule_rule=access_rule)
-            create('SEDAStartDate', seda_start_date=access_rule_seq)
+            create('SEDAStartDate', user_cardinality=u'0..1', seda_start_date=access_rule_seq)
             # binary data object
             bdo = create('SEDABinaryDataObject', id=u"bdo1", user_cardinality=u'0..n',
                          seda_binary_data_object=transfer,
@@ -557,7 +557,7 @@
             appraisal_rule = create('SEDAAppraisalRule', seda_appraisal_rule=unit_alt_seq)
             appraisal_rule_seq = create('SEDASeqAppraisalRuleRule',
                                         reverse_seda_seq_appraisal_rule_rule=appraisal_rule)
-            create('SEDAStartDate', seda_start_date=appraisal_rule_seq)
+            create('SEDAStartDate', user_cardinality=u'0..1', seda_start_date=appraisal_rule_seq)
             access_rule = create('SEDAAccessRule', seda_access_rule=unit_alt_seq)
             create('SEDADisseminationRule', seda_dissemination_rule=unit_alt_seq)
             create('SEDAReuseRule', seda_reuse_rule=unit_alt_seq)
@@ -703,7 +703,8 @@
                              user_cardinality=u'1',
                              seda_content=unit_alt_seq,
                              seda_description_level=concepts['file'],
-                             reverse_seda_start_date=create('SEDAStartDate'),
+                             reverse_seda_start_date=create('SEDAStartDate',
+                                                            user_cardinality=u'0..1'),
                              reverse_seda_end_date=create('SEDAEndDate'),
                              # XXX, value=date(2015, 2, 24)),
                              reverse_seda_description=create('SEDADescription'))
--- a/test/test_schema.py	Fri Oct 14 11:17:35 2016 +0200
+++ b/test/test_schema.py	Thu Oct 13 12:56:28 2016 +0200
@@ -125,6 +125,21 @@
             self.assertEqual(rqlexpr.expression, 'U has_{action}_permission C, '
                              'X container C'.format(action=action))
 
+    def test_rule_default_cardinality(self):
+
+        with self.admin_access.client_cnx() as cnx:
+            transfer = cnx.create_entity('SEDAArchiveTransfer', title=u'test profile')
+            for rule_type in ('access', 'appraisal'):
+                rule_etype = 'SEDA{0}Rule'.format(rule_type.capitalize())
+                rule_rtype = 'seda_{0}_rule'.format(rule_type)
+                rule = cnx.create_entity(rule_etype, **{rule_rtype: transfer})
+                rule_rule_etype = 'SEDASeq{0}RuleRule'.format(rule_type.capitalize())
+                rule_rule_rtype = 'reverse_seda_seq_{0}_rule_rule'.format(rule_type)
+                rule_rule = cnx.create_entity(rule_rule_etype, **{rule_rule_rtype: rule})
+                self.assertEqual(rule_rule.user_cardinality, '1')
+                start_date = cnx.create_entity('SEDAStartDate', seda_start_date=rule_rule)
+                self.assertEqual(start_date.user_cardinality, '1')
+
 
 class SecurityTC(CubicWebTC):
 
--- a/xsd2yams.py	Fri Oct 14 11:17:35 2016 +0200
+++ b/xsd2yams.py	Thu Oct 13 12:56:28 2016 +0200
@@ -177,6 +177,12 @@
 RTYPE_CARD = {
     'seda_custodial_history_item': '*',
 }
+DEFAULT_USER_CARDINALITY = {
+    'SEDAStartDate': '1',
+}
+for rule_type in RULE_TYPES:
+    etype = 'SEDASeq{0}RuleRule'.format(rule_type.capitalize())
+    DEFAULT_USER_CARDINALITY[etype] = '1'
 
 _CARD_TO_CARDS = {
     '1': ['1'],
@@ -433,8 +439,9 @@
         if mapping.cards:
             cards = mapping.cards
             annotable = len(cards) > 1
+            default_card = DEFAULT_USER_CARDINALITY.get(mapping.etype, cards[0])
             code = '''\
-@seda_profile_element(cardinalities={cards}, default_cardinality='{cards[0]}',
+@seda_profile_element(cardinalities={cards}, default_cardinality='{default_card}',
                       annotable={annotable})
 '''.format(**locals())
         else: