示例#1
0
def create_file(record, bucket, filename, stream):
    """Create a file and add in record."""
    obj = ObjectVersion.create(bucket, filename, stream=stream)
    rb = RecordsBuckets(record_id=record.id, bucket_id=obj.bucket_id)
    db.session.add(rb)
    record.update(dict(
        _files=[dict(
            bucket=str(bucket.id),
            key=filename,
            size=obj.file.size,
            checksum=str(obj.file.checksum),
            version_id=str(obj.version_id),
        ), ]
    ))
    record.commit()
    db.session.commit()
示例#2
0
def record_with_file(db, record, testfile):
    """Record with a test file."""
    rb = RecordsBuckets(record_id=record.id, bucket_id=testfile.bucket_id)
    db.session.add(rb)
    record.update(dict(
        _files=[dict(
            bucket=str(testfile.bucket_id),
            key=testfile.key,
            size=testfile.file.size,
            checksum=str(testfile.file.checksum),
            version_id=str(testfile.version_id),
        ), ]
    ))
    record.commit()
    db.session.commit()
    return record
示例#3
0
def test_record_files_factory(app, db, location, record_with_bucket):
    """Test record file factory."""
    record = record_with_bucket
    record.files['test.txt'] = BytesIO(b'Hello world!')

    # Get a valid file
    fileobj = record_file_factory(None, record, 'test.txt')
    assert fileobj.key == 'test.txt'

    # Get a invalid file
    assert record_file_factory(None, record, 'invalid') is None

    # Record has no files property
    assert record_file_factory(None, Record({}), 'invalid') is None
    assert record_file_factory(None, BaseRecord({}), 'invalid') is None
    baserecord = BaseRecord.create({})
    RecordsBuckets(bucket=Bucket.create(), record=baserecord)
    assert record_file_factory(None, baserecord, 'invalid') is None
def add_record(metadata, collection, schema, force, files=[]):
    """Add record."""

    collection = Collection.query.filter(Collection.name == collection).first()

    if collection is None:
        return

    data, pid, recid = construct_record(collection, metadata, 1,
                                        {} if force else schema)
    d = current_app.config['DATADIR']

    buckets = []
    data['_files'] = []

    for file in files:
        bucket = Bucket.create(default_location=Location.get_default())
        buckets.append(bucket)

        with open(
                pkg_resources.resource_filename(
                    'cap.modules.fixtures',
                    os.path.join('data', 'files', file)), 'rb') as fp:
            obj = ObjectVersion.create(bucket, file, stream=fp)

            data['_files'].append({
                'bucket': str(obj.bucket_id),
                'key': obj.key,
                'size': obj.file.size,
                'checksum': str(obj.file.checksum),
                'version_id': str(obj.version_id),
            })
    try:
        record = Record.create(data, id_=recid)

        for bucket in buckets:
            rb = RecordsBuckets(record_id=record.id, bucket_id=bucket.id)
            db.session.add(rb)

        # Invenio-Indexer is delegating the document inferring to
        # Invenio-Search which is analysing the string splitting by `/` and
        # using `.json` to be sure that it cans understand the mapping.
        record['$schema'] = 'mappings/{0}.json'.format(collection.name.lower())

        indexer = RecordIndexer()
        indexer.index(record)

        # Creating permission needs for the record
        action_edit_record = RecordUpdateActionNeed(str(recid))
        action_read_record = RecordReadActionNeed(str(recid))
        action_index_record = RecordIndexActionNeed(str(recid))

        # Giving index, read, write permissions to user/creator
        db.session.add(ActionUsers.allow(action_edit_record))
        db.session.add(ActionUsers.allow(action_read_record))
        db.session.add(ActionUsers.allow(action_index_record))

        db.session.commit()

        print("DONE!!!")

    except ValidationError as error:
        print("============================")
        pprint(error.message)
        pprint(error.path)
        print("============================")

        db.session.rollback()