diff --git a/server.py b/server.py index b8c3bd6..70ebf4d 100644 --- a/server.py +++ b/server.py @@ -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,7 +17,10 @@ app = Flask(__name__) @app.get("/") def show_all(): 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("/") def create(): @@ -32,9 +37,8 @@ def create(): ) return redirect('/') -@app.post("/") +@app.post("/") 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]) - 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('//edit') def edit(id): cursor.execute('SELECT * FROM items WHERE id=%s', [id]) return render_template('edit.html', item=cursor.fetchone()) + +@app.post('//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 diff --git a/templates/edit.html b/templates/edit.html index 128ab0d..3227184 100644 --- a/templates/edit.html +++ b/templates/edit.html @@ -28,5 +28,14 @@ +
+

Move Item

+
+ {%if item[5]%} + + {%endif%} + +
+
diff --git a/templates/show.html b/templates/show.html index d8b0233..7f737a5 100644 --- a/templates/show.html +++ b/templates/show.html @@ -29,6 +29,17 @@ {%endfor%} + {%if moving_item_id %} + {% if parent %} +
+ +
+ {% else %} +
+ +
+ {% endif %} + {% endif %}

Add Item