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

Commit

Permalink
global: configurable field names
Browse files Browse the repository at this point in the history
* All the installation-based field names are now configurable.
  (addresses #4)

* Renaming functions argument `json` to `data` (closes #8).

Signed-off-by: Panos Paparrigopoulos <panos.paparrigopoulos@cern.ch>
  • Loading branch information
Panos512 committed Sep 28, 2016
1 parent 99c0432 commit f8c4e71
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 35 deletions.
30 changes: 18 additions & 12 deletions invenio_orcid/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,21 @@

ORCID_JSON_CONVERTER_MODULE = 'invenio_orcid.utils:convert_to_orcid'

ORCID_WORK_TYPES = {
'book': 'BOOK',
'conferencepaper': 'CONFERENCE_PAPER',
'proceedings': 'BOOK',
'preprint': 'WORKING_PAPER',
'note': 'WORKING_PAPER',
'published': 'JOURNAL_ARTICLE',
'thesis': 'DISSERTATION',
'lectures': 'LECTURE_SPEECH',
'bookchapter': 'BOOK_CHAPTER',
'report': 'REPORT',
}
ORCID_AUTHORS_SEARCH_CLASS = 'invenio_search:RecordsSearch'

ORCID_RECORDS_PID_TYPE = 'records'
ORCID_RECORDS_DOC_TYPE = 'records'
ORCID_RECORDS_PID_FETCHER = 'recid_fetcher'

ORCID_WORK_TYPES = {}
"""Mapping to ORCID work types.
Example:
.. code-block:: python
ORCID_WORK_TYPES = {
'conferencepaper': 'CONFERENCE_PAPER',
'proceedings': 'BOOK',
}
"""
12 changes: 11 additions & 1 deletion invenio_orcid/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
from __future__ import absolute_import, print_function

import orcid
from werkzeug.utils import cached_property
import six
from werkzeug.utils import cached_property, import_string

from . import config

Expand Down Expand Up @@ -59,6 +60,15 @@ def member(self):
orcid_consumer_secret, orcid_consumer_key, sandbox=sandbox
)

@cached_property
def author_search(self):
"""Return a cache instance."""
author_search = self.app.config.get('ORCID_AUTHORS_SEARCH_CLASS')

return import_string(author_search) \
if isinstance(author_search, six.string_types) \
else author_search


class InvenioORCID(object):
"""Invenio-ORCID extension."""
Expand Down
57 changes: 35 additions & 22 deletions invenio_orcid/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
from flask import current_app
from invenio_db import db
from invenio_oauthclient.models import RemoteAccount, UserIdentity
from invenio_pidstore import current_pidstore
from invenio_pidstore.resolver import Resolver
from invenio_search import current_search_client
from invenio_search.utils import schema_to_index
from requests import RequestException
from werkzeug.utils import import_string
Expand All @@ -40,16 +40,20 @@
logger = get_task_logger(__name__)


def prepare_authors_data_for_pushing_to_orcid(json):
def prepare_authors_data_for_pushing_to_orcid(data):
"""Extract the authors with valid ORCID credentials.
It uses the list of authors from a given record in JSON format.
"""
resolver = Resolver(pid_type='literature',
pid_type = current_app.config['ORCID_RECORDS_PID_TYPE']
resolver = Resolver(pid_type=pid_type,
object_type='rec', getter=lambda x: x)
record_id = resolver.resolve(json.get('control_number'))[
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
authors = get_orcid_valid_authors(json)
authors = get_orcid_valid_authors(data)
token = None
author_orcid = ''
authors_with_orcid_credentials = []
Expand All @@ -76,9 +80,13 @@ def prepare_authors_data_for_pushing_to_orcid(json):
def delete_from_orcid(sender, api=None):
"""Delete a record from orcid."""
api = api or current_orcid.member
resolver = Resolver(pid_type='literature',
pid_type = current_app.config['ORCID_RECORDS_PID_TYPE']
resolver = Resolver(pid_type=pid_type,
object_type='rec', getter=lambda x: x)
record_id = resolver.resolve(sender.get('control_number'))[
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
records = ORCIDRecords.query.filter_by(record_id=record_id).all()
for record in records:
Expand All @@ -95,14 +103,19 @@ def delete_from_orcid(sender, api=None):
def doc_type_should_be_sent_to_orcid(record):
"""Return ``True`` is a document type should be sent to ORCID."""
index, doc_type = schema_to_index(record['$schema'])
return doc_type == 'hep'
main_doc_type = current_app.config['ORCID_RECORDS_DOC_TYPE']
return doc_type == main_doc_type


@shared_task(ignore_result=True)
def send_to_orcid(sender, api=None):
"""Send records to orcid."""
if doc_type_should_be_sent_to_orcid(sender):
logger.info("Sending " + sender.get('control_number') + " to orcid.")
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(
sender.get(record_identifier))
try:
api = api or current_orcid.member
convert_to_orcid = import_string(
Expand All @@ -128,16 +141,20 @@ def send_to_orcid(sender, api=None):
else:
api.update_record(author_orcid, token,
'work', orcid_json, str(put_code))
logger.info("Succersfully sent " +
sender.get('control_number') + " to orcid.")
current_app.logger.info(
'Succersfully sent "{0}" to orcid.').format(
sender.get(record_identifier))
except RequestException as e:
print(e.response.text, sender['control_number'])
logger.info("Failed to push " +
sender.get('control_number') + " to orcid.")
current_app.logger.info(
e.response.text, sender[record_identifier])
current_app.logger.info(
'Failed to push "{0}" to orcid.').format(
sender.get(record_identifier))
continue
except (KeyError, AttributeError, TypeError) as e:
logger.info("Failed to convert " +
sender.get('control_number') + " to orcid.")
current_app.logger.info.info(
'Failed to convert "{0}" to orcid.').format(
sender.get(record_identifier))


def get_author_collection_records_from_valid_authors(authors_refs):
Expand All @@ -155,12 +172,8 @@ def get_author_collection_records_from_valid_authors(authors_refs):
}
}
}
authors = current_search_client.search(
index='records-authors',
doc_type='authors',
body=es_query
)['hits']['hits']
return authors

return current_orcid.author_search.execute(es_query).hits.hits


def get_orcid_valid_authors(record):
Expand Down

0 comments on commit f8c4e71

Please sign in to comment.