sub children

master
Matt Huntington 3 years ago
parent aa935b4cfc
commit b396cbdaea

@ -14,21 +14,28 @@ app = Flask(__name__)
@app.get("/") @app.get("/")
def show_all(): 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) return render_template('show.html', cursor=cursor)
@app.post("/") @app.post("/")
def create(): def create():
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']]) cursor.execute('INSERT INTO items (title, description) VALUES(%s,%s)', [request.form['title'],request.form['description']])
return redirect('/') return redirect('/')
@app.post("/<id>") @app.post("/<id>")
def delete(id): def delete(id):
# print(request.args.get('_method')) # print(request.args.get('_method'))
# TODO: deal with deleting item that has children
cursor.execute('DELETE FROM items WHERE id=%s LIMIT 1', [id]) cursor.execute('DELETE FROM items WHERE id=%s LIMIT 1', [id])
return redirect('/') return redirect('/')
@app.get("/<id>") @app.get("/<id>")
def show(id): def show(id):
cursor.execute('SELECT * FROM items WHERE id=%s', [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)

@ -6,22 +6,37 @@
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
</head> </head>
<body> <body>
{%if parent%}
<section>
<h1>{{parent[1]}}</h1>
{{parent[2]}}
</section>
{%endif%}
<section>
<ul> <ul>
{%for (id, title, description, created_at, updated_at, parent_id) in cursor%} {%for (id, title, description, created_at, updated_at, parent_id) in cursor%}
<li> <li>
<a href="/{{id}}">{{title}}: {{description}}</a> <a href="/{{id}}">{{title}}</a>
<form method="POST" action="/{{id}}?_method=DELETE"> <form method="POST" action="/{{id}}?_method=DELETE">
<input type="submit" value="DELETE" /> <input type="submit" value="DELETE" />
</form> </form>
</li> </li>
{%endfor%} {%endfor%}
</ul> </ul>
</section>
<section>
<h2>Add Item</h2>
<form action="/" method="POST"> <form action="/" method="POST">
<input name="title" type="text"> <input name="title" type="text">
<textarea name="description"></textarea> <textarea name="description"></textarea>
{%if parent%}
<input type="hidden" name="parent_id" value="{{parent[0]}}"/>
{%endif%}
<input type="submit"/> <input type="submit"/>
</form> </form>
</section>
<section>
<a href="/">To Index</a>
</section>
</body> </body>
</html> </html>

Loading…
Cancel
Save