sub children

master
Matt Huntington 3 years ago
parent aa935b4cfc
commit b396cbdaea

@ -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():
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("/<id>")
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("/<id>")
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)

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

Loading…
Cancel
Save