Skip to content

Commit

Permalink
general: upstream invenio-pidstore
Browse files Browse the repository at this point in the history
* No longer uses INSPIRE fork of invenio-pidstore.

* Introduces new get_pid_type_for() and get_endpoint_from_pid_type()
  helpers to map between endpoints and the corresponding pid_types,
  now that pid_types are constrained in length. (closes inspirehep#1579)

Signed-off-by: Samuele Kaplun <[email protected]>
  • Loading branch information
kaplun committed Oct 11, 2016
1 parent a2b8814 commit 02b4651
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 57 deletions.
50 changes: 29 additions & 21 deletions inspirehep/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,8 @@ def _(x):
# =======
RECORDS_REST_ENDPOINTS = dict(
literature=dict(
pid_type='literature',
pid_type='lit',
default_endpoint_prefix=True,
pid_minter='inspire_recid_minter',
pid_fetcher='inspire_recid_fetcher',
search_class='inspirehep.modules.search:LiteratureSearch',
Expand Down Expand Up @@ -420,7 +421,7 @@ def _(x):
record_class='inspirehep.modules.records.es_record:ESRecord'
),
literature_db=dict(
pid_type='literature',
pid_type='lit',
search_class='inspirehep.modules.search:LiteratureSearch',
record_serializers={
'application/json': ('invenio_records_rest.serializers'
Expand All @@ -437,7 +438,8 @@ def _(x):

),
authors=dict(
pid_type='authors',
pid_type='aut',
default_endpoint_prefix=True,
pid_minter='inspire_recid_minter',
pid_fetcher='inspire_recid_fetcher',
search_class='inspirehep.modules.search:AuthorsSearch',
Expand All @@ -460,7 +462,7 @@ def _(x):
search_factory_imp='inspirehep.modules.search.query:inspire_search_factory',
),
authors_citations=dict(
pid_type='authors',
pid_type='aut',
pid_minter='inspire_recid_minter',
pid_fetcher='inspire_recid_fetcher',
search_class='inspirehep.modules.search:AuthorsSearch',
Expand All @@ -484,7 +486,7 @@ def _(x):
':inspire_search_factory'),
),
authors_coauthors=dict(
pid_type='authors',
pid_type='aut',
pid_minter='inspire_recid_minter',
pid_fetcher='inspire_recid_fetcher',
search_class='inspirehep.modules.search:AuthorsSearch',
Expand All @@ -508,7 +510,7 @@ def _(x):
':inspire_search_factory'),
),
authors_publications=dict(
pid_type='authors',
pid_type='aut',
pid_minter='inspire_recid_minter',
pid_fetcher='inspire_recid_fetcher',
search_class='inspirehep.modules.search:AuthorsSearch',
Expand All @@ -532,7 +534,7 @@ def _(x):
':inspire_search_factory'),
),
authors_stats=dict(
pid_type='authors',
pid_type='aut',
pid_minter='inspire_recid_minter',
pid_fetcher='inspire_recid_fetcher',
search_class='inspirehep.modules.search:AuthorsSearch',
Expand All @@ -556,7 +558,8 @@ def _(x):
':inspire_search_factory'),
),
data=dict(
pid_type='data',
pid_type='dat',
default_endpoint_prefix=True,
pid_minter='inspire_recid_minter',
pid_fetcher='inspire_recid_fetcher',
search_class='inspirehep.modules.search:DataSearch',
Expand All @@ -579,7 +582,8 @@ def _(x):
search_factory_imp='inspirehep.modules.search.query:inspire_search_factory',
),
conferences=dict(
pid_type='conferences',
pid_type='con',
default_endpoint_prefix=True,
pid_minter='inspire_recid_minter',
pid_fetcher='inspire_recid_fetcher',
search_class='inspirehep.modules.search:ConferencesSearch',
Expand All @@ -602,7 +606,8 @@ def _(x):
search_factory_imp='inspirehep.modules.search.query:inspire_search_factory',
),
jobs=dict(
pid_type='jobs',
pid_type='job',
default_endpoint_prefix=True,
pid_minter='inspire_recid_minter',
pid_fetcher='inspire_recid_fetcher',
search_class='inspirehep.modules.search:JobsSearch',
Expand All @@ -625,7 +630,8 @@ def _(x):
search_factory_imp='inspirehep.modules.search.query:inspire_search_factory',
),
institutions=dict(
pid_type='institutions',
pid_type='ins',
default_endpoint_prefix=True,
pid_minter='inspire_recid_minter',
pid_fetcher='inspire_recid_fetcher',
search_class='inspirehep.modules.search:InstitutionsSearch',
Expand All @@ -648,7 +654,8 @@ def _(x):
search_factory_imp='inspirehep.modules.search.query:inspire_search_factory',
),
experiments=dict(
pid_type='experiments',
pid_type='exp',
default_endpoint_prefix=True,
pid_minter='inspire_recid_minter',
pid_fetcher='inspire_recid_fetcher',
search_class='inspirehep.modules.search:ExperimentsSearch',
Expand All @@ -671,7 +678,8 @@ def _(x):
search_factory_imp='inspirehep.modules.search.query:inspire_search_factory',
),
journals=dict(
pid_type='journals',
pid_type='jou',
default_endpoint_prefix=True,
pid_minter='inspire_recid_minter',
pid_fetcher='inspire_recid_fetcher',
search_class='inspirehep.modules.search:JournalsSearch',
Expand Down Expand Up @@ -701,50 +709,50 @@ def _(x):

RECORDS_UI_ENDPOINTS = dict(
literature=dict(
pid_type='literature',
pid_type='lit',
route='/literature/<pid_value>',
template='inspirehep_theme/format/record/'
'Inspire_Default_HTML_detailed.tpl',
record_class='inspirehep.modules.records.wrappers:LiteratureRecord',
),
authors=dict(
pid_type='authors',
pid_type='aut',
route='/authors/<pid_value>',
template='inspirehep_theme/format/record/'
'authors/Author_HTML_detailed.html',
record_class='inspirehep.modules.records.wrappers:AuthorsRecord',
),
data=dict(
pid_type='data',
pid_type='dat',
route='/data/<pid_value>',
template='inspirehep_theme/format/record/Data_HTML_detailed.tpl'
),
conferences=dict(
pid_type='conferences',
pid_type='con',
route='/conferences/<pid_value>',
template='inspirehep_theme/format/record/Conference_HTML_detailed.tpl',
record_class='inspirehep.modules.records.wrappers:ConferencesRecord',
),
jobs=dict(
pid_type='jobs',
pid_type='job',
route='/jobs/<pid_value>',
template='inspirehep_theme/format/record/Job_HTML_detailed.tpl',
record_class='inspirehep.modules.records.wrappers:JobsRecord',
),
institutions=dict(
pid_type='institutions',
pid_type='ins',
route='/institutions/<pid_value>',
template='inspirehep_theme/format/record/Institution_HTML_detailed.tpl',
record_class='inspirehep.modules.records.wrappers:InstitutionsRecord',
),
experiments=dict(
pid_type='experiments',
pid_type='exp',
route='/experiments/<pid_value>',
template='inspirehep_theme/format/record/Experiment_HTML_detailed.tpl',
record_class='inspirehep.modules.records.wrappers:ExperimentsRecord',
),
journals=dict(
pid_type='journals',
pid_type='jou',
route='/journals/<pid_value>',
template='inspirehep_theme/format/record/Journal_HTML_detailed.tpl',
record_class='inspirehep.modules.records.wrappers:JournalsRecord',
Expand Down
27 changes: 22 additions & 5 deletions inspirehep/modules/pidstore/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,17 @@
import requests
from flask import current_app

from invenio_records_rest.proxies import current_records_rest
from invenio_pidstore.models import PIDStatus, RecordIdentifier

from invenio_pidstore.providers.base import BaseProvider

from inspirehep.config import RECORDS_UI_ENDPOINTS


_PID_TYPE_TO_ENDPOINT = {value['pid_type']: key
for key, value in RECORDS_UI_ENDPOINTS.items()
if '_' not in key}


def _get_next_pid_from_legacy():
"""Reserve the next pid on legacy.
Expand Down Expand Up @@ -84,8 +91,18 @@ def create(cls, object_type=None, object_uuid=None, **kwargs):

@staticmethod
def schema_to_pid_type(schema):
pid_type = os.path.splitext(os.path.basename(schema))[0]
if pid_type == 'hep':
endpoint = os.path.splitext(os.path.basename(schema))[0]
if endpoint == 'hep':
# FIXME: temp hack until we rename hep.json to literature.json
return 'literature'
return pid_type
endpoint = 'literature'
return get_pid_type_for(endpoint)


def get_pid_type_for(endpoint):
"""Get the pid_type code corresponding to the recid of the given endpoint."""
return RECORDS_UI_ENDPOINTS[endpoint]['pid_type']


def get_endpoint_from_pid_type(pid_type):
"""Return the endpoint corresponding to the given pid_type."""
return current_records_rest.default_endpoint_prefixes[pid_type]
11 changes: 7 additions & 4 deletions inspirehep/modules/theme/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
from invenio_pidstore.models import PersistentIdentifier
from invenio_search import current_search_client

from inspirehep.modules.pidstore.providers import get_pid_type_for, get_endpoint_from_pid_type
from inspirehep.modules.records.conference_series import (
CONFERENCE_CATEGORIES_TO_SERIES,
)
Expand Down Expand Up @@ -226,7 +227,7 @@ def ajax_references():
recid = request.args.get('recid', '')
collection = request.args.get('collection', '')

pid = PersistentIdentifier.get(collection, recid)
pid = PersistentIdentifier.get(get_pid_type_for(collection), recid)

record = LiteratureSearch().get_source(pid.object_uuid)

Expand All @@ -244,7 +245,7 @@ def ajax_citations():
recid = request.args.get('recid', '')
collection = request.args.get('collection', '')

pid = PersistentIdentifier.get(collection, recid)
pid = PersistentIdentifier.get(get_pid_type_for(collection), recid)

record = LiteratureSearch().get_source(pid.object_uuid)

Expand Down Expand Up @@ -471,7 +472,7 @@ def ajax_institutions_experiments():
"""Datatable handler to get experiments in an institution."""
recid = request.args.get('recid', '')

pid = PersistentIdentifier.get('institutions', recid)
pid = PersistentIdentifier.get(get_pid_type_for('institutions'), recid)

record = InstitutionsSearch().get_source(pid.object_uuid)
try:
Expand Down Expand Up @@ -570,7 +571,9 @@ def record(control_number):
abort(404)

return redirect('/{collection}/{control_number}'.format(
collection=pid.pid_type, control_number=control_number)), 301
collection=get_endpoint_from_pid_type(pid.pid_type),
control_number=control_number
)), 301


#
Expand Down
10 changes: 6 additions & 4 deletions inspirehep/utils/record_getter.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
from invenio_pidstore.models import PersistentIdentifier
from invenio_records.api import Record

from inspirehep.modules.pidstore.providers import get_pid_type_for, get_endpoint_from_pid_type


class RecordGetterError(Exception):

Expand Down Expand Up @@ -63,7 +65,7 @@ def wrapper(*args, **kwargs):

@raise_record_getter_error_and_log
def get_es_record(record_type, recid, **kwargs):
pid = PersistentIdentifier.get(record_type, recid)
pid = PersistentIdentifier.get(get_pid_type_for(record_type), recid)
search_conf = current_app.config['RECORDS_REST_ENDPOINTS'][record_type]
search_class = import_string(search_conf['search_class'])()
return search_class.get_source(pid.object_uuid, **kwargs)
Expand All @@ -80,7 +82,7 @@ def get_es_records(record_type, recids, **kwargs):
"""
uuids = PersistentIdentifier.query.filter(
PersistentIdentifier.pid_value.in_(recids),
PersistentIdentifier.pid_type == record_type
PersistentIdentifier.pid_type == get_pid_type_for(record_type)
).all()
uuids = [str(uuid.object_uuid) for uuid in uuids]
search_conf = current_app.config['RECORDS_REST_ENDPOINTS'][record_type]
Expand All @@ -91,12 +93,12 @@ def get_es_records(record_type, recids, **kwargs):
@raise_record_getter_error_and_log
def get_es_record_by_uuid(uuid):
pid = PersistentIdentifier.query.filter_by(object_uuid=uuid).one()
search_conf = current_app.config['RECORDS_REST_ENDPOINTS'][pid.pid_type]
search_conf = current_app.config['RECORDS_REST_ENDPOINTS'][get_endpoint_from_pid_type(pid.pid_type)]
search_class = import_string(search_conf['search_class'])()
return search_class.get_source(uuid)


@raise_record_getter_error_and_log
def get_db_record(record_type, recid):
pid = PersistentIdentifier.get(record_type, recid)
pid = PersistentIdentifier.get(get_pid_type_for(record_type), recid)
return Record.get_record(pid.object_uuid)
4 changes: 1 addition & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@

# FIXME temporary branch for testing
-e git+https://github.com/inspirehep/invenio-query-parser.git@invenio3-inspire#egg=invenio-query-parser==0.6.0

# INSPIRE forks of Invenio packages
-e git+https://github.com/inspirehep/invenio-pidstore.git#egg=invenio-pidstore
-e git+https://github.com/inveniosoftware/invenio-records-rest.git#egg=invenio-records-rest

# Workflows and Holding Pen related dependencies
-e git+https://github.com/inveniosoftware/invenio-classifier.git#egg=invenio-classifier
Expand Down
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@
'invenio-mail>=1.0.0a4',
'invenio-oauthclient>=1.0.0a8',
'invenio-orcid>=1.0.0a1',
'invenio-pidstore>=1.0.0a9',
'invenio-records>=1.0.0a16', # Add [versioning] in the future
'invenio-records-rest>=1.0.0a16',
'invenio-rest[cors]>=1.0.0a7',
'invenio-search>=1.0.0a7',
'invenio-records-rest>=1.0.0a15',
'invenio-records-rest>=1.0.0a16',
'invenio-records-ui>=1.0.0a6',
'invenio-files-rest>=1.0.0a3',
'invenio-records-files>=1.0.0a5',
Expand Down Expand Up @@ -215,6 +217,7 @@
'crossref = inspirehep.modules.crossref:CrossRef',
'inspire_orcid = inspirehep.modules.orcid:INSPIREOrcid',
'inspire_disambiguation = inspirehep.modules.disambiguation:InspireDisambiguation',
'invenio_records_rest = invenio_records_rest:InvenioRecordsREST',
],
'invenio_assets.bundles': [
'inspirehep_theme_css = inspirehep.modules.theme.bundles:css',
Expand Down
7 changes: 4 additions & 3 deletions tests/integration/disambiguation/test_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from invenio_search import current_search_client as es

from inspirehep.modules.disambiguation.models import DisambiguationRecord
from inspirehep.modules.pidstore.providers import get_pid_type_for
from inspirehep.utils.record_getter import get_es_record_by_uuid


Expand All @@ -42,7 +43,7 @@ def test_count_phonetic_block_dispatched(small_app):

# Signature #1.
glashow_record_id = str(PersistentIdentifier.get(
"literature", 4328).object_uuid)
get_pid_type_for("literature"), 4328).object_uuid)
glashow_record = get_es_record_by_uuid(glashow_record_id)

# Add phonetic block to the record.
Expand All @@ -53,7 +54,7 @@ def test_count_phonetic_block_dispatched(small_app):

# Signature #2.
higgs_record_id_first = str(PersistentIdentifier.get(
"literature", 1358492).object_uuid)
get_pid_type_for("literature"), 1358492).object_uuid)
higgs_record_first = get_es_record_by_uuid(higgs_record_id_first)

# Add phonetic block to the record.
Expand All @@ -64,7 +65,7 @@ def test_count_phonetic_block_dispatched(small_app):

# Signature #3.
higgs_record_id_second = str(PersistentIdentifier.get(
"literature", 11883).object_uuid)
get_pid_type_for("literature"), 11883).object_uuid)
higgs_record_second = get_es_record_by_uuid(higgs_record_id_second)

# Add phonetic block to the record.
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/disambiguation/test_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@
from invenio_pidstore.models import PersistentIdentifier

from inspirehep.modules.disambiguation.logic import _create_distance_signature
from inspirehep.modules.pidstore.providers import get_pid_type_for


def test_create_distance_signature_method(small_app):
"""Test the method responsible for creating data in Beard format."""
pid = PersistentIdentifier.get("literature", 4328)
pid = PersistentIdentifier.get(get_pid_type_for("literature"), 4328)
publication_id = str(pid.object_uuid)

signatures_map = {
Expand Down
Loading

0 comments on commit 02b4651

Please sign in to comment.