master
Matt Huntington 3 years ago
parent 86e81270e9
commit de1a89f47e

@ -1,7 +1,9 @@
from flask import Flask, render_template, request, redirect
from flask import Flask, render_template, request, redirect, make_response
import mysql.connector
import os
# TODO: add breadcrumb
mydb = mysql.connector.connect(
host=os.environ.get("HOST"),
user=os.environ.get("USER"),
@ -15,6 +17,9 @@ app = Flask(__name__)
@app.get("/")
def show_all():
cursor.execute('SELECT * FROM items WHERE parent_id IS NULL')
if request.cookies.get('movingItemId'):
return render_template('show.html', cursor=cursor, moving_item_id=request.cookies.get('movingItemId'))
else:
return render_template('show.html', cursor=cursor)
@app.post("/")
@ -32,9 +37,8 @@ def create():
)
return redirect('/')
@app.post("/<id>")
@app.post("/<int:id>")
def deleteUpdate(id):
print(request.args.get('_method'))
# TODO: deal with deleting item that has children
if request.args.get('_method') == 'DELETE':
cursor.execute('DELETE FROM items WHERE id=%s LIMIT 1', [id])
@ -53,9 +57,28 @@ def show(id):
cursor.execute('SELECT * FROM items WHERE id=%s', [id])
parent=cursor.fetchone()
cursor.execute('SELECT * FROM items WHERE parent_id=%s',[id])
if request.cookies.get('movingItemId'):
return render_template('show.html', parent=parent, cursor=cursor, moving_item_id=request.cookies.get('movingItemId'))
else:
return render_template('show.html', parent=parent, cursor=cursor)
@app.get('/<id>/edit')
def edit(id):
cursor.execute('SELECT * FROM items WHERE id=%s', [id])
return render_template('edit.html', item=cursor.fetchone())
@app.post('/<id>/move')
@app.post('/move')
def move(id=None):
if request.args.get('_method') == 'PUT':
cursor.execute('UPDATE items SET parent_id = %s WHERE id = %s LIMIT 1', [id if id else None, request.cookies.get('movingItemId')])
response = make_response(redirect('/' + id))
response.set_cookie('movingItemId', '', expires = 0)
return response
else:
if 'parent_id' in request.form:
response = make_response(redirect('/'+request.form['parent_id']))
else:
response = make_response(redirect('/'))
response.set_cookie('movingItemId', id)
return response

@ -28,5 +28,14 @@
<input type="submit" value="DELETE" />
</form>
</section>
<section>
<h2>Move Item</h2>
<form method="POST" action="/{{item[0]}}/move">
{%if item[5]%}
<input type="hidden" value="{{item[5]}}" name="parent_id"/>
{%endif%}
<input type="submit" value="MOVE"/>
</form>
</section>
</body>
</html>

@ -29,6 +29,17 @@
</li>
{%endfor%}
</ul>
{%if moving_item_id %}
{% if parent %}
<form action="/{{parent[0]}}/move?_method=PUT" method="POST">
<input type="submit" value="Move Item Here"/>
</form>
{% else %}
<form action="/move?_method=PUT" method="POST">
<input type="submit" value="Move Item Here"/>
</form>
{% endif %}
{% endif %}
</section>
<section>
<h2>Add Item</h2>

Loading…
Cancel
Save