[saem-devel] [PATCH] [hook] export EAC ARK on AuthorityRecordId

Philippe Pepiot philippe.pepiot at logilab.fr
Wed Feb 8 16:23:41 CET 2017


# HG changeset patch
# User Philippe Pepiot <philippe.pepiot at logilab.fr>
# Date 1486565867 -3600
#      Wed Feb 08 15:57:47 2017 +0100
# Node ID 1fb0cc9f7273c44e9953ea6b02d1bc135ab14089
# Parent  8a064175a178fcaa2e116f031821b70e786ddbce
# Available At https://hg.logilab.org/review/cubes/saem_ref
#              hg pull https://hg.logilab.org/review/cubes/saem_ref -r 1fb0cc9f7273
# Tested at https://jenkins.logilab.org/job/cubicweb-saem_ref/78/
[hook] export EAC ARK on AuthorityRecordId

Copy (eventually) created recordId to otherRecordId and set recordId to ARK
(thus export ARK in a <recordId /> field).

Closes extranet #12572781

diff --git a/cubicweb_saem_ref/hooks.py b/cubicweb_saem_ref/hooks.py
--- a/cubicweb_saem_ref/hooks.py
+++ b/cubicweb_saem_ref/hooks.py
@@ -222,6 +222,27 @@ def extract_ark(url):
     return ark
 
 
+class AuthorityRecordIdOp(hook.DataOperationMixIn, hook.Operation):
+    """Copy EAC recordId to otherRecordId and set recordId to ARK"""
+
+    def precommit_event(self):
+        cnx = self.cnx
+        for entity in self.get_data():
+            if entity.record_id:
+                cnx.create_entity('EACOtherRecordId', value=entity.record_id,
+                                  eac_other_record_id_of=entity)
+                entity.cw_set(record_id=entity.ark)
+
+
+class AuthorityRecordIdHook(hook.Hook):
+    __regid__ = 'saem_ref.authority_record_id'
+    __select__ = hook.Hook.__select__ & is_instance('AuthorityRecord')
+    events = ('after_add_entity',)
+
+    def __call__(self):
+        AuthorityRecordIdOp.get_instance(self._cw).add_data(self.entity)
+
+
 # Life-cycle logging ###########################################################
 
 class Record(object):
diff --git a/test/test_eac.py b/test/test_eac.py
--- a/test/test_eac.py
+++ b/test/test_eac.py
@@ -33,6 +33,22 @@ class EACExportTC(CubicWebTC):
             self.assertIn('<agent>admin</agent>', eac_xml)
             self.assertIn('<agent>007</agent>', eac_xml)
 
+    def test_record_id(self):
+        with self.admin_access.client_cnx() as cnx:
+            record = testutils.authority_record(cnx, u'My record',
+                                                record_id=u'My record ID')
+            cnx.commit()
+            self.assertEqual(
+                set([r.value for r in record.reverse_eac_other_record_id_of]),
+                set(['My record ID']))
+            ark = u'0/r00000{0}'.format(record.eid)
+            self.assertEqual(record.ark, ark)
+            self.assertEqual(record.record_id, ark)
+            eac_xml = record.cw_adapt_to('EAC-CPF').dump()
+            self.assertIn('<recordId>{0}</recordId>'.format(ark), eac_xml)
+            self.assertIn('<otherRecordId>My record ID</otherRecordId>',
+                          eac_xml)
+
 
 if __name__ == '__main__':
     import unittest



More information about the saem-devel mailing list