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():
cursor.execute('INSERT INTO items (title, description) VALUES(%s,%s)', [request.form['title'],request.form['description']]) if 'parent_id' in request.form:
return redirect('/') 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>") @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>
<ul> {%if parent%}
{%for (id, title, description, created_at, updated_at, parent_id) in cursor%} <section>
<li> <h1>{{parent[1]}}</h1>
<a href="/{{id}}">{{title}}: {{description}}</a> {{parent[2]}}
<form method="POST" action="/{{id}}?_method=DELETE"> </section>
<input type="submit" value="DELETE" /> {%endif%}
</form> <section>
</li> <ul>
{%endfor%} {%for (id, title, description, created_at, updated_at, parent_id) in cursor%}
</ul> <li>
<a href="/{{id}}">{{title}}</a>
<form action="/" method="POST"> <form method="POST" action="/{{id}}?_method=DELETE">
<input name="title" type="text"> <input type="submit" value="DELETE" />
<textarea name="description"></textarea> </form>
<input type="submit"/> </li>
</form> {%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> </body>
</html> </html>

Loading…
Cancel
Save