Browse Source

Added functions for tools

joel
Simon Bowie 3 years ago
parent
commit
bef6f04f76
5 changed files with 107 additions and 7 deletions
  1. +6
    -1
      web/app/templates/base.html
  2. +31
    -0
      web/app/templates/edit.html
  3. +7
    -0
      web/app/templates/tool.html
  4. +14
    -4
      web/app/templates/tools.html
  5. +49
    -2
      web/app/tool.py

+ 6
- 1
web/app/templates/base.html View File

@@ -23,9 +23,14 @@
Home
</a>
</li>
<li class="nav-item">
<a href="{{ url_for('tool.get_tools') }}" class="nav-link">
Tools
</a>
</li>
{% if current_user.is_authenticated %}
<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
</a>
</li>

+ 31
- 0
web/app/templates/edit.html View File

@@ -0,0 +1,31 @@
{% 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 %}

+ 7
- 0
web/app/templates/tool.html View File

@@ -0,0 +1,7 @@
{% 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 %}

+ 14
- 4
web/app/templates/tools.html View File

@@ -1,7 +1,17 @@
{% extends "base.html" %}
{% extends 'base.html' %}

{% 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 %}

+ 49
- 2
web/app/tool.py View File

@@ -1,13 +1,50 @@
from flask import Blueprint, render_template, request, flash, redirect, url_for
from flask_login import login_required, current_user
from .models import Tool
from werkzeug.exceptions import abort
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.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
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':
name = request.form.get('name')
description = request.form.get('description')
@@ -29,3 +66,13 @@ def create():
db.session.commit()

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'))

Loading…
Cancel
Save