def _fill_video_extra_links(record, links): """Add extra links if it's a video.""" project = None try: pid, project = record_resolver.resolve(record['_project_id']) # include record project links links.update(**_build_record_project_links( project_pid=pid)) except KeyError: # Most likely are dealing with a project if is_project_record(record): project = record except PIDDoesNotExistError: # The project has not being published yet try: pid, project = project_resolver.resolve(record['_project_id']) except PIDDoesNotExistError: pass try: # include deposit project links if project: links.update(**_build_deposit_project_links( deposit_project=project_resolver.resolve( project['_deposit']['id']))) except (KeyError, PIDDoesNotExistError): pass
def _resolve_deposit(id_type, id_value): """Return the deposit video.""" depid = id_value if id_type == 'recid': _, record = record_resolver.resolve(id_value) depid = record['_deposit']['id'] return deposit_video_resolver(depid), depid
def record_link_factory(pid): """Record link creation.""" try: links = default_links_factory(pid=pid) _, record = record_resolver.resolve(pid_value=pid.pid_value) _fill_video_extra_links(record=record, links=links) return links except Exception: return {}
def fix_bucket_conflict(recid): """Create missing subformats given a record id or deposit id.""" def _force_sync_deposit_bucket(record): """Replace deposit bucket with a copy of the record bucket.""" deposit = Video.get_record(record.depid.object_uuid) # if deposit['_deposit']['status'] == 'draft': # raise RuntimeError('Deposit in edit mode: {0}'.format(deposit.id)) deposit_old_bucket = deposit.files.bucket # create a copy of record bucket new_bucket = record.files.bucket.snapshot() new_bucket.locked = False db.session.commit() rb = RecordsBuckets.query.filter( RecordsBuckets.bucket_id == deposit_old_bucket.id).one() rb.bucket = new_bucket db.session.add(rb) db.session.commit() # Put tags correctly pointing to the right object master_file = CDSVideosFilesIterator.get_master_video_file(record) if master_file: master_deposit_obj = ObjectVersion.get(new_bucket, master_file['key']) for slave in ObjectVersion.query_heads_by_bucket( bucket=new_bucket).join(ObjectVersion.tags).filter( ObjectVersion.file_id.isnot(None), ObjectVersionTag.key == 'master'): ObjectVersionTag.create_or_update( slave, 'master', str(master_deposit_obj.version_id)) db.session.add(slave) db.session.commit() # Delete the old bucket deposit_old_bucket.locked = False _ = deposit_old_bucket.remove() deposit['_buckets']['deposit'] = str(new_bucket.id) record['_buckets']['deposit'] = str(new_bucket.id) record['_deposit'] = deposit['_deposit'] deposit['_files'] = deposit.files.dumps() deposit.commit() record.commit() db.session.commit() return deposit_old_bucket.id, new_bucket.id if not recid: raise ClickException('Missing option "--recid"') pid, record = record_resolver.resolve(recid) old_bucket_id, new_bucket_id = _force_sync_deposit_bucket(record) click.echo( "Deposit bucket re-created from record bucket. Old bucket id: {0} - " "New bucket id: {1}".format(old_bucket_id, new_bucket_id))
def api_project_published(api_app, api_project): """New published project with videos.""" (project, video_1, video_2) = api_project with api_app.test_request_context(): prepare_videos_for_publish([video_1, video_2]) new_project = project.publish() new_videos = [record_resolver.resolve(id_)[1] for id_ in new_project.video_ids] assert len(new_videos) == 2 return (new_project, Video.get_record(new_videos[0].id), Video.get_record(new_videos[1].id))
def extract_frames(recid, depid): """Re-trigger the extract frames task.""" if not recid and not depid: raise ClickException('Missing option "--recid" or "--depid"') if recid: _, record = record_resolver.resolve(recid) depid = record['_deposit']['id'] video_deposit = deposit_video_resolver(depid) master = CDSVideosFilesIterator.get_master_video_file(video_deposit) ExtractFramesTask().si(version_id=master['version_id'], deposit_id=depid).apply_async()
def run(self, preset_quality, sleep_time=5, *args, **kwargs): super(MaintenanceTranscodeVideoTask, self).run(preset_quality=preset_quality, sleep_time=sleep_time, *args, **kwargs) logger.debug("Updating deposit and record") # get deposit and record dep_video = deposit_video_resolver(self.deposit_id) rec_video = record_resolver.resolve(dep_video['recid'])[1] # sync deposit --> record dep_video._sync_record_files(record=rec_video) dep_video.commit() rec_video.commit() db.session.commit()
def test_subformat_creation_if_missing(api_app, location, datadir, es, users): """Test subformat creation if missing.""" # [[ migrate the video ]] migration_streams = get_migration_streams(datadir=datadir) data = load_json(datadir, 'cds_records_demo_1_video.json') dump = CDSRecordDump(data=data[0]) with mock.patch.object(DataCiteProvider, 'register'), \ mock.patch.object(CDSRecordDumpLoader, '_create_frame', side_effect=get_frames), \ mock.patch.object(ExtractFramesTask, '_create_gif'), \ mock.patch.object(CDSRecordDumpLoader, '_clean_file_list'), \ mock.patch.object( CDSRecordDumpLoader, '_get_migration_file_stream_and_size', side_effect=migration_streams): video = CDSRecordDumpLoader.create(dump=dump) db.session.commit() with mock.patch.object(TranscodeVideoTask, 'run') as mock_transcode: deposit = deposit_video_resolver(video['_deposit']['id']) deposit_id = deposit.id # simulate the missing of a subformat del deposit['_files'][0]['subformat'][0] assert len(deposit['_files'][0]['subformat']) == 4 # recreate 240p format CDSRecordDumpLoader._create_missing_subformats( record=video, deposit=deposit) db.session.commit() # check subformats deposit = Video.get_record(deposit_id) rec_video = record_resolver.resolve(video['recid'])[1] # rec_video = record_resolver.resolve(video['recid'])[1] assert len(deposit['_files'][0]['subformat']) == 5 assert len(rec_video['_files'][0]['subformat']) == 5 # check if transcoding is called properly assert mock_transcode.called is True [(_, call_args)] = mock_transcode.call_args_list assert call_args == {'preset_quality': '240p'}