[entities] Extract code to get closest management rule from profile generation
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 17 Mar 2017 08:58:37 +0100
changeset 2513 660611c16bb1
parent 2512 93a52e1de95c
child 2514 c18e4cdffd64
[entities] Extract code to get closest management rule from profile generation and add it as a method of archive unit entities. Related to extranet #14593198
cubicweb_seda/entities/custom.py
cubicweb_seda/entities/profile_generation.py
--- a/cubicweb_seda/entities/custom.py	Fri Mar 17 07:59:03 2017 +0100
+++ b/cubicweb_seda/entities/custom.py	Fri Mar 17 08:58:37 2017 +0100
@@ -14,6 +14,7 @@
 # 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/>.
 
+from ..xsd2yams import RULE_TYPES
 from . import generated
 
 
@@ -24,6 +25,18 @@
     return annotation.splitlines()[0]
 
 
+def _climb_rule_holders(transfer_or_archive_unit):
+    """Starting from a transfer or archive unit entity, yield entity that may be linked to
+    management rule until the root (transfer) is reached.
+    """
+    while transfer_or_archive_unit is not None:
+        if transfer_or_archive_unit.cw_etype == 'SEDAArchiveTransfer':
+            yield transfer_or_archive_unit
+        else:
+            yield transfer_or_archive_unit.first_level_choice.content_sequence
+        transfer_or_archive_unit = transfer_or_archive_unit.cw_adapt_to('ITreeBase').parent()
+
+
 class SEDAArchiveTransfer(generated.SEDAArchiveTransfer):
 
     def dc_title(self):
@@ -77,6 +90,17 @@
         """
         return self.related('seda_alt_archive_unit_archive_unit_ref_id', 'subject').one()
 
+    def inherited_rule(self, rule_type):
+        """Return the rule entity of the given type, defined on this unit or in its nearest parent
+        possible defining it, or None if no matching rule has been found.
+        """
+        assert rule_type in RULE_TYPES
+        rtype = 'reverse_seda_{}_rule'.format(rule_type)
+        for rule_holder in _climb_rule_holders(self):
+            if getattr(rule_holder, rtype):
+                return getattr(rule_holder, rtype)[0]
+        return None
+
 
 class SEDABinaryDataObject(generated.SEDABinaryDataObject):
 
--- a/cubicweb_seda/entities/profile_generation.py	Fri Mar 17 07:59:03 2017 +0100
+++ b/cubicweb_seda/entities/profile_generation.py	Fri Mar 17 08:58:37 2017 +0100
@@ -754,18 +754,6 @@
         return parent
 
 
-def climb_rule_holders(transfer_or_archive_unit):
-    """Starting from a transfer or archive unit entity, yield entity that may be linked to management
-    rule until the root (transfer) is reached.
-    """
-    while transfer_or_archive_unit is not None:
-        if transfer_or_archive_unit.cw_etype == 'SEDAArchiveTransfer':
-            yield transfer_or_archive_unit
-        else:
-            yield transfer_or_archive_unit.first_level_choice.content_sequence
-        transfer_or_archive_unit = transfer_or_archive_unit.cw_adapt_to('ITreeBase').parent()
-
-
 def _safe_cardinality(entity):
     """Return entity's cardinality if some entity is given, else None."""
     if entity is None:
@@ -924,11 +912,11 @@
         self.xsd_transferring_agency_archive_identifier(archive_node, content_entity,
                                                         'TransferringAgencyArchiveIdentifier')
         self.xsd_content_description(archive_node, content_entity)
-        appraisal_rule_entity = self.archive_unit_appraisal_rule(archive_unit)
+        appraisal_rule_entity = archive_unit.inherited_rule('appraisal')
         if appraisal_rule_entity:
             self.xsd_appraisal_rule(archive_node, appraisal_rule_entity)
         # not optional in seda 1
-        access_rule_entity = self.archive_unit_access_rule(archive_unit)
+        access_rule_entity = archive_unit.inherited_rule('access')
         self.xsd_access_rule(archive_node, access_rule_entity)
         self.xsd_children(archive_node, archive_unit)
 
@@ -953,10 +941,10 @@
                 or content_entity.description
                 or content_entity.keywords):
             self.xsd_content_description(ao_node, content_entity)
-        appraisal_rule_entity = self.archive_unit_appraisal_rule(archive_unit)
+        appraisal_rule_entity = archive_unit.inherited_rule('appraisal')
         if appraisal_rule_entity:
             self.xsd_appraisal_rule(ao_node, appraisal_rule_entity)
-        access_rule_entity = self.archive_unit_access_rule(archive_unit)
+        access_rule_entity = archive_unit.inherited_rule('access')
         if access_rule_entity:
             self.xsd_access_rule(ao_node, access_rule_entity)
         self.xsd_children(ao_node, archive_unit)
@@ -1260,22 +1248,10 @@
         #     attributes[0]['use'] = 'required'
         return attributes
 
-    def archive_unit_access_rule(self, archive_unit):
-        for rule_holder in climb_rule_holders(archive_unit):
-            if rule_holder.reverse_seda_access_rule:
-                return rule_holder.reverse_seda_access_rule[0]
-        return None
-
     def archive_unit_name(self, archive_unit):
         seq = archive_unit.first_level_choice.content_sequence
         return seq.title
 
-    def archive_unit_appraisal_rule(self, archive_unit):
-        for rule_holder in climb_rule_holders(archive_unit):
-            if rule_holder.reverse_seda_appraisal_rule:
-                return rule_holder.reverse_seda_appraisal_rule[0]
-        return None
-
     def agency_name(self, agency):
         return agency.agency.dc_title() if agency and agency.agency else None
 
@@ -1346,11 +1322,11 @@
                                                         'TransferringAgencyArchiveIdentifier')
         self.xsd_content_description(archive_node, content_entity)
 
-        appraisal_rule_entity = self.archive_unit_appraisal_rule(archive_unit)
+        appraisal_rule_entity = archive_unit.inherited_rule('appraisal')
         if appraisal_rule_entity:
             self.xsd_appraisal_rule(archive_node, appraisal_rule_entity)
         # in SEDA 0.2, access restriction is not mandatory
-        access_rule_entity = self.archive_unit_access_rule(archive_unit)
+        access_rule_entity = archive_unit.inherited_rule('access')
         if access_rule_entity:
             self.xsd_access_rule(archive_node, access_rule_entity)
         self.xsd_children(archive_node, archive_unit)