def detach_node(scenario_id): """Detach scenario node from the structure tree.""" scenario_manager = ScenarioManager(get_autostorage_api()) single_scenario_manager = scenario_manager.get_scenario(scenario_id) tree_path = request.args.get('tree_path') single_scenario_manager.detach_node(tree_path=tree_path) return redirect(url_for('single_scenario', scenario_id=scenario_id))
def download_report(file_id): """Download report file.""" api = get_autostorage_api() report_manager = ReportManager(api) report_file_query = report_manager.get_report_files().filter_by(file_id=file_id) with api.get_session() as session: report_file = report_file_query.with_session(session).one() file_path = report_file.file_path return send_file(REPORTS.path(file_path))
def attach_node(scenario_id): """Attach scenario node to the structure tree.""" scenario_manager = ScenarioManager(get_autostorage_api()) single_scenario_manager = scenario_manager.get_scenario(scenario_id) parent_tree_path = request.args.get('parent_tree_path') node_id = int(request.args.get('node_id')) single_scenario_manager.attach_node(node_ids=[node_id], parent_tree_path=parent_tree_path) return redirect(url_for('single_scenario', scenario_id=scenario_id))
def single_scenario(scenario_id): """Return page for single scenario.""" api = get_autostorage_api() scenario_manager = ScenarioManager(api) single_scenario_manager = scenario_manager.get_scenario(scenario_id) with api.get_session() as session: view_states = single_scenario_manager.get_view_states().with_session(session).all() current_view_state = view_states[0] current_name = current_view_state.name current_description = current_view_state.description name_form = EditScenarioNameForm(original_name=current_name) description_form = EditScenarioDescriptionForm(original_description=current_description) if name_form.rename.data and name_form.validate_on_submit(): single_scenario_manager.rename(name_form.name.data) flash('Your changes have been saved.') return redirect(url_for('single_scenario', scenario_id=scenario_id)) else: name_form.name.data = current_name if description_form.change_description.data and description_form.validate_on_submit(): single_scenario_manager.change_description(description_form.description.data) flash('Your changes have been saved.') return redirect(url_for('single_scenario', scenario_id=scenario_id)) else: description_form.description.data = current_description structure_tree = _create_tree(single_scenario_manager) node_manager = ScenarioNodeManager(api) with api.get_session() as session: node_states = node_manager.get_scenario_node_states().with_session(session).all() return render_template( "single_scenario.html", scenario_id=scenario_id, scenario_name=current_name, scenario_description=current_description, view_states=view_states, name_form=name_form, description_form=description_form, structure_tree=structure_tree, node_states=node_states )
def scenario_nodes(): """Return page with scenario nodes.""" new_node_form = CreateScenarioNodeForm() api = get_autostorage_api() node_manager = ScenarioNodeManager(api) if new_node_form.validate_on_submit(): node_manager.create_scenario_node(new_node_form.name.data) with api.get_session() as session: node_states = node_manager.get_scenario_node_states().with_session(session).all() return render_template( "scenario_nodes.html", node_states=node_states, new_node_form=new_node_form, )
def scenarios(): """Return page with scenarios.""" new_scenario_form = CreateScenarioForm() api = get_autostorage_api() scenario_manager = ScenarioManager(api) if new_scenario_form.validate_on_submit(): single_scenario_manager = scenario_manager.create_scenario(new_scenario_form.name.data) single_scenario_manager.change_description(new_scenario_form.description.data) with api.get_session() as session: view_states = scenario_manager.get_scenarios_view_states().with_session(session).all() return render_template( "scenarios.html", view_states=view_states, new_scenario_form=new_scenario_form, )
def reports(): """Return page with report files.""" api = get_autostorage_api() report_manager = ReportManager(api) upload_form = UploadReportFileForm() if upload_form.validate_on_submit(): report_file = upload_form.report_file.data prepared_name = secure_filename(report_file.filename) # pylint: disable=no-member file_name = REPORTS.save(report_file, name=prepared_name) report_manager.add_report_file(file_name) flash("File has been uploaded") return redirect(url_for("reports")) with api.get_session() as session: report_files = report_manager.get_report_files().with_session(session).all() return render_template("reports.html", report_files=report_files, upload_form=upload_form)
def _create_tree(single_scenario_manager): # pylint: disable=too-many-locals """Create view tree.""" api = get_autostorage_api() structure_query = single_scenario_manager.get_current_structure_states().subquery() node_manager = ScenarioNodeManager(api) nodes_query = node_manager.get_scenario_node_states().subquery() joined = structure_query.join( nodes_query, structure_query.columns.node_id == nodes_query.columns.node_id ) with api.get_session() as session: tree_nodes_data = session.query( select( columns=[ structure_query.columns.tree_path, nodes_query.columns.name, nodes_query.columns.node_id ], from_obj=joined ) ).all() tree = {} for tree_path, name, node_id in tree_nodes_data: path = tree_path.split('-') parent_key = path[0] subtree = tree.setdefault(parent_key, {}) for key in path[1:]: parent_key = parent_key + '-' + key subtree = subtree.setdefault("children", {}).setdefault(parent_key, {}) subtree['name'] = name subtree['node_id'] = node_id return tree