def test_b2handle_pid_creation(): """Test the creatio of B2Handle PID.""" app = Flask(__name__) app.config.update(dict(PID_HANDLE_CREDENTIALS=dict(prefix='myprefix'))) with app.app_context(): with patch.object(EUDATHandleClient, 'generate_and_register_handle') as mock_register: B2ShareHandle(app) assert current_handle.handle_client is not None assert current_handle.handle_prefix == 'myprefix' current_handle.create_handle(location='mylocation', checksum='mychecksum', fixed=True) entries = { 'EUDAT/FIXED_CONTENT': 'True', 'EUDAT/PROFILE_VERSION': '1', 'EUDAT/CHECKSUM_TIMESTAMP': ANY, 'EUDAT/CHECKSUM': 'mychecksum' } mock_register.assert_called_with(location='mylocation', checksum='mychecksum', prefix='myprefix', **entries)
def create_file_pids(record_metadata): from flask import current_app throw_on_failure = current_app.config.get('CFG_FAIL_ON_MISSING_FILE_PID', False) external_pids = record_metadata['_deposit'].get('external_pids', []) external_keys = {x.get('key') for x in external_pids} for f in record_metadata.get('_files'): if f.get('ePIC_PID') or f.get('key') in external_keys: continue file_url = url_for('invenio_files_rest.object_api', bucket_id=f.get('bucket'), key=f.get('key'), _external=True) try: from b2share.modules.handle.proxies import current_handle from b2share.modules.handle.errors import EpicPIDError file_pid = current_handle.create_handle(file_url, checksum=f.get('checksum'), fixed=True) if file_pid is None: raise EpicPIDError("EPIC PID allocation for file failed") f['ePIC_PID'] = file_pid except EpicPIDError as e: if throw_on_failure: raise e else: current_app.logger.warning(e)
def b2share_pid_minter(rec_pid, data): """Mint EPIC PID for published record.""" epic_pids = [p for p in data['_pid'] if p.get('type') == 'ePIC_PID'] assert len(epic_pids) == 0 url = make_record_url(rec_pid.pid_value) throw_on_failure = current_app.config.get('CFG_FAIL_ON_MISSING_PID', True) try: pid = current_handle.create_handle(url) if pid is None: raise EpicPIDError("EPIC PID allocation failed") data['_pid'].append({'value': pid, 'type': 'ePIC_PID'}) except EpicPIDError as e: if throw_on_failure: raise e else: current_app.logger.warning(e)
def b2share_pid_minter(rec_pid, data): """Mint EPIC PID for published record.""" epic_pids = [p for p in data['_pid'] if p.get('type') == 'ePIC_PID'] assert len(epic_pids) == 0 url = make_record_url(rec_pid.pid_value) throw_on_failure = current_app.config.get('CFG_FAIL_ON_MISSING_PID', True) try: pid = current_handle.create_handle(url) if pid is None: raise EpicPIDError("EPIC PID allocation failed") data['_pid'].append({'value': pid, 'type': 'ePIC_PID'}) except EpicPIDError as e: if throw_on_failure: raise e else: current_app.logger.warning(e)
def create_file_pids(record_metadata): from flask import current_app throw_on_failure = current_app.config.get('CFG_FAIL_ON_MISSING_FILE_PID', False) for f in record_metadata.get('_files'): if f.get('ePIC_PID'): continue file_url = url_for('invenio_files_rest.object_api', bucket_id=f.get('bucket'), key=f.get('key'), _external=True) try: file_pid = current_handle.create_handle( file_url, checksum=f.get('checksum'), fixed=True ) if file_pid is None: raise EpicPIDError("EPIC PID allocation for file failed") f['ePIC_PID'] = file_pid except EpicPIDError as e: if throw_on_failure: raise e else: current_app.logger.warning(e)