| Home | Home | ||||
| </a> | </a> | ||||
| </li> | </li> | ||||
| <li class="nav-item"> | |||||
| <a href="{{ url_for('tool.get_tools') }}" class="nav-link"> | |||||
| Tools | |||||
| </a> | |||||
| </li> | |||||
| {% if current_user.is_authenticated %} | {% if current_user.is_authenticated %} | ||||
| <li class="nav-item"> | <li class="nav-item"> | ||||
| <a href="{{ url_for('tool.create') }}" class="nav-link"> | |||||
| <a href="{{ url_for('tool.create_tool') }}" class="nav-link"> | |||||
| Add tool | Add tool | ||||
| </a> | </a> | ||||
| </li> | </li> |
| {% extends 'base.html' %} | |||||
| {% block content %} | |||||
| <h1>{% block title %} Edit "{{ tool['name'] }}" {% endblock %}</h1> | |||||
| <form method="post"> | |||||
| <div class="form-group"> | |||||
| <label for="name">Name</label> | |||||
| <input type="text" name="name" placeholder="Tool name" | |||||
| class="form-control" | |||||
| value="{{ request.form['name'] or tool['name'] }}"> | |||||
| </input> | |||||
| </div> | |||||
| <div class="form-group"> | |||||
| <label for="description">Description</label> | |||||
| <textarea name="description" placeholder="Tool description" | |||||
| class="form-control">{{ request.form['description'] or tool['description'] }}</textarea> | |||||
| </div> | |||||
| <div class="form-group"> | |||||
| <button type="submit" class="btn btn-primary">Submit</button> | |||||
| </div> | |||||
| </form> | |||||
| <hr> | |||||
| <form action="{{ url_for('tool.delete_tool', tool_id=tool['id']) }}" method="POST"> | |||||
| <input type="submit" value="Delete Tool" | |||||
| class="btn btn-danger btn-sm" | |||||
| onclick="return confirm('Are you sure you want to delete this tool?')"> | |||||
| </form> | |||||
| {% endblock %} |
| {% extends 'base.html' %} | |||||
| {% block content %} | |||||
| <h2>{% block title %} {{ tool['name'] }} {% endblock %}</h2> | |||||
| <span class="badge badge-primary">{{ tool['created'] }}</span> | |||||
| <p>{{ tool['description'] }}</p> | |||||
| {% endblock %} |
| {% extends "base.html" %} | |||||
| {% extends 'base.html' %} | |||||
| {% block content %} | {% block content %} | ||||
| <h1 class="title"> | |||||
| Welcome, {{ name }}! | |||||
| </h1> | |||||
| <h1>{% block title %} Tools {% endblock %}</h1> | |||||
| {% for tool in tools %} | |||||
| <a href="{{ url_for('tool.show_tool', tool_id=tool['id']) }}"> | |||||
| <h2>{{ tool['name'] }}</h2> | |||||
| </a> | |||||
| <span class="badge badge-primary">{{ tool['created'] }}</span> | |||||
| {% if current_user.is_authenticated %} | |||||
| <a href="{{ url_for('tool.edit_tool', tool_id=tool['id']) }}"> | |||||
| <span class="badge badge-warning">Edit</span> | |||||
| </a> | |||||
| {% endif %} | |||||
| <hr> | |||||
| {% endfor %} | |||||
| {% endblock %} | {% endblock %} |
| from flask import Blueprint, render_template, request, flash, redirect, url_for | from flask import Blueprint, render_template, request, flash, redirect, url_for | ||||
| from flask_login import login_required, current_user | from flask_login import login_required, current_user | ||||
| from .models import Tool | from .models import Tool | ||||
| from werkzeug.exceptions import abort | |||||
| from . import db | from . import db | ||||
| def get_tool(tool_id): | |||||
| tool = Tool.query.filter_by(id=tool_id).first() | |||||
| if tool is None: | |||||
| abort(404) | |||||
| return tool | |||||
| tool = Blueprint('tool', __name__) | tool = Blueprint('tool', __name__) | ||||
| @tool.route('/create', methods=('GET', 'POST')) | |||||
| @tool.route('/tools') | |||||
| def get_tools(): | |||||
| tools = Tool.query | |||||
| return render_template('tools.html', tools=tools) | |||||
| @tool.route('/tools/<int:tool_id>') | |||||
| def show_tool(tool_id): | |||||
| tool = get_tool(tool_id) | |||||
| return render_template('tool.html', tool=tool) | |||||
| @tool.route('/tools/<int:tool_id>/edit', methods=('GET', 'POST')) | |||||
| @login_required | @login_required | ||||
| def create(): | |||||
| def edit_tool(tool_id): | |||||
| tool = get_tool(tool_id) | |||||
| if request.method == 'POST': | |||||
| name = request.form['name'] | |||||
| description = request.form['description'] | |||||
| if not name: | |||||
| flash('Name is required!') | |||||
| else: | |||||
| tool = Tool.query.get(tool_id) | |||||
| tool.name = name | |||||
| tool.description = description | |||||
| db.session.commit() | |||||
| return redirect(url_for('tool.get_tools')) | |||||
| return render_template('edit.html', tool=tool) | |||||
| @tool.route('/tools/create', methods=('GET', 'POST')) | |||||
| @login_required | |||||
| def create_tool(): | |||||
| if request.method == 'POST': | if request.method == 'POST': | ||||
| name = request.form.get('name') | name = request.form.get('name') | ||||
| description = request.form.get('description') | description = request.form.get('description') | ||||
| db.session.commit() | db.session.commit() | ||||
| return render_template('create.html') | return render_template('create.html') | ||||
| @tool.route('/tools/<int:tool_id>/delete', methods=('POST',)) | |||||
| @login_required | |||||
| def delete_tool(tool_id): | |||||
| tool = get_tool(tool_id) | |||||
| deletion = Tool.query.get(tool_id) | |||||
| db.session.delete(deletion) | |||||
| db.session.commit() | |||||
| flash('Successfully deleted!') | |||||
| return redirect(url_for('tool.get_tools')) |