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

Sylvain Thénault sylvain.thenault at logilab.fr
Fri Feb 24 15:51:57 CET 2017



Le 24/02/2017 à 15:37, Philippe Pepiot a écrit :
> # HG changeset patch
> # User Philippe Pepiot <philippe.pepiot at logilab.fr>
> # Date 1487946737 -3600
> #      Fri Feb 24 15:32:17 2017 +0100
> # Node ID bfa49acea3f1b7271ec74f85d85c1d1cba253d66
> # Parent  5b36edccfd6eab8e7cf78107a779335654ec4a91
> # Available At https://hg.logilab.org/review/cubes/saem_ref
> #              hg pull https://hg.logilab.org/review/cubes/saem_ref -r bfa49acea3f1
> # Tested at: https://jenkins.logilab.org/job/cubicweb-saem_ref/101/
> [hook] export EAC ARK on AuthorityRecordId
>
> Copy (eventually) created recordId to otherRecordId and set recordId to ARK
> (thus export ARK in a <recordId /> field).
>
> Do this in hooks as well as in dataimport machinery which use MassiveStore and
> doesn't trigger hooks.
>
> 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/cubicweb_saem_ref/sobjects/__init__.py b/cubicweb_saem_ref/sobjects/__init__.py
> --- a/cubicweb_saem_ref/sobjects/__init__.py
> +++ b/cubicweb_saem_ref/sobjects/__init__.py
> @@ -19,6 +19,7 @@ from six.moves import map as imap
>  
>  from cubicweb.predicates import match_user_groups, match_kwargs
>  from cubicweb.server import Service
> +from cubicweb.dataimport.importer import ExtEntity
>  from cubicweb.dataimport.stores import MetadataGenerator
>  
>  from cubicweb_eac import sobjects as eac
> @@ -56,7 +57,8 @@ class SAEMMetadataGenerator(MetadataGene
>      def entity_attrs(self, etype):
>          entity_attrs = super(SAEMMetadataGenerator, self).entity_attrs(etype)
>          if etype in ('ConceptScheme', 'Concept', 'AuthorityRecord', 'Agent'):
> -            entity_attrs.insert(0, 'ark')  # insert before cwuri
> +            # insert before cwuri
> +            entity_attrs = ['ark', 'record_id'] + entity_attrs
>          return entity_attrs

on AuthorityRecords have record_id, not other
>  
>      def gen_ark(self, etype, eid, attrs):
> @@ -66,6 +68,9 @@ class SAEMMetadataGenerator(MetadataGene
>          # implementation
>          return attrs['ark']
>  
> +    def gen_record_id(self, etype, eid, attrs):
> +        return attrs['ark']
> +
>  
I wouldn't have botherered with the metadata generator, only the
transform function below should be enough. Simpli add
values['record_id'] = values['ark] there.
>  class EACImportService(eac.EACImportService):
>      """Service to import an AuthorityRecord from an EAC XML file - overriden from the EAC cube to
> @@ -99,7 +104,21 @@ class EACImportService(eac.EACImportServ
>                  extentity.values['authority'] = set([self._authority.cwuri])
>              return extentity
>  
> -        return imap(set_authority_or_naa, extentities)
> +        def move_record_id(extentities):
> +            for extentity in extentities:
> +                if (extentity.etype == 'AuthorityRecord'
> +                        and 'record_id' in extentity.values):
> +                    yield ExtEntity(etype='EACOtherRecordId',
> +                                    extid='EACOtherRecordId-{0}'.format(extentity.extid),
> +                                    values={
> +                                        'value': extentity.values.pop('record_id'),
> +                                        'eac_other_record_id_of': set([extentity.extid]),
> +                                    })
> +                yield extentity
> +
> +        extentities = imap(set_authority_or_naa, extentities)
> +        extentities = move_record_id(extentities)
> +        return extentities
>  
>  
>  class AllocateArk(Service):
> diff --git a/test/test_ccplugin.py b/test/test_ccplugin.py
> --- a/test/test_ccplugin.py
> +++ b/test/test_ccplugin.py
> @@ -70,6 +70,9 @@ class ImportEacDataCommandTC(testlib.Cub
>              record = cnx.find('AuthorityRecord').one()
>              self.assertEqual(record.dc_title(), u"Centre d'information et d'orientation (CIO)")
>              self.assertTrue(record.ark.startswith('0/r0'), record.ark)
> +            self.assertEqual(record.record_id, record.ark)
> +            self.assertEqual([r.value for r in record.reverse_eac_other_record_id_of],
> +                             ['D33-100'])
>  
>      def test_bad_authority(self):
>          code, output = self.run_import_eac('--authority', 'unexisting')
> 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,20 @@ 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)
> +
> +            # If created with a record_id, copy it to eac_other_record_id and
> +            # copy ark to record_id
> +            record = testutils.authority_record(cnx, u'test 2',
> +                                                record_id=u'Record ID')
> +            self.assertEqual(
> +                set([r.value for r in record.reverse_eac_other_record_id_of]),
> +                set(['Record ID']))
> +
>  
>  if __name__ == '__main__':
>      import unittest
> diff --git a/test/unittest_dataimport.py b/test/unittest_dataimport.py
> --- a/test/unittest_dataimport.py
> +++ b/test/unittest_dataimport.py
> @@ -31,7 +31,7 @@ class EACDataImportTC(CubicWebTC):
>              cnx.user.cw_set(authority=testutils.authority_with_naa(cnx))
>              cnx.commit()
>  
> -    def test_imported_activities(self):
> +    def test_import(self):
>          fpath = self.datapath('EAC', 'FRAD033_EAC_dataimport.xml')
>          with self.admin_access.repo_cnx() as cnx:
>              import_log = SimpleImportLog(basename(fpath))
> @@ -43,6 +43,11 @@ class EACDataImportTC(CubicWebTC):
>              rset = cnx.execute('Any A WHERE A generated X, X eid %(x)s', {'x': record.eid})
>              # Two activities imported from EAC-CPF file, one created by our hook.
>              self.assertEqual(len(rset), 3)
> +            # recordId should go in eac_other_record_id and ark should be
> +            # copied to record_id instead.
> +            self.assertEqual(record.record_id, record.ark)
> +            self.assertEqual([r.value for r in record.reverse_eac_other_record_id_of],
> +                             ['FRAD033_EAC_00001'])
>  
>  
>  if __name__ == '__main__':
>

-- 
Sylvain Thénault, LOGILAB, Paris (01.45.32.03.12) - Toulouse (05.62.17.16.42)
Formations Python, Debian, Méth. Agiles: http://www.logilab.fr/formations
Développement logiciel sur mesure:       http://www.logilab.fr/services
CubicWeb, the semantic web framework:    http://www.cubicweb.org



More information about the saem-devel mailing list