[PATCH saem_ref V2] [hook] export EAC ARK on AuthorityRecordId

Philippe Pepiot philippe.pepiot at logilab.fr
Thu Feb 23 18:09:59 CET 2017


# HG changeset patch
# User Philippe Pepiot <philippe.pepiot at logilab.fr>
# Date 1487869703 -3600
#      Thu Feb 23 18:08:23 2017 +0100
# Node ID 4dd41a5f9b317e40f69cb3b2e80a5ef64e898e66
# Parent  f37f15e37039b11ade8f1d456a0bd158f26ddbea
# Available At https://hg.logilab.org/review/cubes/saem_ref
#              hg pull https://hg.logilab.org/review/cubes/saem_ref -r 4dd41a5f9b31
# Tested at: https://jenkins.logilab.org/job/cubicweb-saem_ref/98/
[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,20 @@ def extract_ark(url):
     return 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):
+        entity = self.entity
+        if entity.record_id:
+            # move record_id to eac_other_record_id
+            self._cw.create_entity('EACOtherRecordId', value=entity.record_id,
+                                   eac_other_record_id_of=entity)
+        entity.cw_set(record_id=entity.ark)
+
+
 # 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,32 @@ 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.cnx() as cnx:
+            # ark should be copied to record_id
+            record = testutils.authority_record(cnx, u'test 1')
+            self.assertEqual(record.record_id, record.ark)
+            eac_xml = record.cw_adapt_to('EAC-CPF').dump()
+            self.assertIn('<recordId>{0}</recordId>'.format(record.ark),
+                          eac_xml)
+
+        with self.admin_access.client_cnx() as cnx:
+            # If created with a record_id, copy record_id to
+            # eac_other_record_id and copy ark to record_id
+            record = testutils.authority_record(cnx, u'test 2',
+                                                record_id=u'Record ID')
+            cnx.commit()
+            self.assertEqual(
+                set([r.value for r in record.reverse_eac_other_record_id_of]),
+                set(['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>Record ID</otherRecordId>',
+                          eac_xml)
+
 
 if __name__ == '__main__':
     import unittest



More information about the saem-devel mailing list