Exemplo n.º 1
0
def test_orcids_for_push_orcid_in_author_with_claim(author_in_isolated_app):
    record = {
        '_collections': ['Literature'],
        'authors': [
            {
                'full_name': 'No Orcid, Jimmy',
            },
            {
                'full_name': 'Smith, John',
                'ids': [
                    {
                        'schema': 'INSPIRE BAI',
                        'value': 'J.Smith.1',
                    },
                ],
                'record': get_record_ref(author_in_isolated_app, 'authors'),
                'curated_relation': True,
            },
        ],
        'document_type': ['article'],
        'titles': [
            {'title': 'An interesting paper'},
        ],
    }

    assert validate(record, 'hep') is None
    assert list(get_orcids_for_push(record)) == ['0000-0002-1825-0097']
Exemplo n.º 2
0
def test_orcids_for_push_orcid_in_author_with_claim(author_in_isolated_app):
    record = {
        "_collections": ["Literature"],
        "authors": [
            {
                "full_name": "No Orcid, Jimmy"
            },
            {
                "full_name": "Smith, John",
                "ids": [{
                    "schema": "INSPIRE BAI",
                    "value": "J.Smith.1"
                }],
                "record": get_record_ref(author_in_isolated_app),
                "curated_relation": True,
            },
        ],
        "document_type": ["article"],
        "titles": [{
            "title": "An interesting paper"
        }],
    }

    assert validate(record, "hep") is None
    assert list(get_orcids_for_push(record)) == ["0000-0002-1825-0097"]
Exemplo n.º 3
0
def match_reference(reference):
    """Match references given a reference metadata using InspireMatcher queires.

    Args:
        reference: The reference metadata
        config: The configurtaion(s) for InspireMatcher queries

    Returns:
        The record ID of the matched reference
    """

    config_default = current_app.config[
        'WORKFLOWS_REFERENCE_MATCHER_DEFAULT_CONFIG']
    config_jcap_and_jhep = current_app.config[
        'WORKFLOWS_REFERENCE_MATCHER_JHEP_AND_JCAP_CONFIG']

    journal_title = get_value(reference,
                              'reference.publication_info.journal_title')
    config = config_jcap_and_jhep if journal_title in ['JCAP', 'JHEP'
                                                       ] else config_default
    result = next(match(reference, config), None)
    if result:
        matched_recid = result['_source']['control_number']
        reference['record'] = get_record_ref(matched_recid, 'literature')
    return reference
Exemplo n.º 4
0
def test_orcids_for_push_orcid_in_author_with_claim(author_in_isolated_app):
    record = {
        '_collections': ['Literature'],
        'authors': [
            {
                'full_name': 'No Orcid, Jimmy',
            },
            {
                'full_name': 'Smith, John',
                'ids': [
                    {
                        'schema': 'INSPIRE BAI',
                        'value': 'J.Smith.1',
                    },
                ],
                'record': get_record_ref(author_in_isolated_app, 'authors'),
                'curated_relation': True,
            },
        ],
        'document_type': ['article'],
        'titles': [
            {
                'title': 'An interesting paper'
            },
        ],
    }

    assert validate(record, 'hep') is None
    assert list(get_orcids_for_push(record)) == ['0000-0002-1825-0097']
Exemplo n.º 5
0
def link_signature_to_author(signature_data, author_control_number):
    """Adds record/$ref of the given author to the given signature.

    Args:
        author_control_number (int): The control number of the author to which we want to link.
        signature_data (list): List containing 2 elements: the publication_id and the signature uuid.

    Returns:
        dict: The signature data from the publication with the linked author.
    """
    record = LiteratureRecord.get_record_by_pid_value(
        signature_data["publication_id"])
    signature = next(
        (author for author in record.get("authors")
         if author.get("uuid") == signature_data["signature_uuid"]),
        None,
    )
    if not signature or ("record" in signature
                         and signature.get("curated_relation")):
        return None

    if signature.get("curated_relation") and "record" not in signature:
        signature["curated_relation"] = False

    new_author_record = get_record_ref(author_control_number, "authors")
    if new_author_record == signature.get("record"):
        # no changes, avoid creating a new useless version of the record
        return None

    signature["record"] = new_author_record
    record.update(dict(record))
    return signature
Exemplo n.º 6
0
def test_get_record_ref_with_empty_server_name():
    config = {'SERVER_NAME': None}

    with patch.dict(current_app.config, config):
        expected = 'http://inspirehep.net/api/endpoint/123'
        result = get_record_ref(123, 'endpoint')

        assert expected == result['$ref']
Exemplo n.º 7
0
def test_get_record_ref_with_server_name_localhost():
    config = {'SERVER_NAME': 'localhost:5000'}

    with patch.dict(current_app.config, config):
        expected = 'http://localhost:5000/api/endpoint/123'
        result = get_record_ref(123, 'endpoint')

        assert expected == result['$ref']
Exemplo n.º 8
0
def test_get_record_ref_without_endpoint_defaults_to_record():
    config = {'SERVER_NAME': None}

    with patch.dict(current_app.config, config):
        expected = 'http://inspirehep.net/api/record/123'
        result = get_record_ref(123)

        assert expected == result['$ref']
Exemplo n.º 9
0
def test_get_record_ref_with_https_server_name():
    config = {'SERVER_NAME': 'https://example.com'}

    with patch.dict(current_app.config, config):
        expected = 'https://example.com/api/endpoint/123'
        result = get_record_ref(123, 'endpoint')

        assert expected == result['$ref']
Exemplo n.º 10
0
def assign_papers(from_author_recid, to_author_recid, literature_recids):
    author_signatures = []
    for record in get_literature_records_by_recid(literature_recids):
        from_author = get_author_by_recid(record, from_author_recid)
        from_author["record"] = get_record_ref(to_author_recid, endpoint="authors")
        from_author["curated_relation"] = True
        author_signatures.append(from_author)
        record.update(dict(record))
    return author_signatures
Exemplo n.º 11
0
def store_records(obj, eng):
    """Store the records involved in the manual merge.

    Performs the following steps:

        1. Updates the ``head`` so that it contains the result of the merge.
        2. Marks the ``update`` as merged with the ``head`` and deletes it.
        3. Populates the ``deleted_records`` and ``new_record`` keys in,
           respectively, ``head`` and ``update`` so that they contain a JSON
           reference to each other.

    Todo:
        The last step should be performed by the ``merge`` method itself.

    Args:
        obj: a workflow object.
        eng: a workflow engine.

    Returns:
        None

    """
    head_control_number = obj.extra_data['head_control_number']
    update_control_number = obj.extra_data['update_control_number']

    head = get_db_record('lit', head_control_number)
    update = get_db_record('lit', update_control_number)

    # 1. Updates the head so that it contains the result of the merge.
    head.clear()
    head.update(obj.data)
    # 2. Marks the update as merged with the head and deletes it.
    update.merge(head)
    update.delete()
    # 3. Populates the deleted_records and new_record keys.
    update['new_record'] = get_record_ref(head_control_number, 'literature')
    update_ref = get_record_ref(update_control_number, 'literature')
    head.setdefault('deleted_records', []).append(update_ref)

    head.commit()
    update.commit()
    db.session.commit()
Exemplo n.º 12
0
def assign_papers(
    from_author_recid, to_author_record, author_papers, is_stub_author=False
):
    author_bai = get_values_for_schema(to_author_record["ids"], "INSPIRE BAI")[0]
    for record in author_papers:
        lit_author = get_author_by_recid(record, from_author_recid)
        lit_author["record"] = get_record_ref(
            to_author_record["control_number"], endpoint="authors"
        )
        if not is_stub_author:
            lit_author["curated_relation"] = True
        lit_author["ids"] = update_author_bai(author_bai, lit_author)
        record.update(dict(record))
Exemplo n.º 13
0
def store_records(obj, eng):
    """Store the records involved in the manual merge.

    Update the head with the deleted record and send it to hep.

    Args:
        obj: a workflow object.
        eng: a workflow engine.

    Returns:
        None
    """
    head_control_number = obj.extra_data['head_control_number']
    update_control_number = obj.extra_data['update_control_number']
    head = dict(obj.data)

    update_ref = get_record_ref(update_control_number, 'literature')
    head.setdefault('deleted_records', []).append(update_ref)
    put_record_to_hep('lit', head_control_number, data=obj.data)
Exemplo n.º 14
0
def assign_papers(
    self,
    from_author_recid,
    to_author_record,
    author_papers_recids,
    is_stub_author=False,
):
    author_bai = get_values_for_schema(to_author_record["ids"],
                                       "INSPIRE BAI")[0]
    for recid in author_papers_recids:
        record = LiteratureRecord.get_record_by_pid_value(recid)
        lit_author = get_author_by_recid(record, from_author_recid)
        lit_author["record"] = get_record_ref(
            to_author_record["control_number"], endpoint="authors")
        if not is_stub_author:
            lit_author["curated_relation"] = True
        lit_author["ids"] = update_author_bai(author_bai, lit_author)
        record.update(dict(record))
    db.session.commit()
Exemplo n.º 15
0
def _add_match_to_reference(reference, matched_recid, es_index):
    """Modifies a reference to include its record id."""
    if es_index == "records-data":
        reference["record"] = get_record_ref(matched_recid, "data")
    elif es_index == "records-hep":
        reference["record"] = get_record_ref(matched_recid, "literature")
Exemplo n.º 16
0
def create_author(profile):
    """Create a new author profile based on a given signature.

    The method receives a dictionary representing an author.
    Based on the values, it creates a dictionary in the invenio_records format.
    After all the fields are processed, the method calls create_record
    from invenio_records.api to put the new record.

    :param profile:
        A signature representing an author's to be created as a profile.

        Example:
            profile = {u'affiliations': [{u'value': u'Yerevan Phys. Inst.'}],
                       u'alternative_name': None,
                       u'curated_relation': False,
                       u'email': None,
                       u'full_name': u'Chatrchyan, Serguei',
                       u'inspire_id': None,
                       u'orcid': None,
                       u'profile': u'',
                       u'recid': None,
                       u'role': None,
                       u'uuid': u'd63537a8-1df4-4436-b5ed-224da5b5028c'}

    :return:
        A recid, where the new profile can be accessed.

        Example:
            "1234"
    """
    name = profile.get('full_name')

    # Template of an initial record.
    record = {
        '$schema': _get_author_schema(),
        '_collections': ['Authors'],
        'name': {
            'value': name
        },
    }

    # The author's email address.
    # Unfortunately the method will not correlate a given e-mail address
    # with an affiliation.
    if 'email' in profile:
        email = profile.get('email')

        record['positions'] = []
        record['positions'].append({'email': email})

    # The author can be a member of more than one affiliation.
    if 'affiliations' in profile:
        affiliations = profile.get('affiliations')

        if 'positions' not in record:
            record['positions'] = []

        for affiliation in affiliations:
            name = affiliation.get('value')
            recid = affiliation.get('recid', None)

            if recid:
                record['positions'].append(
                    {'institution': {
                        'name': name,
                        'recid': recid
                    }})
            else:
                record['positions'].append({'institution': {'name': name}})

    # FIXME: The method should also collect the useful data
    #        from the publication, like category field, subject,
    #        etc.

    # Disconnect the signal on insert of a new record.
    after_record_insert.disconnect(append_new_record_to_queue)

    # Create a new author profile.
    record = InspireRecord.create(record, id_=None)

    # Create Inspire recid.
    record_pid = inspire_recid_minter(record.id, record)

    # Extend the new record with Inspire recid and self key.
    record['control_number'] = record_pid.pid_value
    record['self'] = inspire_dojson_utils.get_record_ref(
        record_pid.pid_value, 'authors')

    # Apply the changes.
    record.commit()
    db.session.commit()

    # Reconnect the disconnected signal.
    after_record_insert.connect(append_new_record_to_queue)

    # Report.
    logger.info("Created profile: %s", record_pid.pid_value)

    # Return the recid of new profile to which signatures will point to.
    return record_pid.pid_value
Exemplo n.º 17
0
def _add_match_to_reference(reference, matched_recid, es_index):
    """Modifies a reference to include its record id."""
    if es_index == 'records-data':
        reference['record'] = get_record_ref(matched_recid, 'data')
    elif es_index == 'records-hep':
        reference['record'] = get_record_ref(matched_recid, 'literature')
Exemplo n.º 18
0
def test_get_record_ref_without_recid_returns_none():
    assert get_record_ref(None, 'endpoint') is None
Exemplo n.º 19
0
def _add_match_to_reference(reference, matched_recid, es_index):
    """Modifies a reference to include its record id."""
    if es_index == 'records-data':
        reference['record'] = get_record_ref(matched_recid, 'data')
    elif es_index == 'records-hep':
        reference['record'] = get_record_ref(matched_recid, 'literature')
Exemplo n.º 20
0
def get_ref_from_pid(pid_type, pid_value):
    """Return full $ref for record with pid_type and pid_value"""
    return get_record_ref(pid_value,
                          PidStoreBase.get_endpoint_from_pid_type(pid_type))