Skip to content
This repository has been archived by the owner on Feb 17, 2022. It is now read-only.

Commit

Permalink
orcid: send to orcid fixes
Browse files Browse the repository at this point in the history
* Fixes wrong field names.

* Fixes serializer not being able to import configurations.

* Fixes `get_author_collection_records_from_valid_authors`
  query to return expected data.

* Adds plug for `orcid id` extraction method.

Signed-off-by: Panos Paparrigopoulos <panos.paparrigopoulos@cern.ch>
  • Loading branch information
Panos512 committed Sep 30, 2016
1 parent 02124ec commit 4e31f12
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 31 deletions.
1 change: 1 addition & 0 deletions invenio_orcid/config.py
Expand Up @@ -31,6 +31,7 @@
}

ORCID_JSON_CONVERTER_MODULE = 'invenio_orcid.utils:convert_to_orcid'
ORCID_ID_FETCHER = 'invenio_orcid.utils:get_orcid_id'

ORCID_AUTHORS_SEARCH_CLASS = 'invenio_search:RecordsSearch'

Expand Down
13 changes: 6 additions & 7 deletions invenio_orcid/serializers/orcid_serializer.py
Expand Up @@ -26,21 +26,20 @@

from __future__ import absolute_import, print_function

import json

from flask import jsonify
from flask import current_app, jsonify

from invenio_orcid.utils import convert_to_orcid

from werkzeug import import_string
from werkzeug import cached_property, import_string


class ORCIDSerializer(object):
"""Orcid serializer for records."""

def __init__(self):
"""Initialize state."""
self.convert_to_orcid = import_string(
@cached_property
def convert_to_orcid(self):
"""Import the orcid converter."""
return import_string(
current_app.config['ORCID_JSON_CONVERTER_MODULE'])

def serialize(self, pid, record, links_factory=None):
Expand Down
33 changes: 12 additions & 21 deletions invenio_orcid/tasks.py
Expand Up @@ -51,15 +51,14 @@ def prepare_authors_data_for_pushing_to_orcid(data):
fetcher_name = current_app.config['ORCID_RECORDS_PID_FETCHER']
pid = current_pidstore.fetchers[fetcher_name](None, data)
record_identifier = pid.pid_value
record_id = resolver.resolve(data.get(record_identifier))[
0].object_uuid
record_id = resolver.resolve(record_identifier)[0].object_uuid
authors = get_orcid_valid_authors(data)
token = None
author_orcid = ''
authors_with_orcid_credentials = []
for author in authors:
try:
token, author_orcid = get_authors_credentials(author['_source'])
token, author_orcid = get_authors_credentials(author)
except AttributeError:
continue
try:
Expand All @@ -86,14 +85,13 @@ def delete_from_orcid(sender, api=None):
fetcher_name = current_app.config['ORCID_RECORDS_PID_FETCHER']
pid = current_pidstore.fetchers[fetcher_name](None, sender)
record_identifier = pid.pid_value
record_id = resolver.resolve(sender.get(record_identifier))[
0].object_uuid
record_id = resolver.resolve(record_identifier)[0].object_uuid
records = ORCIDRecords.query.filter_by(record_id=record_id).all()
for record in records:
raw_user = UserIdentity.query.filter_by(
id=record.orcid, method='orcid').first()
user = RemoteAccount.query.filter_by(user_id=raw_user.id_user).first()
token = user.tokens[0].access_token
token = user.remote_tokens[0].access_token
api.remove_record(record.orcid, token, 'work', record.put_code)
with db.session.begin_nested():
db.session.delete(record)
Expand All @@ -114,7 +112,8 @@ def send_to_orcid(sender, api=None):
fetcher_name = current_app.config['ORCID_RECORDS_PID_FETCHER']
pid = current_pidstore.fetchers[fetcher_name](None, sender)
record_identifier = pid.pid_value
current_app.logger.info('Sending "{0}" to orcid.'.format(record_identifier))
current_app.logger.info('Sending "{0}" to orcid.'.format(
record_identifier))
try:
api = api or current_orcid.member
convert_to_orcid = import_string(
Expand Down Expand Up @@ -158,27 +157,19 @@ def send_to_orcid(sender, api=None):

def get_author_collection_records_from_valid_authors(authors_refs):
"""Query elasticsearch for the author of the given authors references."""
es_query = {
"filter": {
"bool": {
"must": [
{"terms": {
"self.$ref": authors_refs
}}, {"match": {
"ids.type": "ORCID"
}}
]
}
}
search_args = {
'self__$ref': authors_refs
}

return current_orcid.author_search.execute(es_query).hits.hits
query = current_orcid.author_search().query('match', ids__type='ORCID') \
.query('terms', **search_args)
return query.execute().hits


def get_orcid_valid_authors(record):
"""Return all the valid author-records from a record.
A valid author-rerord is one that contains an orcid id.
A valid author-rerord is one that contains an ORCID iD.
"""
authors_refs = []
for author in record['authors']:
Expand Down
22 changes: 19 additions & 3 deletions invenio_orcid/utils.py
Expand Up @@ -22,19 +22,35 @@

"""Implement helper functions."""

from flask import current_app

from invenio_oauthclient.models import RemoteAccount, UserIdentity

from werkzeug.utils import import_string


def get_authors_credentials(author_identifier, method='orcid'):
def get_authors_credentials(author, method='orcid'):
"""Return the access token for a specific author (if available).
:param author_identifier: The id of the author (e.g. the orcid-id).
:param author: An author record.
:param method: The service associated with the author_identifier.
"""
get_identifier = import_string(
current_app.config['ORCID_ID_FETCHER'])
author_identifier = get_identifier(author)
raw_user = UserIdentity.query.filter_by(
id=author_identifier, method=method).first()
user = RemoteAccount.query.filter_by(user_id=raw_user.id_user).first()
return user.tokens[0].access_token

return user.remote_tokens[0].access_token, author_identifier


def get_orcid_id(author):
"""Return the ORCID iD of a given author record.
:param author: An author record.
"""
return author['orcid']


def convert_to_orcid(record):
Expand Down

0 comments on commit 4e31f12

Please sign in to comment.