Пример #1
0
def update_authors_recid(record_id, uuid, profile_recid):
    """Update author profile for a given signature.

    The method receives UUIDs representing record and signature
    respectively together with an author profile recid.
    The new recid will be placed in the signature with the given
    UUID.

    :param record_id:
        A string representing UUID of a given record.

        Example:
            record_id = "a5afb151-8f75-4e91-8dc1-05e7e8e8c0b8"

    :param uuid:
        A string representing UUID of a given signature.

        Example:
            uuid = "c2f432bd-2f52-4c16-ac66-096f168c762f"

    :param profile_recid:
        A string representing author profile recid, that
        updated signature should point to.

        Example:
            profile_recid = "1"
    """
    try:
        record = Record.get_record(record_id)
        update_flag = False

        for author in record['authors']:
            if author['uuid'] == uuid:
                author['recid'] = str(profile_recid)
                update_flag = True

        if update_flag:
            # Disconnect the signal on insert of a new record.
            before_record_index.disconnect(append_updated_record_to_queue)

            # Update the record in the database.
            record.commit()
            db.session.commit()

            # Update the record in Elasticsearch.
            indexer = RecordIndexer()
            indexer.index_by_id(record.id)
    except StaleDataError as exc:
        raise update_authors_recid.retry(exc=exc)
    finally:
        # Reconnect the disconnected signal.
        before_record_index.connect(append_updated_record_to_queue)

    # Report.
    logger.info("Updated signature %s with profile %s",
                uuid, profile_recid)
Пример #2
0
def update_authors_recid(record_id, uuid, profile_recid):
    """Update author profile for a given signature.

    The method receives UUIDs representing record and signature
    respectively together with an author profile recid.
    The new recid will be placed in the signature with the given
    UUID.

    :param record_id:
        A string representing UUID of a given record.

        Example:
            record_id = "a5afb151-8f75-4e91-8dc1-05e7e8e8c0b8"

    :param uuid:
        A string representing UUID of a given signature.

        Example:
            uuid = "c2f432bd-2f52-4c16-ac66-096f168c762f"

    :param profile_recid:
        A string representing author profile recid, that
        updated signature should point to.

        Example:
            profile_recid = "1"
    """
    try:
        record = InspireRecord.get_record(record_id)
        update_flag = False

        for author in record['authors']:
            if author['uuid'] == uuid:
                author['recid'] = str(profile_recid)
                update_flag = True

        if update_flag:
            # Disconnect the signal on insert of a new record.
            before_record_index.disconnect(append_updated_record_to_queue)

            # Update the record in the database.
            record.commit()
            db.session.commit()
    except StaleDataError as exc:
        raise update_authors_recid.retry(exc=exc)
    finally:
        # Reconnect the disconnected signal.
        before_record_index.connect(append_updated_record_to_queue)

    # Report.
    logger.info("Updated signature %s with profile %s", uuid, profile_recid)
Пример #3
0
def test_before_record_index_dynamic_connect(app):
    """Test before_record_index.dynamic_connect."""
    with app.app_context():
        with patch("invenio_records.api._records_state.validate"):
            auth_record = Record.create(
                {
                    "$schema": "/records/authorities/authority-v1.0.0.json",
                    "title": "Test",
                }
            )
            bib_record = Record.create(
                {
                    "$schema": "/records/bibliographic/bibliographic-v1.0.0.json",
                    "title": "Test",
                }
            )
            db.session.commit()

        def _simple(sender, json=None, **kwargs):
            json["simple"] = "simple"

        def _custom(sender, json=None, **kwargs):
            json["custom"] = "custom"

        def _cond(sender, connect_kwargs, index=None, **kwargs):
            return "bibliographic" in index

        _receiver1 = before_record_index.dynamic_connect(
            _simple, index="records-authorities-authority-v1.0.0"
        )
        _receiver2 = before_record_index.dynamic_connect(_custom, condition_func=_cond)

        action = RecordIndexer()._index_action(dict(id=str(auth_record.id), op="index"))
        assert "title" in action["_source"]
        assert action["_source"]["simple"] == "simple"

        action = RecordIndexer()._index_action(
            dict(id=str(bib_record.id), index="foo", op="index")
        )
        assert "title" in action["_source"]
        assert action["_source"]["custom"] == "custom"

        before_record_index.disconnect(_receiver1)
        before_record_index.disconnect(_receiver2)
Пример #4
0
def test_before_record_index_dynamic_connect(app):
    """Test before_record_index.dynamic_connect."""
    with app.app_context():
        with patch('invenio_records.api.Record.validate'):
            auth_record = Record.create({
                '$schema': '/records/authorities/authority-v1.0.0.json',
                'title': 'Test'
            })
            bib_record = Record.create({
                '$schema': '/records/bibliographic/bibliographic-v1.0.0.json',
                'title': 'Test'
            })
            db.session.commit()

        def _simple(sender, json=None, **kwargs):
            json['simple'] = 'simple'

        def _custom(sender, json=None, **kwargs):
            json['custom'] = 'custom'

        def _cond(sender, connect_kwargs, index=None, **kwargs):
            return 'bibliographic' in index

        _receiver1 = before_record_index.dynamic_connect(
            _simple, index='records-authorities-authority-v1.0.0')
        _receiver2 = before_record_index.dynamic_connect(_custom,
                                                         condition_func=_cond)

        action = RecordIndexer()._index_action(
            dict(id=str(auth_record.id), op='index'))
        assert 'title' in action['_source']
        assert action['_source']['simple'] == 'simple'

        action = RecordIndexer()._index_action(
            dict(id=str(bib_record.id), index='foo', op='index'))
        assert 'title' in action['_source']
        assert action['_source']['custom'] == 'custom'

        before_record_index.disconnect(_receiver1)
        before_record_index.disconnect(_receiver2)