def test_031_edit_task_modifies_task(self): from invenio_checker.api import create_task, edit_task # Given a task create_task(TestApi.task_branched) # Edit it edited_task = edit_task(TestApi.task_branched['name'], TestApi.task_edit_data) # Pull it out of the database and check that it's what we asked for task_in_db = CheckerRule.query.filter(CheckerRule.name == TestApi.task_edited['name']).one() for key, val in TestApi.task_edited.iteritems(): assert getattr(task_in_db, key) == val assert task_in_db == edited_task
def submit_task(): """Insert or modify an existing task and its reporters.""" from invenio_checker.clients.supervisor import run_task def failure(type_, errors): assert type_ in ('general', 'validation') return jsonify({'failure_type': type_, 'errors': errors}), 400 def success(): return jsonify({}) # Recreate the forms that we have previously served to the user so that we # can validate. form_origin = get_NewTaskForm(request.form) form_plugin = get_ArgForm(request.form['plugin'], request.form) if not (form_origin.validate() & form_plugin.validate()): form_errors = defaultdict(list) for field, errors in chain(form_origin.errors.items(), form_plugin.errors.items()): form_errors[field].extend(errors) return failure('validation', form_errors) # Get a dictionary that we can pass as kwargs to the database object, form_for_db = dict(form_origin.data) # but first, pop metadata out of it. modify = form_for_db.pop('modify') original_name = form_for_db.pop('original_name') requested_action = form_for_db.pop('requested_action') reporter_names = set(form_for_db.pop('reporters')) form_for_db['arguments'] = form_plugin.data_for_db try: # Create or edit task if modify: task = edit_task(original_name, form_for_db, commit=False) else: task = create_task(form_for_db, commit=False) # Create or edit reporters and attach them as well for reporter_name in reporter_names: form_reporter = get_ArgForm(reporter_name, request.form) try: reporter = get_reporter_db(form_reporter.plugin_name, original_name) except NoResultFound: create_reporter({'plugin': form_reporter.plugin_name, 'rule_name': task.name, 'arguments': form_reporter.data_for_db}, commit=False) else: edit_reporter(reporter, {'plugin': form_reporter.plugin_name, 'rule_name': task.name, 'arguments': form_reporter.data_for_db}, commit=False) if modify: # Delete reporters that are no longer selected attached_reporter_plugin_names = {reporter.plugin for reporter in task.reporters} for plugin_name_to_remove in attached_reporter_plugin_names - reporter_names: ex_reporter = get_reporter_db(plugin_name_to_remove, original_name) remove_reporter(ex_reporter, commit=False) db.session.commit() except Exception as e: db.session.rollback() return failure('general', format_exc()) if requested_action.startswith('submit_run'): try: run_task(task.name) except Exception as e: return failure('general', format_exc()) return success()
def submit_task(): """Insert or modify an existing task and its reporters.""" from invenio_checker.clients.supervisor import run_task def failure(type_, errors): assert type_ in ('general', 'validation') return jsonify({'failure_type': type_, 'errors': errors}), 400 def success(): return jsonify({}) # Recreate the forms that we have previously served to the user so that we # can validate. form_origin = get_NewTaskForm(request.form) form_plugin = get_ArgForm(request.form['plugin'], request.form) if not (form_origin.validate() & form_plugin.validate()): form_errors = defaultdict(list) for field, errors in chain(form_origin.errors.items(), form_plugin.errors.items()): form_errors[field].extend(errors) return failure('validation', form_errors) # Get a dictionary that we can pass as kwargs to the database object, form_for_db = dict(form_origin.data) # but first, pop metadata out of it. modify = form_for_db.pop('modify') original_name = form_for_db.pop('original_name') requested_action = form_for_db.pop('requested_action') reporter_names = set(form_for_db.pop('reporters')) form_for_db['arguments'] = form_plugin.data_for_db try: # Create or edit task if modify: task = edit_task(original_name, form_for_db, commit=False) else: task = create_task(form_for_db, commit=False) # Create or edit reporters and attach them as well for reporter_name in reporter_names: form_reporter = get_ArgForm(reporter_name, request.form) try: reporter = get_reporter_db(form_reporter.plugin_name, original_name) except NoResultFound: create_reporter( { 'plugin': form_reporter.plugin_name, 'rule_name': task.name, 'arguments': form_reporter.data_for_db }, commit=False) else: edit_reporter(reporter, { 'plugin': form_reporter.plugin_name, 'rule_name': task.name, 'arguments': form_reporter.data_for_db }, commit=False) if modify: # Delete reporters that are no longer selected attached_reporter_plugin_names = { reporter.plugin for reporter in task.reporters } for plugin_name_to_remove in attached_reporter_plugin_names - reporter_names: ex_reporter = get_reporter_db(plugin_name_to_remove, original_name) remove_reporter(ex_reporter, commit=False) db.session.commit() except Exception as e: db.session.rollback() return failure('general', format_exc()) if requested_action.startswith('submit_run'): try: run_task(task.name) except Exception as e: return failure('general', format_exc()) return success()