Exemplo n.º 1
0
def record_insert_or_replace(json, skip_files=False):
    """Insert or replace a record."""
    pid_type = get_pid_type_from_schema(json['$schema'])
    control_number = json['control_number']

    try:
        pid = PersistentIdentifier.get(pid_type, control_number)
        record = InspireRecord.get_record(pid.object_uuid)
        record.clear()
        record.update(json, skip_files=skip_files)
        if json.get('legacy_creation_date'):
            record.model.created = datetime.strptime(
                json['legacy_creation_date'], '%Y-%m-%d')
        record.commit()
    except PIDDoesNotExistError:
        record = InspireRecord.create(json, id_=None, skip_files=skip_files)
        if json.get('legacy_creation_date'):
            record.model.created = datetime.strptime(
                json['legacy_creation_date'], '%Y-%m-%d')
        inspire_recid_minter(str(record.id), json)

    if json.get('deleted'):
        new_recid = get_recid_from_ref(json.get('new_record'))
        if not new_recid:
            record.delete()

    return record
Exemplo n.º 2
0
def _create_and_index_record(record):
    record = InspireRecord.create(record)
    inspire_recid_minter(record.id, record)
    db.session.commit()
    es.indices.refresh('records-hep')

    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)

        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.º 4
0
def _create_and_index_record(record):
    record = InspireRecord.create(record)
    inspire_recid_minter(record.id, record)
    db.session.commit()
    es.indices.refresh('records-hep')

    return record
Exemplo n.º 5
0
def record_insert_or_replace(json, skip_files=False):
    """Insert or replace a record."""
    pid_type = get_pid_type_from_schema(json['$schema'])
    control_number = json['control_number']

    try:
        pid = PersistentIdentifier.get(pid_type, control_number)
        record = InspireRecord.get_record(pid.object_uuid)
        record.clear()
        record.update(json, skip_files=skip_files)
        if json.get('legacy_creation_date'):
            record.model.created = datetime.strptime(json['legacy_creation_date'], '%Y-%m-%d')
        record.commit()
    except PIDDoesNotExistError:
        record = InspireRecord.create(json, id_=None, skip_files=skip_files)
        if json.get('legacy_creation_date'):
            record.model.created = datetime.strptime(json['legacy_creation_date'], '%Y-%m-%d')
        inspire_recid_minter(str(record.id), json)

    if json.get('deleted'):
        new_recid = get_recid_from_ref(json.get('new_record'))
        if not new_recid:
            record.delete()

    return record
Exemplo n.º 6
0
def _create_and_index_record(record):
    record = InspireRecord.create(record)
    inspire_recid_minter(record.id, record)
    # invenio-collections will populate _collections field in record upon
    # commit
    db.session.commit()
    es.indices.refresh('records-hep')

    return record
Exemplo n.º 7
0
def _create_and_index_record(record):
    record = Record.create(record)
    inspire_recid_minter(record.id, record)
    # invenio-collections will populate _collections field in record upon
    # commit
    db.session.commit()

    # Record needs to be indexed since views fetch records from ES
    r = RecordIndexer()
    r.index(record)
    es.indices.refresh('records-hep')

    return record
Exemplo n.º 8
0
def store_record(obj, *args, **kwargs):
    """Create and index new record in main record space."""
    assert "$schema" in obj.data, "No $schema attribute found!"

    # Create record
    # FIXME: Do some preprocessing of obj.data before creating a record so that
    # we're sure that the schema will be validated without touching the full
    # holdingpen stack.
    record = Record.create(obj.data, id_=None)

    # Create persistent identifier.
    pid = inspire_recid_minter(str(record.id), record)

    # Commit any changes to record
    record.commit()

    # Dump any changes to record
    obj.data = record.dumps()

    # Commit to DB before indexing
    db.session.commit()

    # Index record
    indexer = RecordIndexer()
    indexer.index_by_id(pid.object_uuid)
Exemplo n.º 9
0
def store_record(obj, *args, **kwargs):
    """Create and index new record in main record space."""
    assert "$schema" in obj.data, "No $schema attribute found!"

    # Create record
    # FIXME: Do some preprocessing of obj.data before creating a record so that
    # we're sure that the schema will be validated without touching the full
    # holdingpen stack.
    record = Record.create(obj.data, id_=None)

    # Create persistent identifier.
    pid = inspire_recid_minter(str(record.id), record)

    # Commit any changes to record
    record.commit()

    # Dump any changes to record
    obj.data = record.dumps()

    # Commit to DB before indexing
    db.session.commit()

    # Index record
    indexer = RecordIndexer()
    indexer.index_by_id(pid.object_uuid)
Exemplo n.º 10
0
def record_insert_or_replace(json):
    """Insert or replace a record."""
    control_number = json.get('control_number', json.get('recid'))
    if control_number:
        pid_type = get_pid_type_from_schema(json['$schema'])
        try:
            pid = PersistentIdentifier.get(pid_type, control_number)
            record = InspireRecord.get_record(pid.object_uuid)
            record.clear()
            record.update(json)
            record.commit()
        except PIDDoesNotExistError:
            record = InspireRecord.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 not new_recid:
                record.delete()

        return record
Exemplo n.º 11
0
def store_record(obj, *args, **kwargs):
    """Create and index new record in main record space."""
    if '$schema' in obj.data:
        obj.data['$schema'] = url_for(
            'invenio_jsonschemas.get_schema',
            schema_path="records/{0}".format(obj.data['$schema'])
        )
    # Create record
    rec_uuid = str(Record.create(obj.data, id_=None).id)

    # Create persistent identifier.
    pid = inspire_recid_minter(rec_uuid, obj.data)
    db.session.commit()

    # Index record
    indexer = RecordIndexer()
    indexer.index_by_id(pid.object_uuid)
Exemplo n.º 12
0
def store_record(obj, eng):
    """Insert or replace a record."""
    is_update = obj.extra_data.get('is-update')
    if is_update:
        record = InspireRecord.get_record(obj.extra_data['head_uuid'])
        record.clear()
        record.update(obj.data, files_src_records=[obj])

    else:
        record = InspireRecord.create(obj.data, id_=None)
        # Create persistent identifier.
        created_pid = inspire_recid_minter(str(record.id), record).pid_value
        # Now that we have a recid, we can properly download the documents
        record.download_documents_and_figures(src_records=[obj])

        obj.data['control_number'] = created_pid
        # store head_uuid to store the root later
        obj.extra_data['head_uuid'] = str(record.id)

    record.commit()
    obj.save()
    db.session.commit()
Exemplo n.º 13
0
def store_record(obj, *args, **kwargs):
    """Create and index new record in main record space."""
    assert "$schema" in obj.data, "No $schema attribute found!"

    # Create record
    record = Record.create(obj.data, id_=None)

    # Create persistent identifier.
    pid = inspire_recid_minter(str(record.id), record)

    # Commit any changes to record
    record.commit()

    # Dump any changes to record
    obj.data = record.dumps()

    # Commit to DB before indexing
    db.session.commit()

    # Index record
    indexer = RecordIndexer()
    indexer.index_by_id(pid.object_uuid)
Exemplo n.º 14
0
def store_record(obj, eng):
    """Insert or replace a record."""
    def _get_updated_record(obj):
        """TODO: use only head_uuid once we have them merger."""
        if 'head_uuid' in obj.extra_data:
            updated_record = InspireRecord.get_record(
                obj.extra_data['head_uuid'],
            )
        else:
            pid_type = get_pid_type_from_schema(obj.data['$schema'])
            updated_record_id = obj.extra_data['record_matches'][0]
            updated_record = get_db_record(pid_type, updated_record_id)

        return updated_record

    is_update = obj.extra_data.get('is-update')
    if is_update:
        record = _get_updated_record(obj)
        obj.data['control_number'] = record['control_number']
        record.clear()
        record.update(obj.data, files_src_records=[obj])

    else:
        # Skip the files to avoid issues in case the record has already pid
        # TODO: remove the skip files once labs becomes master
        record = InspireRecord.create(obj.data, id_=None, skip_files=True)
        # Create persistent identifier.
        created_pid = inspire_recid_minter(str(record.id), record).pid_value
        # Now that we have a recid, we can properly download the documents
        record.download_documents_and_figures(src_records=[obj])

        obj.data['control_number'] = created_pid
        # store head_uuid to store the root later
        obj.extra_data['head_uuid'] = str(record.id)

    record.commit()
    obj.save()
    db.session.commit()
Exemplo n.º 15
0
def store_record(obj, eng):
    """Insert or replace a record."""
    def _get_updated_record(obj):
        """TODO: use only head_uuid once we have them merger."""
        if 'head_uuid' in obj.extra_data:
            updated_record = InspireRecord.get_record(
                obj.extra_data['head_uuid'], )
        else:
            pid_type = get_pid_type_from_schema(obj.data['$schema'])
            updated_record_id = obj.extra_data['record_matches'][0]
            updated_record = get_db_record(pid_type, updated_record_id)

        return updated_record

    is_update = obj.extra_data.get('is-update')
    if is_update:
        record = _get_updated_record(obj)
        obj.data['control_number'] = record['control_number']
        record.clear()
        record.update(obj.data, files_src_records=[obj])

    else:
        # Skip the files to avoid issues in case the record has already pid
        # TODO: remove the skip files once labs becomes master
        record = InspireRecord.create(obj.data, id_=None, skip_files=True)
        # Create persistent identifier.
        created_pid = inspire_recid_minter(str(record.id), record).pid_value
        # Now that we have a recid, we can properly download the documents
        record.download_documents_and_figures(src_records=[obj])

        obj.data['control_number'] = created_pid
        # store head_uuid to store the root later
        obj.extra_data['head_uuid'] = str(record.id)

    record.commit()
    obj.save()
    db.session.commit()
Exemplo n.º 16
0
 def mint(id_, data):
     """Mint the record."""
     return inspire_recid_minter(id_, data)
Exemplo n.º 17
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 = {'collections': [{'primary': 'HEPNAMES'}],
              'name': {'value': name},
              '$schema': _get_author_schema()}

    # 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.º 18
0
 def mint(id_, data):
     """Mint the record."""
     return inspire_recid_minter(id_, data)
Exemplo n.º 19
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 = {'collections': [{'primary': 'HEPNAMES'}],
              'name': {'value': name},
              '$schema': _get_author_schema()}

    # 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 = Record.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()

    # Add the record to Elasticsearch.
    indexer = RecordIndexer()
    indexer.index_by_id(record_pid.object_uuid)

    # 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