def load_reporters(invenio_rule, execution): """ ..note:: Calling `get_reporter` on the module also runs any initialization code. """ from invenio_checker.models import get_reporter_db all_reporters = [] for reporter in invenio_rule.reporters: db_reporter = get_reporter_db(reporter.module.__name__, invenio_rule.name) all_reporters.append(reporter.module.get_reporter(db_reporter, execution)) return all_reporters
def test_033_reporter_is_registered(self): """Attach a reporter to a task and see if it's actually attached.""" from invenio_checker.api import create_task, create_reporter from invenio_checker.models import get_reporter_db # Given a task new_task = create_task(TestApi.task_data) # Attach a reporter to it new_reporter = create_reporter(TestApi.reporter_data) # Make sure the reporter was created reporter_from_db = get_reporter_db(**TestApi.reporter_data) assert reporter_from_db.plugin == TestApi.reporter_data['plugin'] assert reporter_from_db.rule_name == TestApi.reporter_data['rule_name'] # and attached to the task assert new_task.reporters == [new_reporter]
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()