def clean_up_files(snap_id, group_id): ''' Deletes all picture files for a given snap if those pictures sources are designated in the group to be deleted after processing ''' group_document = get_group_document(group_id) if 'image_sources_to_delete' in group_document: image_sources_to_delete = group_document['image_sources_to_delete'].split(',') pictures = search_generic(document_type='picture', args_dict={'snap_id': str(snap_id)}) clean_up_files = True if len(pictures): snap_document = get_generic(snap_id, 'snap') if snap_document['clean_up_files'] == False: # TODO make sure this comparison works clean_up_files = False if clean_up_files: for pic_id in pictures.keys(): if pictures[pic_id]['source'] in image_sources_to_delete: os.remove(pictures[pic_id]['uri']) pictures[pic_id]['uri'] = '' pictures[pic_id]['deleted'] = True else: shutil.move(pictures[pic_id]['uri'], current_app.config['PICTURE_SAVE_DIRECTORY']) pictures[pic_id]['uri'] = os.path.join(current_app.config['PICTURE_SAVE_DIRECTORY'], pictures[pic_id]['filename']) update_generic(pictures[pic_id], 'picture') os.rmdir(os.path.join(current_app.config['PICTURE_SAVE_DIRECTORY'], str(snap_id))) snap_document['files_have_been_cleaned_up'] = True update_generic(snap_document, 'snap')
def clean_up_files(snap_id, group_id): """ Deletes all picture files for a given snap if those pictures sources are designated in the group to be deleted after processing """ group_document = get_group_document(group_id) if "image_sources_to_delete" in group_document: image_sources_to_delete = group_document["image_sources_to_delete"].split(",") pictures = search_generic(document_type="picture", args_dict={"snap_id": str(snap_id)}) clean_up_files = True if len(pictures): snap_document = get_generic(snap_id, "snap") if snap_document["clean_up_files"] == False: # TODO make sure this comparison works clean_up_files = False if clean_up_files: for pic_id in pictures.keys(): if pictures[pic_id]["source"] in image_sources_to_delete: os.remove(pictures[pic_id]["uri"]) pictures[pic_id]["uri"] = "" pictures[pic_id]["deleted"] = True else: shutil.move(pictures[pic_id]["uri"], current_app.config["PICTURE_SAVE_DIRECTORY"]) pictures[pic_id]["uri"] = os.path.join( current_app.config["PICTURE_SAVE_DIRECTORY"], pictures[pic_id]["filename"] ) update_generic(pictures[pic_id], "picture") os.rmdir(os.path.join(current_app.config["PICTURE_SAVE_DIRECTORY"], str(snap_id))) snap_document["files_have_been_cleaned_up"] = True update_generic(snap_document, "snap")
def test_get_generic_throws_error_when_item_doesnt_exist( self, ts_get_document, ts_item_exists): ts_item_exists.return_value = False item_id = uuid.uuid4() with pytest.raises(NotFoundError) as exception_info: ret_val = ts.get_generic(item_id, 'teacher') assert 'teacher not found for id {0}'.format(str(item_id)) in str( exception_info.value)
def test_get_generic_throws_error_when_item_doesnt_exist(self, ts_get_document, ts_item_exists): ts_item_exists.return_value = False item_id = uuid.uuid4() with pytest.raises(NotFoundError) as exception_info: ret_val = ts.get_generic(item_id, 'teacher') assert 'teacher not found for id {0}'.format(str(item_id)) in str(exception_info.value)
def test_get_generic_calls_expected_functions(self, ts_get_document, ts_item_exists): ts_item_exists.return_value = True ts_get_document.return_value = {'gabe': 'kaplan'} item_id = uuid.uuid4() ret_val = ts.get_generic(item_id, 'teacher') assert ts_item_exists.called_once_with(item_id, 'teacher') assert ts_get_document.called_once_with(item_id) assert ret_val == {'gabe': 'kaplan'}