You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
2.0 KiB
62 lines
2.0 KiB
from flask import Flask, render_template, request, redirect
|
|
import mysql.connector
|
|
import os
|
|
|
|
mydb = mysql.connector.connect(
|
|
host=os.environ.get("HOST"),
|
|
user=os.environ.get("USER"),
|
|
password=os.environ.get("PASSWORD"),
|
|
database="new_trello"
|
|
)
|
|
mydb.autocommit = True
|
|
cursor = mydb.cursor()
|
|
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)
|
|
|
|
@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 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])
|
|
else:
|
|
cursor.execute(
|
|
'UPDATE items SET title=%s, description=%s, parent_id=%s WHERE id=%s',
|
|
[request.form['title'], request.form['description'], request.form['parent_id'] if request.form['parent_id'] != '' else None, id]
|
|
)
|
|
if 'parent_id' in request.form:
|
|
return redirect('/'+request.form['parent_id'])
|
|
else:
|
|
return redirect('/')
|
|
|
|
@app.get("/<id>")
|
|
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)
|
|
|
|
@app.get('/<id>/edit')
|
|
def edit(id):
|
|
cursor.execute('SELECT * FROM items WHERE id=%s', [id])
|
|
return render_template('edit.html', item=cursor.fetchone())
|