def delete_portfolio_export(folio_export, history_user, history_info, _db_session=None): """ Deletes a portfolio export record and the associated zip file (if it exists), and adds an audit trail entry for the parent portfolio. If you supply a database session it will be committed before the zip file is deleted, so that files are only deleted once the database operations are known to have worked. Raises a ServerTooBusyError if the export is still in progress. Raises an OSError if the zip file or directory cannot be deleted. """ db_session = _db_session or data_engine.db_get_session() try: # Ensure we can access folio_export.portfolio if not data_engine.object_in_session(folio_export, db_session): folio_export = data_engine.get_object(FolioExport, folio_export.id, _db_session=db_session) # Check whether the export task is running if folio_export.task_id: task = task_engine.get_task(folio_export.task_id, _db_session=db_session) if (task and task.status == Task.STATUS_ACTIVE) or ( task and task.status == Task.STATUS_PENDING and not task_engine.cancel_task(task)): raise ServerTooBusyError( 'this export is currently in progress, wait a while then try again' ) # Delete and add history in one commit data_engine.add_portfolio_history(folio_export.portfolio, history_user, FolioHistory.ACTION_UNPUBLISHED, history_info, _db_session=db_session, _commit=False) data_engine.delete_object(folio_export, _db_session=db_session, _commit=True) # If we got this far the database delete worked and we now need to # delete the exported zip file zip_rel_path = get_portfolio_export_file_path(folio_export) if folio_export.filename: delete_file(zip_rel_path) # And if the zip directory is now empty, delete the directory too zip_rel_dir = get_portfolio_directory(folio_export.portfolio) if path_exists(zip_rel_dir, require_directory=True): zips_count = count_files(zip_rel_dir, recurse=False) if zips_count[0] == 0: delete_dir(zip_rel_dir) finally: if not _db_session: db_session.close()
def test_task_result_none(self): # Test no return value task_obj = tm.add_task(None, 'Test return values', 'test_result_task', { 'raise_exception': False, 'return_value': None }, Task.PRIORITY_NORMAL, 'info', 'error', 5) self.assertIsNotNone(task_obj) tm.wait_for_task(task_obj.id, 10) task_obj = tm.get_task(task_obj.id, decode_attrs=True) self.assertIsNone(task_obj.result) dm.delete_object(task_obj)
def test_task_result_object(self): # Test normal return value task_obj = tm.add_task( None, 'Test return values', 'test_result_task', {'raise_exception': False, 'return_value': {'my_bool': True}}, Task.PRIORITY_NORMAL, 'info', 'error', 5 ) self.assertIsNotNone(task_obj) tm.wait_for_task(task_obj.id, 20) task_obj = tm.get_task(task_obj.id, decode_attrs=True) self.assertEqual(task_obj.result, {'my_bool': True}) dm.delete_object(task_obj)
def get(self, task_id): db_task = task_engine.get_task(task_id=task_id, decode_attrs=True) if not db_task: raise DoesNotExistError(str(task_id)) else: # Requires super user or task owner if not db_task.user or db_task.user.id != get_session_user_id(): permissions_engine.ensure_permitted(SystemPermissions.PERMIT_SUPER_USER, get_session_user()) tdict = object_to_dict(db_task) if tdict.get("user") is not None: # Do not give out anything password related del tdict["user"]["password"] return make_api_success_response(tdict)
def test_task_cancel(self): task_obj = tm.add_task(None, 'Test task cancelling', 'test_result_task', { 'raise_exception': False, 'return_value': None }, Task.PRIORITY_LOW, 'info', 'error', 0) self.assertIsNotNone(task_obj) self.assertGreater(task_obj.id, 0) # Yes, this could be a fragile test if the task server gets to it first # It has worked the first 5 times in a row I've tried it, so fingers crossed self.assertTrue(tm.cancel_task(task_obj)) task_obj = tm.get_task(task_obj.id) self.assertIsNone(task_obj)
def test_task_result_exception(self): # Test exception raised task_obj = tm.add_task( None, 'Test return values', 'test_result_task', {'raise_exception': True, 'return_value': None}, Task.PRIORITY_NORMAL, 'info', 'error', 5 ) self.assertIsNotNone(task_obj) tm.wait_for_task(task_obj.id, 20) task_obj = tm.get_task(task_obj.id, decode_attrs=True) self.assertIsInstance(task_obj.result, ValueError) self.assertEqual(repr(task_obj.result), repr(ValueError('An error happened'))) dm.delete_object(task_obj)
def test_task_result_exception(self): # Test exception raised task_obj = tm.add_task(None, 'Test return values', 'test_result_task', { 'raise_exception': True, 'return_value': None }, Task.PRIORITY_NORMAL, 'info', 'error', 5) self.assertIsNotNone(task_obj) tm.wait_for_task(task_obj.id, 10) task_obj = tm.get_task(task_obj.id, decode_attrs=True) self.assertIsInstance(task_obj.result, ValueError) self.assertEqual(repr(task_obj.result), repr(ValueError('An error happened'))) dm.delete_object(task_obj)