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 mysql.connector
import os import os
# TODO: add breadcrumb
mydb = mysql.connector.connect( mydb = mysql.connector.connect(
host=os.environ.get("HOST"), host=os.environ.get("HOST"),
user=os.environ.get("USER"), user=os.environ.get("USER"),
@ -15,7 +17,10 @@ app = Flask(__name__)
@app.get("/") @app.get("/")
def show_all(): def show_all():
cursor.execute('SELECT * FROM items WHERE parent_id IS NULL') cursor.execute('SELECT * FROM items WHERE parent_id IS NULL')
return render_template('show.html', cursor=cursor) 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("/") @app.post("/")
def create(): def create():
@ -32,9 +37,8 @@ def create():
) )
return redirect('/') return redirect('/')
@app.post("/<id>") @app.post("/<int:id>")
def deleteUpdate(id): def deleteUpdate(id):
print(request.args.get('_method'))
# TODO: deal with deleting item that has children # TODO: deal with deleting item that has children
if request.args.get('_method') == 'DELETE': if request.args.get('_method') == 'DELETE':
cursor.execute('DELETE FROM items WHERE id=%s LIMIT 1', [id]) 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]) cursor.execute('SELECT * FROM items WHERE id=%s', [id])
parent=cursor.fetchone() parent=cursor.fetchone()
cursor.execute('SELECT * FROM items WHERE parent_id=%s',[id]) cursor.execute('SELECT * FROM items WHERE parent_id=%s',[id])
return render_template('show.html', parent=parent, cursor=cursor) 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') @app.get('/<id>/edit')
def edit(id): def edit(id):
cursor.execute('SELECT * FROM items WHERE id=%s', [id]) cursor.execute('SELECT * FROM items WHERE id=%s', [id])
return render_template('edit.html', item=cursor.fetchone()) 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" /> <input type="submit" value="DELETE" />
</form> </form>
</section> </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> </body>
</html> </html>

@ -29,6 +29,17 @@
</li> </li>
{%endfor%} {%endfor%}
</ul> </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>
<section> <section>
<h2>Add Item</h2> <h2>Add Item</h2>

Loading…
Cancel
Save