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.

104 lines
3.6 KiB

from flask import Flask, render_template, request, redirect, make_response, Request
import mysql.connector
import os
# TODO: add breadcrumb
class HTTPMethodOverrideMiddleware(object):
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
request = Request(environ)
if request.args.get('_method'):
environ['REQUEST_METHOD'] = request.args.get('_method')
return self.app(environ, start_response)
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.wsgi_app = HTTPMethodOverrideMiddleware(app.wsgi_app)
@app.get("/")
def show_all():
cursor.execute('SELECT * FROM items WHERE parent_id IS NULL')
if request.cookies.get('movingItemId'):
return render_template('show.html', children=cursor, moving_item_id=request.cookies.get('movingItemId'))
else:
return render_template('show.html', children=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.put("/<int:id>")
def deleteUpdate(id):
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.delete('/<int:id>')
def delete(id):
# TODO: deal with deleting item that has children
cursor.execute('DELETE FROM items WHERE id=%s LIMIT 1', [id])
if 'parent_id' in request.form:
return redirect('/'+request.form['parent_id'])
else:
return redirect('/')
@app.get("/<int: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])
if request.cookies.get('movingItemId'):
return render_template('show.html', parent=parent, children=cursor, moving_item_id=request.cookies.get('movingItemId'))
else:
return render_template('show.html', parent=parent, children=cursor)
@app.get('/<int:id>/edit')
def edit(id):
cursor.execute('SELECT * FROM items WHERE id=%s', [id])
return render_template('edit.html', item=cursor.fetchone())
@app.post('/<int:id>/move')
def move(id):
if 'parent_id' in request.form:
response = make_response(redirect('/'+request.form['parent_id']))
else:
response = make_response(redirect('/'))
response.set_cookie('movingItemId', str(id))
return response
@app.put('/<int:id>/move')
@app.put('/move')
def updateParentId(id=None):
cursor.execute('UPDATE items SET parent_id = %s WHERE id = %s LIMIT 1', [id, request.cookies.get('movingItemId')])
if id:
response = make_response(redirect('/' + str(id)))
else:
response = make_response(redirect('/'))
response.set_cookie('movingItemId', '', expires = 0)
return response