From b396cbdaea14c562466ae559b18357604f61621a Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Fri, 13 Jan 2023 10:40:53 -0500 Subject: [PATCH] sub children --- server.py | 15 ++++++++++---- templates/show.html | 49 +++++++++++++++++++++++++++++---------------- 2 files changed, 43 insertions(+), 21 deletions(-) diff --git a/server.py b/server.py index 3e0ca9c..b1f06f4 100644 --- a/server.py +++ b/server.py @@ -14,21 +14,28 @@ app = Flask(__name__) @app.get("/") def show_all(): - cursor.execute('SELECT * FROM items') + cursor.execute('SELECT * FROM items WHERE parent_id IS NULL') return render_template('show.html', cursor=cursor) @app.post("/") def create(): - cursor.execute('INSERT INTO items (title, description) VALUES(%s,%s)', [request.form['title'],request.form['description']]) - return redirect('/') + if 'parent_id' in request.form: + cursor.execute('INSERT INTO items (title, description,parent_id) VALUES(%s,%s,%s)', [request.form['title'],request.form['description'],int(request.form['parent_id'])]) + return redirect('/'+request.form['parent_id']) + else: + cursor.execute('INSERT INTO items (title, description) VALUES(%s,%s)', [request.form['title'],request.form['description']]) + return redirect('/') @app.post("/") def delete(id): # print(request.args.get('_method')) + # TODO: deal with deleting item that has children cursor.execute('DELETE FROM items WHERE id=%s LIMIT 1', [id]) return redirect('/') @app.get("/") def show(id): cursor.execute('SELECT * FROM items WHERE id=%s', [id]) - return render_template('show.html', cursor=cursor) + parent=cursor.fetchone() + cursor.execute('SELECT * FROM items WHERE parent_id=%s',[id]) + return render_template('show.html', parent=parent, cursor=cursor) diff --git a/templates/show.html b/templates/show.html index f4e42ed..b230839 100644 --- a/templates/show.html +++ b/templates/show.html @@ -6,22 +6,37 @@ -
    - {%for (id, title, description, created_at, updated_at, parent_id) in cursor%} -
  • - {{title}}: {{description}} -
    - -
    -
  • - {%endfor%} -
- -
- - - -
- + {%if parent%} +
+

{{parent[1]}}

+ {{parent[2]}} +
+ {%endif%} +
+
    + {%for (id, title, description, created_at, updated_at, parent_id) in cursor%} +
  • + {{title}} +
    + +
    +
  • + {%endfor%} +
+
+
+

Add Item

+
+ + + {%if parent%} + + {%endif%} + +
+
+
+ To Index +