# @name: tool.py # @version: 0.1 # @creation_date: 2021-10-20 # @license: The MIT License # @author: Simon Bowie # @purpose: tool route for tool-related functions and pages # @acknowledgements: # https://www.digitalocean.com/community/tutorials/how-to-make-a-web-application-using-flask-in-python-3 from flask import Blueprint, render_template, request, flash, redirect, url_for from flask_login import login_required, current_user from .models import Resource from .resources import * from werkzeug.exceptions import abort from . import db tool = Blueprint('tool', __name__) # route for displaying all tools in database @tool.route('/tools') def get_tools(): tools = Resource.query.filter_by(type='tool') return render_template('tools.html', tools=tools) # route for displaying a single tool based on the ID in the database @tool.route('/tools/') def show_tool(tool_id): tool = get_resource(tool_id) resources = get_linked_resources(tool_id) return render_template('tool.html', tool=tool, resources=resources) # route for editing a single tool based on the ID in the database @tool.route('/tools//edit', methods=('GET', 'POST')) @login_required def edit_tool(tool_id): tool = get_resource(tool_id) if request.method == 'POST': name = request.form['name'] description = request.form['description'] if not name: flash('Name is required!') else: tool = Resource.query.get(tool_id) tool.name = name tool.description = description # tool.projectUrl = project_url # tool.repositoryUrl = repository_url # tool.dependencies = dependencies # tool.expertiseToUse = expertise # tool.expertiseToHost = self_host_expertise # tool.ingestFormats = ingest # tool.outputFormats = output # tool.status = status db.session.commit() return redirect(url_for('tool.get_tools')) return render_template('edit.html', resource=tool) # route for function to delete a single tool from the edit page @tool.route('/tools//delete', methods=('POST',)) @login_required def delete_tool(tool_id): delete_resource(tool_id) return redirect(url_for('tool.get_tools'))