本文整理汇总了Python中invenio_records.Record.create方法的典型用法代码示例。如果您正苦于以下问题:Python Record.create方法的具体用法?Python Record.create怎么用?Python Record.create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类invenio_records.Record
的用法示例。
在下文中一共展示了Record.create方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_validate_with_format
# 需要导入模块: from invenio_records import Record [as 别名]
# 或者: from invenio_records.Record import create [as 别名]
def test_validate_with_format(app, db):
"""Test that validation can accept custom format rules."""
with app.app_context():
checker = FormatChecker()
checker.checks("foo")(lambda el: el.startswith("foo"))
data = {"bar": "foo", "$schema": {"properties": {"bar": {"format": "foo"}}}}
# test record creation with valid data
assert data == Record.create(data)
record = Record.create(data, format_checker=checker)
# test direct call to validate with valid data
assert record.validate(format_checker=checker) is None
# test commit with valid data
record.commit(format_checker=checker)
record["bar"] = "bar"
# test direct call to validate with invalid data
with pytest.raises(ValidationError) as excinfo:
record.validate(format_checker=checker)
assert "'bar' is not a 'foo'" in str(excinfo.value)
# test commit with invalid data
with pytest.raises(ValidationError) as excinfo:
record.commit(format_checker=checker)
assert "'bar' is not a 'foo'" in str(excinfo.value)
data["bar"] = "bar"
# test record creation with invalid data
with pytest.raises(ValidationError) as excinfo:
record = Record.create(data, format_checker=checker)
assert "'bar' is not a 'foo'" in str(excinfo.value)
示例2: test_validate_partial
# 需要导入模块: from invenio_records import Record [as 别名]
# 或者: from invenio_records.Record import create [as 别名]
def test_validate_partial(app, db):
"""Test partial validation."""
schema = {"properties": {"a": {"type": "string"}, "b": {"type": "string"}}, "required": ["b"]}
data = {"a": "hello", "$schema": schema}
with app.app_context():
# Test validation on create()
# normal validation should fail because 'b' is required
with pytest.raises(ValidationError) as exc_info:
Record.create(data)
assert "'b' is a required property" == exc_info.value.message
# validate with a less restrictive validator
record = Record.create(data, validator=PartialDraft4Validator)
# set wrong data types should fails in any case
data_incorrect = copy.deepcopy(data)
data_incorrect["a"] = 1
with pytest.raises(ValidationError) as exc_info:
Record.create(data_incorrect, validator=PartialDraft4Validator)
assert "1 is not of type 'string'" == exc_info.value.message
# Test validation on commit()
# validation not passing with normal validator
with pytest.raises(ValidationError) as exc_info:
record.commit()
assert "'b' is a required property" == exc_info.value.message
# validation passing with less restrictive validator
assert data == record.commit(validator=PartialDraft4Validator)
# set wrong data types should fails in any case
record["a"] = 1
with pytest.raises(ValidationError) as exc_info:
record.commit(validator=PartialDraft4Validator)
示例3: test_file_permissions
# 需要导入模块: from invenio_records import Record [as 别名]
# 或者: from invenio_records.Record import create [as 别名]
def test_file_permissions(app, db, test_object, # fixtures
user, access_right, expected):
"""Test file permissions."""
# Create test users
admin = User(email='[email protected]', password='123456')
owner = User(email='[email protected]', password='123456')
auth = User(email='[email protected]', password='123456')
db.session.add_all([admin, owner, auth])
db.session.add(
ActionUsers.allow(ActionNeed('admin-access'), user=admin)
)
# Create test record
rec_uuid = uuid.uuid4()
PersistentIdentifier.create(
'recid',
'1',
object_type='rec',
object_uuid=rec_uuid,
status=PIDStatus.REGISTERED
)
Record.create({
'recid': 1,
'owners': [2],
'access_right': access_right,
'_files': [
{
'key': test_object.key,
'bucket': str(test_object.bucket_id),
'checksum': 'invalid'
},
]
}, id_=rec_uuid)
db.session.add(
RecordsBuckets(record_id=rec_uuid, bucket_id=test_object.bucket_id)
)
file_url = url_for(
'invenio_records_ui.recid_files',
pid_value='1',
filename=test_object.key
)
db.session.commit()
with app.test_client() as client:
if user:
# Login as user
with client.session_transaction() as sess:
sess['user_id'] = User.query.filter_by(
email='{}@zenodo.org'.format(user)).one().id
sess['_fresh'] = True
res = client.get(file_url)
assert res.status_code == expected
示例4: files
# 需要导入模块: from invenio_records import Record [as 别名]
# 或者: from invenio_records.Record import create [as 别名]
def files():
"""Load files."""
data_path = os.path.join(os.path.dirname(__file__), 'data')
# Create location
loc = Location(name='local', uri=data_path, default=True)
db.session.commit()
# Bucket
bucket = Bucket.create(loc)
# Example files from the data folder
example_files = (
'markdown.md',
'csvfile.csv',
'zipfile.zip',
'jsonfile.json',
'xmlfile.xml',
'notebook.ipynb',
'jpgfile.jpg',
'pngfile.png',
)
# Create single file records
for f in example_files:
with open(os.path.join(data_path, f), 'rb') as fp:
create_object(bucket, f, fp)
# Create a multi-file record
rec_uuid = uuid4()
provider = RecordIdProvider.create(object_type='rec', object_uuid=rec_uuid)
data = {
'pid_value': provider.pid.pid_value,
'files': []
}
# Template to create different files
template_file = {
'uri': '/files/{0}/{1}',
'key': '',
'bucket': str(bucket.id),
'local': True
}
for filename in example_files:
file_data = template_file.copy()
file_data['uri'] = file_data['uri'].format(str(bucket.id), filename)
file_data['key'] = filename
data['files'].append(file_data)
Record.create(data, id_=rec_uuid)
db.session.commit()
示例5: load_records
# 需要导入模块: from invenio_records import Record [as 别名]
# 或者: from invenio_records.Record import create [as 别名]
def load_records(app, filename, schema, tries=5):
"""Try to index records."""
indexer = RecordIndexer()
records = []
with app.app_context():
with mock.patch('invenio_records.api.Record.validate',
return_value=None):
data_filename = pkg_resources.resource_filename(
'invenio_records', filename)
records_data = load(data_filename)
with db.session.begin_nested():
for item in records_data:
record_id = uuid.uuid4()
item_dict = dict(marc21.do(item))
item_dict['$schema'] = schema
recid_minter(record_id, item_dict)
oaiid_minter(record_id, item_dict)
record = Record.create(item_dict, id_=record_id)
indexer.index(record)
records.append(record.id)
db.session.commit()
# Wait for indexer to finish
for i in range(tries):
response = current_search_client.search()
if response['hits']['total'] >= len(records):
break
current_search.flush_and_refresh('_all')
return records
示例6: test_record_replace_refs
# 需要导入模块: from invenio_records import Record [as 别名]
# 或者: from invenio_records.Record import create [as 别名]
def test_record_replace_refs(app):
"""Test the replacement of JSON references using JSONResolver."""
with app.app_context():
record = Record.create({
'one': {'$ref': 'http://nest.ed/A'},
'three': {'$ref': 'http://nest.ed/ABC'}
})
app.extensions['invenio-records'].loader_cls = json_loader_factory(
JSONResolver(plugins=['demo.json_resolver']))
out_json = record.replace_refs()
expected_json = {
'one': {
'letter': 'A',
'next': '.',
},
'three': {
'letter': 'A',
'next': {
'letter': 'B',
'next': {
'letter': 'C',
'next': '.',
},
},
},
}
assert out_json == expected_json
示例7: test_record_dump
# 需要导入模块: from invenio_records import Record [as 别名]
# 或者: from invenio_records.Record import create [as 别名]
def test_record_dump(app, db):
"""Test record dump method."""
with app.app_context():
record = Record.create({'foo': {'bar': 'Bazz', }, })
record_dump = record.dumps()
record_dump['foo']['bar'] = 'Spam'
assert record_dump['foo']['bar'] != record['foo']['bar']
示例8: record_upsert
# 需要导入模块: from invenio_records import Record [as 别名]
# 或者: from invenio_records.Record import create [as 别名]
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
示例9: store_record
# 需要导入模块: from invenio_records import Record [as 别名]
# 或者: from invenio_records.Record import create [as 别名]
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)
示例10: test_reindex
# 需要导入模块: from invenio_records import Record [as 别名]
# 或者: from invenio_records.Record import create [as 别名]
def test_reindex(app, script_info):
"""Test reindex."""
# load records
with app.test_request_context():
runner = CliRunner()
rec_uuid = uuid.uuid4()
data = {'title': 'Test0'}
record = Record.create(data, id_=rec_uuid)
db.session.commit()
# Initialize queue
res = runner.invoke(cli.queue, ['init', 'purge'],
obj=script_info)
assert 0 == res.exit_code
res = runner.invoke(cli.reindex, ['--yes-i-know'], obj=script_info)
assert 0 == res.exit_code
res = runner.invoke(cli.run, [], obj=script_info)
assert 0 == res.exit_code
sleep(5)
indexer = RecordIndexer()
index, doc_type = indexer.record_to_index(record)
res = current_search_client.get(index=index, doc_type=doc_type,
id=rec_uuid)
assert res['found']
# Destroy queue
res = runner.invoke(cli.queue, ['delete'],
obj=script_info)
assert 0 == res.exit_code
示例11: test_preprocessor_mixin_record
# 需要导入模块: from invenio_records import Record [as 别名]
# 或者: from invenio_records.Record import create [as 别名]
def test_preprocessor_mixin_record(app):
"""Test preprocessor mixin."""
with db.session.begin_nested():
recuuid = uuid.uuid4()
record = Record.create({
'title': 'test', 'aref': {'$ref': '#/title'}}, id_=recuuid)
record.model.created = datetime(2015, 10, 1, 11, 11, 11, 1)
pid = PersistentIdentifier.create(
'recid', '1', object_type='rec', object_uuid=recuuid,
status=PIDStatus.REGISTERED)
db.session.commit()
data = PreprocessorMixin().preprocess_record(pid, record)
for k in keys:
assert k in data
assert data['metadata']['title'] == 'test'
assert data['metadata']['aref'] == {'$ref': '#/title'}
assert data['created'] == '2015-10-01T11:11:11.000001+00:00'
assert data['revision'] == 1
data = PreprocessorMixin(replace_refs=True).preprocess_record(
pid, Record({'title': 'test2', 'aref': {'$ref': '#/title'}}))
assert data['created'] is None
assert data['updated'] is None
assert data['metadata']['aref'] == 'test2'
示例12: migrate_chunk
# 需要导入模块: from invenio_records import Record [as 别名]
# 或者: from invenio_records.Record import create [as 别名]
def migrate_chunk(chunk, broken_output=None, dry_run=False):
from invenio_indexer.api import RecordIndexer
from ..pidstore.minters import inspire_recid_minter
indexer = RecordIndexer()
index_queue = []
for raw_record in chunk:
record = marc_create_record(raw_record, keep_singletons=False)
json_record = create_record(record)
if '$schema' in json_record:
json_record['$schema'] = url_for(
'invenio_jsonschemas.get_schema',
schema_path="records/{0}".format(json_record['$schema'])
)
rec_uuid = str(Record.create(json_record, id_=None).id)
# Create persistent identifier.
pid = inspire_recid_minter(rec_uuid, json_record)
index_queue.append(pid.object_uuid)
db.session.commit()
# Request record indexing
for i in index_queue:
indexer.index_by_id(i)
# Send task to migrate files.
return rec_uuid
示例13: test_permission_factory
# 需要导入模块: from invenio_records import Record [as 别名]
# 或者: from invenio_records.Record import create [as 别名]
def test_permission_factory(app, action, permission_factory):
"""Test revisions."""
InvenioAccess(app)
with app.app_context():
rec_uuid = uuid.uuid4()
with db.session.begin_nested():
user_all = User(email='[email protected]')
user_one = User(email='[email protected]')
user_none = User(email='[email protected]')
db.session.add(user_all)
db.session.add(user_one)
db.session.add(user_none)
db.session.add(ActionUsers(action=action,
user=user_all, argument=None))
db.session.add(ActionUsers(action=action,
user=user_one, argument=str(rec_uuid)))
record = Record.create({'title': 'permission test'}, id_=rec_uuid)
# Create a record and assign permissions.
permission = permission_factory(record)
# Assert which permissions has access.
assert permission.allows(FakeIdentity(UserNeed(user_all.id)))
assert permission.allows(FakeIdentity(UserNeed(user_one.id)))
assert not permission.allows(FakeIdentity(UserNeed(user_none.id)))
示例14: test_valid_put
# 需要导入模块: from invenio_records import Record [as 别名]
# 或者: from invenio_records.Record import create [as 别名]
def test_valid_put(app):
"""Test VALID record patch request (PATCH .../records/<record_id>)."""
with app.app_context():
InvenioRecordsREST(app)
# create the record using the internal API
internal_record = Record.create(test_data)
with app.test_client() as client:
headers = [('Content-Type', 'application/json'),
('Accept', 'application/json')]
res = client.put(url_for(blueprint.name + '.' +
RecordResource.view_name,
record_id=internal_record.model.id),
data=json.dumps(test_data_patched),
headers=headers)
assert res.status_code == 200
# check that the returned record matches the given data
response_data = json.loads(res.get_data(as_text=True))
assert response_data['data'] == test_data_patched
# check that an internal record returned id and that it contains
# the same data
assert 'id' in response_data.keys()
internal_record = Record.get_record(response_data['id'])
assert internal_record == response_data['data']
# check that the returned self link returns the same data
subtest_self_link(response_data, res.headers, client)
示例15: test_record_dump
# 需要导入模块: from invenio_records import Record [as 别名]
# 或者: from invenio_records.Record import create [as 别名]
def test_record_dump(app, db):
"""Test record dump method."""
with app.app_context():
record = Record.create({"foo": {"bar": "Bazz"}})
record_dump = record.dumps()
record_dump["foo"]["bar"] = "Spam"
assert record_dump["foo"]["bar"] != record["foo"]["bar"]