def add_drafts_from_file(file_path, schema, egroup=None, user=None, limit=None): """Adds drafts from a specified file. Drafts with specified pid will be registered under those. For drafts without pid, new pids will be minted. """ with open(file_path, 'r') as fp: entries = json.load(fp) for entry in entries[0:limit]: data = construct_draft_obj(schema, entry) pid = cap_deposit_fetcher(None, data) pid_value = pid.pid_value if pid else None try: PersistentIdentifier.get('depid', pid_value) print('Draft with id {} already exist!'.format(pid_value)) except PIDDoesNotExistError: record_uuid = uuid.uuid4() pid = cap_deposit_minter(record_uuid, data) if user: user = User.query.filter_by(email=user).one() if egroup: role = Role.query.filter_by(name=egroup).one() deposit = CAPDeposit.create(data, record_uuid, user) deposit.commit() if egroup: add_read_permission_for_egroup(deposit, egroup) print('Draft {} added.'.format(pid.pid_value))
def test_deposit_minter(app, db): with app.app_context(): test_data = {} rec_uuid = uuid.uuid4() pid = cap_deposit_minter(rec_uuid, test_data) assert pid assert test_data['_deposit']['id'] == int(pid.pid_value) assert pid.object_type == 'rec' assert pid.object_uuid == rec_uuid
def test_deposit_fetcher(app, db): with app.app_context(): test_data = {} rec_uuid = uuid.uuid4() minter_pid = cap_deposit_minter(rec_uuid, test_data) fetcher_pid = cap_deposit_fetcher(rec_uuid, test_data) assert minter_pid.pid_value == fetcher_pid.pid_value assert fetcher_pid.provider is None assert fetcher_pid.pid_type == 'depid'
def test_deposit_minter_when_no_pid_in_data_mints_one(app, db): with app.app_context(): test_data = {} record_uuid = uuid.uuid4() pid = cap_deposit_minter(record_uuid, test_data) assert pid assert test_data['_deposit']['id'] == pid.pid_value assert pid.object_type == 'rec' assert pid.object_uuid == record_uuid assert pid.status == PIDStatus.REGISTERED
def test_deposit_minter_when_pid_specified_registers_deposit_with_it(app, db): with app.app_context(): pid_value = '1234123412341234' test_data = { '_deposit': { 'id': pid_value } } record_uuid = uuid.uuid4() pid = cap_deposit_minter(record_uuid, test_data) assert pid assert pid.pid_value == pid_value assert pid.object_type == 'rec' assert pid.object_uuid == record_uuid assert pid.status == PIDStatus.REGISTERED
def add_drafts_from_file(file_path, schema, egroup=None, usermail=None, limit=None): """Adds drafts from a specified file. Drafts with specified pid will be registered under those. For drafts without pid, new pids will be minted. """ if usermail: user = get_existing_or_register_user(usermail) else: user = None with open(file_path, 'r') as fp: entries = json.load(fp) for entry in entries[0:limit]: data = construct_draft_obj(schema, entry) pid = cap_deposit_fetcher(None, data) pid_value = pid.pid_value if pid else None try: PersistentIdentifier.get('depid', pid_value) print('Draft with id {} already exist!'.format(pid_value)) except PIDDoesNotExistError: record_uuid = uuid.uuid4() pid = cap_deposit_minter(record_uuid, data) deposit = CAPDeposit.create(data, record_uuid, user) deposit.commit() if egroup: add_read_permission_for_egroup(deposit, egroup) print('Draft {} added.'.format(pid.pid_value)) db.session.commit()
def add(file_path, schema, version, egroup, usermail, limit): """Add drafts from a file. Drafts with specified pid will be registered under those. For drafts without pid, new pids will be minted. """ if usermail: user = get_existing_or_register_user(usermail) else: user = None with open(file_path, 'r') as fp: entries = json.load(fp) for entry in entries[0:limit]: pid = cap_deposit_fetcher(None, entry) pid_value = pid.pid_value if pid else None try: PersistentIdentifier.get('depid', pid_value) click.secho( 'Draft with id {} already exist!'.format(pid_value), fg='red') except PIDDoesNotExistError: record_uuid = uuid.uuid4() pid = cap_deposit_minter(record_uuid, entry) deposit = CAPDeposit.create(entry, record_uuid, user) deposit.commit() if egroup: add_read_permission_for_egroup(deposit, egroup) click.secho('Draft {} added.'.format(pid.pid_value), fg='green') db.session.commit()