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

Sylvain Thénault sylvain.thenault at logilab.fr
Tue Feb 21 15:29:52 CET 2017


Le 20/02/2017 à 09:33, Denis Laxalde a écrit :
> Philippe Pepiot a écrit :
>> On 02/09/2017 01:13 PM, Denis Laxalde wrote:
>>> Philippe Pepiot a écrit :
>>>> # 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) 

record_id should be set even if entity has not record_id yet.
>>>> +
>>>> +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)
>>>> +
>>>> +
>>>
>>> I wonder why this is done in 'after_add_entity' event and using an
>>> operation. Couldn't it be a 'before_entity_event' so that you'd update
>>> 'self.entity.cw_edited'? Otherwise, this deserves an explanation.
>>
>> Since ark assignation is done in a 'before_add_entity' event, I thought
>> that I might be better to not rely on hook execution order for this.
>>
>
> AssignARKHook has "order = metadata.InitMetaAttrsHook.order - 1" so it
> would be called pretty early. IIUC, using a before_add_entity hook would
> avoid an extra database request to set the record_id attribute since you
> can insert it in .cw_edited.
>
> Anyways, this does not explain why you'll need an operation.

I agree we may not bother with an operation to detect if the record has
a pre-existing record it. But in this case, we would probably have to do
a 'before_add_entity' hook to set the new record id in cw_edited (hence
avoid an extra query), and if necessary record an operation to create a
EACOtherRecordId once the record has been created.

-- 
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