本文整理汇总了Python中invenio_records_files.api.Record.create方法的典型用法代码示例。如果您正苦于以下问题:Python Record.create方法的具体用法?Python Record.create怎么用?Python Record.create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类invenio_records_files.api.Record
的用法示例。
在下文中一共展示了Record.create方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: glossary_terms
# 需要导入模块: from invenio_records_files.api import Record [as 别名]
# 或者: from invenio_records_files.api.Record import create [as 别名]
def glossary_terms():
"""Load demo terms records."""
from invenio_db import db
from invenio_records import Record
from invenio_indexer.api import RecordIndexer
from cernopendata.modules.records.minters.termid import \
cernopendata_termid_minter
indexer = RecordIndexer()
schema = current_app.extensions['invenio-jsonschemas'].path_to_url(
'records/glossary-term-v1.0.0.json'
)
data = pkg_resources.resource_filename('cernopendata',
'modules/fixtures/data')
glossary_terms_json = glob.glob(os.path.join(data, 'terms', '*.json'))
for filename in glossary_terms_json:
click.echo('Loading glossary-terms from {0} ...'.format(filename))
with open(filename, 'rb') as source:
for data in json.load(source):
if "collections" not in data and \
not isinstance(
data.get("collections", None), basestring):
data["collections"] = []
data["collections"].append({"primary": "Terms"})
id = uuid.uuid4()
cernopendata_termid_minter(id, data)
data['$schema'] = schema
record = Record.create(data, id_=id)
db.session.commit()
indexer.index(record)
db.session.expunge_all()
示例2: create_doc
# 需要导入模块: from invenio_records_files.api import Record [as 别名]
# 或者: from invenio_records_files.api.Record import create [as 别名]
def create_doc(data, schema):
"""Creates a new doc record."""
from invenio_records import Record
id = uuid.uuid4()
cernopendata_docid_minter(id, data)
data['$schema'] = schema
record = Record.create(data, id_=id)
return record
示例3: test_file_download_ui
# 需要导入模块: from invenio_records_files.api import Record [as 别名]
# 或者: from invenio_records_files.api.Record import create [as 别名]
def test_file_download_ui(app, objects):
"""Test get buckets."""
app.config.update(dict(
FILES_REST_PERMISSION_FACTORY=lambda *a, **kw: type(
'Allow', (object, ), {'can': lambda self: True}
)(),
RECORDS_UI_DEFAULT_PERMISSION_FACTORY=None, # No permission checking
RECORDS_UI_ENDPOINTS=dict(
recid=dict(
pid_type='recid',
route='/records/<pid_value>',
),
recid_files=dict(
pid_type='recid',
route='/records/<pid_value>/files/<filename>',
view_imp='invenio_records_files.utils:file_download_ui',
record_class='invenio_records_files.api:Record',
),
)
))
InvenioRecordsUI(app)
obj1 = objects[0]
with app.test_request_context():
# Record 1 - Live record
rec_uuid = uuid.uuid4()
PersistentIdentifier.create(
'recid', '1', object_type='rec', object_uuid=rec_uuid,
status=PIDStatus.REGISTERED)
record = Record.create({
'title': 'Registered',
'recid': 1,
'_files': [
{'key': obj1.key, 'bucket': str(obj1.bucket_id),
'checksum': 'invalid'},
]
}, id_=rec_uuid)
RecordsBuckets.create(record=record.model, bucket=obj1.bucket)
db.session.commit()
main_url = url_for('invenio_records_ui.recid', pid_value='1')
file_url = url_for(
'invenio_records_ui.recid_files', pid_value='1', filename=obj1.key)
no_file_url = url_for(
'invenio_records_ui.recid_files', pid_value='1', filename='')
invalid_file_url = url_for(
'invenio_records_ui.recid_files', pid_value='1', filename='no')
with app.test_client() as client:
res = client.get(main_url)
assert res.status_code == 200
res = client.get(file_url)
assert res.status_code == 200
res = client.get(no_file_url)
assert res.status_code == 404
res = client.get(invalid_file_url)
assert res.status_code == 404
示例4: data_policies
# 需要导入模块: from invenio_records_files.api import Record [as 别名]
# 或者: from invenio_records_files.api.Record import create [as 别名]
def data_policies(skip_files):
"""Load demo Data Policy records."""
from invenio_db import db
from invenio_indexer.api import RecordIndexer
from cernopendata.modules.records.minters.recid import \
cernopendata_recid_minter
from invenio_files_rest.models import \
Bucket, FileInstance, ObjectVersion
from invenio_records_files.models import RecordsBuckets
from invenio_records_files.api import Record
from invenio_records.models import RecordMetadata
indexer = RecordIndexer()
schema = current_app.extensions['invenio-jsonschemas'].path_to_url(
'records/data-policies-v1.0.0.json'
)
data = pkg_resources.resource_filename('cernopendata',
'modules/fixtures/data')
data_policies_json = glob.glob(os.path.join(data, '*.json'))
for filename in data_policies_json:
click.echo('Loading data-policies from {0} ...'.format(filename))
with open(filename, 'rb') as source:
for data in json.load(source):
files = data.pop('files', [])
id = uuid.uuid4()
cernopendata_recid_minter(id, data)
data['$schema'] = schema
record = Record.create(data, id_=id)
bucket = Bucket.create()
RecordsBuckets.create(
record=record.model, bucket=bucket)
for file in files:
if skip_files:
break
assert 'uri' in file
assert 'size' in file
assert 'checksum' in file
f = FileInstance.create()
filename = file.get("uri").split('/')[-1:][0]
f.set_uri(file.get("uri"), file.get(
"size"), file.get("checksum"))
ObjectVersion.create(
bucket,
filename,
_file_id=f.id
)
db.session.commit()
indexer.index(record)
db.session.expunge_all()
示例5: record_with_bucket
# 需要导入模块: from invenio_records_files.api import Record [as 别名]
# 或者: from invenio_records_files.api.Record import create [as 别名]
def record_with_bucket(full_record, bucket, db):
"""Create a bucket."""
record = RecordFile.create(full_record)
RecordsBuckets.create(bucket=bucket, record=record.model)
pid = PersistentIdentifier.create(
pid_type='recid', pid_value=12345, object_type='rec',
object_uuid=record.id, status='R')
db.session.commit()
return pid, record
示例6: record
# 需要导入模块: from invenio_records_files.api import Record [as 别名]
# 或者: from invenio_records_files.api.Record import create [as 别名]
def record(app, db):
"""Create a record."""
record = {
'title': 'fuu'
}
record = Record.create(record)
record.commit()
db.session.commit()
return record
示例7: publish
# 需要导入模块: from invenio_records_files.api import Record [as 别名]
# 或者: from invenio_records_files.api.Record import create [as 别名]
def publish(self, pid=None, id_=None):
"""Publish a deposit."""
pid = pid or self.pid
if not pid.is_registered():
raise PIDInvalidAction()
self['_deposit']['status'] = 'published'
if self['_deposit'].get('pid') is None: # First publishing
minter = current_pidstore.minters[
current_app.config['DEPOSIT_PID_MINTER']
]
id_ = id_ or uuid.uuid4()
record_pid = minter(id_, self)
self['_deposit']['pid'] = {
'type': record_pid.pid_type, 'value': record_pid.pid_value,
'revision_id': 0,
}
data = dict(self.dumps())
data['$schema'] = self.record_schema
# During first publishing create snapshot the bucket.
@contextmanager
def process_files(data):
"""Process deposit files."""
if self.files and self.files.bucket:
assert not self.files.bucket.locked
self.files.bucket.locked = True
snapshot = self.files.bucket.snapshot(lock=True)
data['_files'] = self.files.dumps(bucket=snapshot.id)
yield data
db.session.add(RecordsBuckets(
record_id=id_, bucket_id=snapshot.id
))
else:
yield data
with process_files(data) as data:
record = Record.create(data, id_=id_)
else: # Update after edit
record_pid, record = self.fetch_published()
# TODO add support for patching
assert record.revision_id == self['_deposit']['pid']['revision_id']
data = dict(self.dumps())
data['$schema'] = self.record_schema
record = record.__class__(data, model=record.model)
record.commit()
self.commit()
return self
示例8: test_RecordSIP_create
# 需要导入模块: from invenio_records_files.api import Record [as 别名]
# 或者: from invenio_records_files.api.Record import create [as 别名]
def test_RecordSIP_create(db, mocker):
"""Test create method from the API class RecordSIP."""
# we setup a file storage
tmppath = tempfile.mkdtemp()
db.session.add(Location(name='default', uri=tmppath, default=True))
# setup metadata
mtype = SIPMetadataType(title='JSON Test', name='json-test',
format='json', schema='url://to/schema')
db.session.add(mtype)
db.session.commit()
# first we create a record
recid = uuid.uuid4()
pid = PersistentIdentifier.create(
'recid',
'1337',
object_type='rec',
object_uuid=recid,
status=PIDStatus.REGISTERED)
mocker.patch('invenio_records.api.RecordBase.validate',
return_value=True, autospec=True)
record = Record.create(
{'title': 'record test', '$schema': 'url://to/schema'},
recid)
# we add a file to the record
bucket = Bucket.create()
content = b'Test file\n'
RecordsBuckets.create(record=record.model, bucket=bucket)
record.files['test.txt'] = BytesIO(content)
db.session.commit()
# Let's create a SIP
user = create_test_user('[email protected]')
agent = {'email': '[email protected]', 'ip_address': '1.1.1.1'}
rsip = RecordSIP.create(pid, record, True, user_id=user.id, agent=agent)
db.session.commit()
# test!
assert RecordSIP_.query.count() == 1
assert SIP_.query.count() == 1
assert SIPFile.query.count() == 1
assert SIPMetadata.query.count() == 1
assert len(rsip.sip.files) == 1
assert len(rsip.sip.metadata) == 1
metadata = rsip.sip.metadata[0]
assert metadata.type.format == 'json'
assert '"title": "record test"' in metadata.content
assert rsip.sip.archivable is True
# we try with no files
rsip = RecordSIP.create(pid, record, True, create_sip_files=False,
user_id=user.id, agent=agent)
assert SIPFile.query.count() == 1
assert SIPMetadata.query.count() == 2
assert len(rsip.sip.files) == 0
assert len(rsip.sip.metadata) == 1
# finalization
rmtree(tmppath)
示例9: record
# 需要导入模块: from invenio_records_files.api import Record [as 别名]
# 或者: from invenio_records_files.api.Record import create [as 别名]
def record(db):
"""Record fixture."""
rec_uuid = uuid.uuid4()
provider = RecordIdProvider.create(
object_type='rec', object_uuid=rec_uuid)
record = Record.create({
'control_number': provider.pid.pid_value,
'title': 'TestDefault',
}, id_=rec_uuid)
db.session.commit()
return record
示例10: create_record
# 需要导入模块: from invenio_records_files.api import Record [as 别名]
# 或者: from invenio_records_files.api.Record import create [as 别名]
def create_record(schema, data, files, skip_files):
"""Creates a new record."""
id = uuid.uuid4()
cernopendata_recid_minter(id, data)
data['$schema'] = schema
record = Record.create(data, id_=id)
if not skip_files:
bucket = Bucket.create()
handle_record_files(data, bucket, files, skip_files)
RecordsBuckets.create(
record=record.model, bucket=bucket)
return record
示例11: test_files_protection
# 需要导入模块: from invenio_records_files.api import Record [as 别名]
# 或者: from invenio_records_files.api.Record import create [as 别名]
def test_files_protection(app, db, location):
"""Test record files property protection."""
record = Record.create({})
bucket = record.files.bucket
assert bucket
# Create first file:
record.files['hello.txt'] = BytesIO(b'Hello world!')
file_0 = record.files['hello.txt']
assert 'hello.txt' == file_0['key']
assert 1 == len(record.files)
# Lock bucket.
bucket.locked = True
assert record.files.bucket.locked
with pytest.raises(InvalidOperationError):
del record.files['hello.txt']
示例12: test_RecordSIP
# 需要导入模块: from invenio_records_files.api import Record [as 别名]
# 或者: from invenio_records_files.api.Record import create [as 别名]
def test_RecordSIP(db):
"""Test RecordSIP API class."""
user = create_test_user('[email protected]')
agent = {'email': '[email protected]', 'ip_address': '1.1.1.1'}
# we create a record
recid = uuid.uuid4()
pid = PersistentIdentifier.create(
'recid',
'1337',
object_type='rec',
object_uuid=recid,
status=PIDStatus.REGISTERED)
title = {'title': 'record test'}
record = Record.create(title, recid)
# we create the models
sip = SIP.create(True, user_id=user.id, agent=agent)
recordsip = RecordSIP_(sip_id=sip.id, pid_id=pid.id)
db.session.commit()
# We create an API SIP on top of it
api_recordsip = RecordSIP(recordsip, sip)
assert api_recordsip.model is recordsip
assert api_recordsip.sip.id == sip.id
示例13: store_record
# 需要导入模块: from invenio_records_files.api import Record [as 别名]
# 或者: from invenio_records_files.api.Record import create [as 别名]
def store_record(obj, eng):
"""Stores record in database"""
set_springer_source_if_needed(obj)
obj.data['record_creation_year'] = parse_date(obj.data['record_creation_date']).year
try:
record = Record.create(obj.data, id_=None)
# Create persistent identifier.
scoap3_recid_minter(str(record.id), record)
obj.save()
record.commit()
# Commit to DB before indexing
db.session.commit()
obj.data['control_number'] = record['control_number']
obj.save()
except ValidationError as err:
__halt_and_notify("Validation error: %s." % err, eng)
except PIDAlreadyExists:
__halt_and_notify("Record with this id already in DB", eng)
示例14: add_record
# 需要导入模块: from invenio_records_files.api import Record [as 别名]
# 或者: from invenio_records_files.api.Record import create [as 别名]
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()
示例15: datasets
# 需要导入模块: from invenio_records_files.api import Record [as 别名]
# 或者: from invenio_records_files.api.Record import create [as 别名]
def datasets(skip_files):
"""Load demo datasets records."""
from invenio_db import db
from invenio_records_files.api import Record
from invenio_indexer.api import RecordIndexer
from cernopendata.modules.records.minters.recid import \
cernopendata_recid_minter
from cernopendata.modules.records.minters.datasetid import \
cernopendata_datasetid_minter
from invenio_files_rest.models import \
Bucket, FileInstance, ObjectVersion
from invenio_records_files.models import RecordsBuckets
indexer = RecordIndexer()
schema = current_app.extensions['invenio-jsonschemas'].path_to_url(
'records/datasets-v1.0.0.json'
)
data = pkg_resources.resource_filename('cernopendata',
'modules/fixtures/data/datasets')
datasets_json = glob.glob(os.path.join(data, '*.json'))
# FIXME: change the treatment of `files` according to `records` fixtures.
for filename in datasets_json:
click.echo('Loading datasets from {0} ...'.format(filename))
with open(filename, 'rb') as source:
for data in json.load(source):
files = data.pop('files', [])
id = uuid.uuid4()
# (TOFIX) Remove if statement in production
# as every dataset record should have a doi
if data.get('doi', None):
cernopendata_datasetid_minter(id, data)
else:
cernopendata_recid_minter(id, data)
data['$schema'] = schema
record = Record.create(data, id_=id)
bucket = Bucket.create()
RecordsBuckets.create(
record=record.model, bucket=bucket)
for file in files:
if skip_files:
break
assert 'uri' in file
assert 'size' in file
assert 'checksum' in file
f = FileInstance.create()
filename = file.get("uri").split('/')[-1:][0]
f.set_uri(file.get("uri"), file.get(
"size"), file.get("checksum"))
ObjectVersion.create(
bucket,
filename,
_file_id=f.id
)
db.session.commit()
indexer.index(record)
db.session.expunge_all()