Exemplo n.º 1
0
def record_upsert(json):
    """Insert or update a record."""
    control_number = json.get('control_number', json.get('recid'))
    if control_number:
        control_number = int(control_number)
        pid_type = InspireRecordIdProvider.schema_to_pid_type(json['$schema'])
        try:
            pid = PersistentIdentifier.get(pid_type, control_number)
            record = Record.get_record(pid.object_uuid)
            record.update(json)
            record.commit()
        except PIDDoesNotExistError:
            record = Record.create(json, id_=None)
            # Create persistent identifier.
            inspire_recid_minter(str(record.id), json)

        if json.get('deleted'):
            new_recid = get_recid_from_ref(json.get('new_record'))
            if new_recid:
                merged_record = get_db_record(pid_type, new_recid)
                merge_pidstores_of_two_merged_records(merged_record.id, record.id)
            else:
                soft_delete_pidstore_for_record(record.id)

        return record
Exemplo n.º 2
0
def record_upsert(json):
    """Insert or update a record."""
    control_number = json.get('control_number', json.get('recid'))
    if control_number:
        control_number = int(control_number)
        pid_type = InspireRecordIdProvider.schema_to_pid_type(json['$schema'])
        try:
            pid = PersistentIdentifier.get(pid_type, control_number)
            record = Record.get_record(pid.object_uuid)
            record.update(json)
            record.commit()
        except PIDDoesNotExistError:
            record = Record.create(json, id_=None)
            # Create persistent identifier.
            inspire_recid_minter(str(record.id), json)

        return record
Exemplo n.º 3
0
def record_upsert(json):
    """Insert or update a record."""
    control_number = json.get('control_number', json.get('recid'))
    if control_number:
        control_number = int(control_number)
        pid_type = InspireRecordIdProvider.schema_to_pid_type(json['$schema'])
        try:
            pid = PersistentIdentifier.get(pid_type, control_number)
            record = Record.get_record(pid.object_uuid)
            record.update(json)
            record.commit()
        except PIDDoesNotExistError:
            record = Record.create(json, id_=None)
            # Create persistent identifier.
            inspire_recid_minter(str(record.id), json)

        return record
Exemplo n.º 4
0
def check_if_record_is_going_to_be_deleted(sender, *args, **kwargs):
    """Checks if 'deleted' field is set as True before updating.

    If 'deleted' field exists and its value is True, before update,
    then delete all the record's pidstores.
    """
    control_number = int(sender.get('control_number'))
    collection = InspireRecordIdProvider.schema_to_pid_type(sender.get('$schema'))
    record = get_db_record(collection, control_number)

    if sender.get('deleted'):
        new_recid = get_recid_from_ref(sender.get('new_record'))
        if new_recid:
            merged_record = get_db_record(collection, new_recid)
            merge_pidstores_of_two_merged_records(merged_record.id, record.id)
        else:
            record = get_db_record(collection, control_number)
            soft_delete_pidstore_for_record(record.id)
Exemplo n.º 5
0
def test_getting_next_recid_from_legacy(httpretty_mock, app):
    extra_config = {
        'LEGACY_PID_PROVIDER': 'http://server/batchuploader/allocaterecord',
    }

    with app.app_context():
        with mock.patch.dict(app.config, extra_config):
            httpretty.register_uri(
                httpretty.GET,
                "http://server/batchuploader/allocaterecord",
                content_type="application/json",
                body=u'3141592',
                status=200)

            args = dict(object_type="rec",
                        object_uuid="7753a30b-4c4b-469c-8d8d-d5020069b3ab",
                        pid_type="literature")
            provider = InspireRecordIdProvider.create(**args)

            assert str(provider.pid.pid_value) == "3141592"
Exemplo n.º 6
0
def test_getting_next_recid_from_legacy(app):
    extra_config = {
        'LEGACY_PID_PROVIDER': 'http://server/batchuploader/allocaterecord',
    }

    with mock.patch.dict(current_app.config, extra_config):
        httpretty.register_uri(
            httpretty.GET,
            'http://server/batchuploader/allocaterecord',
            content_type='application/json',
            body='3141592',
            status=200,
        )

        args = dict(object_type='rec',
                    object_uuid='7753a30b-4c4b-469c-8d8d-d5020069b3ab',
                    pid_type='lit')
        provider = InspireRecordIdProvider.create(**args)

        assert str(provider.pid.pid_value) == "3141592"
Exemplo n.º 7
0
def test_getting_next_recid_from_legacy(app):
    extra_config = {
        'LEGACY_PID_PROVIDER': 'http://server/batchuploader/allocaterecord',
    }

    with mock.patch.dict(current_app.config, extra_config):
        with requests_mock.Mocker() as requests_mocker:
            requests_mocker.register_uri(
                'GET',
                'http://server/batchuploader/allocaterecord',
                text='3141592',
                headers={'content-type': 'application/json'},
                status_code=200,
            )

            args = dict(object_type='rec',
                        object_uuid='7753a30b-4c4b-469c-8d8d-d5020069b3ab',
                        pid_type='lit')
            provider = InspireRecordIdProvider.create(**args)

            assert str(provider.pid.pid_value) == '3141592'
Exemplo n.º 8
0
def test_getting_next_recid_from_legacy(httpretty_mock, app):
    extra_config = {
        'LEGACY_PID_PROVIDER': 'http://server/batchuploader/allocaterecord',
    }

    with app.app_context():
        with mock.patch.dict(app.config, extra_config):
            httpretty.register_uri(
                httpretty.GET,
                "http://server/batchuploader/allocaterecord",
                content_type="application/json",
                body=u'3141592',
                status=200)

            args = dict(
                object_type="rec",
                object_uuid="7753a30b-4c4b-469c-8d8d-d5020069b3ab",
                pid_type="literature"
            )
            provider = InspireRecordIdProvider.create(**args)

            assert str(provider.pid.pid_value) == "3141592"
Exemplo n.º 9
0
def test_getting_next_recid_from_legacy(app):
    extra_config = {
        'LEGACY_PID_PROVIDER': 'http://server/batchuploader/allocaterecord',
    }

    with mock.patch.dict(current_app.config, extra_config):
        with requests_mock.Mocker() as requests_mocker:
            requests_mocker.register_uri(
                'GET', 'http://server/batchuploader/allocaterecord',
                text='3141592',
                headers={'content-type': 'application/json'},
                status_code=200,
            )

            args = dict(
                object_type='rec',
                object_uuid='7753a30b-4c4b-469c-8d8d-d5020069b3ab',
                pid_type='lit'
            )
            provider = InspireRecordIdProvider.create(**args)

            assert str(provider.pid.pid_value) == '3141592'