101 lines
2.7 KiB
Python
Executable File
101 lines
2.7 KiB
Python
Executable File
#!/usr/bin/env python
|
|
from flask import Flask, render_template, request
|
|
from pprint import pprint
|
|
import sqlite3
|
|
import time
|
|
import os.path
|
|
|
|
|
|
def createDB():
|
|
if os.path.isfile('list.db'):
|
|
print("DB Exists")
|
|
else:
|
|
conn = sqlite3.connect('list.db')
|
|
c = conn.cursor()
|
|
c.execute('''CREATE TABLE LIST (
|
|
id integer PRIMARY KEY, requester text, item text,
|
|
quatity integer, gotten text, whenis real)''')
|
|
conn.close()
|
|
|
|
|
|
def insTestData(c, conn, data):
|
|
c.execute("INSERT INTO list VALUES ('Tim', 'Memes', 100, 0)")
|
|
conn.commit()
|
|
|
|
|
|
def getData():
|
|
with sqlite3.connect('list.db') as conn:
|
|
c = conn.cursor()
|
|
res = c.execute("SELECT * FROM LIST")
|
|
data = res.fetchall()
|
|
# pprint(data)
|
|
return data
|
|
|
|
|
|
def parseData(data):
|
|
print("Parsing Data >>")
|
|
results = []
|
|
# pprint(data)
|
|
for x in range(len(data)):
|
|
results.append({"id": data[x][0], "req": data[x][1],
|
|
"item": data[x][2], "quantity": data[x][3],
|
|
"gotten": data[x][4], "time": data[x][5]})
|
|
# pprint(results)
|
|
return results
|
|
|
|
|
|
def addRow(req):
|
|
for item in req:
|
|
for value in item:
|
|
if value == "":
|
|
print("OwO there has been a fuckey wukey :(")
|
|
return
|
|
with sqlite3.connect('list.db') as conn:
|
|
c = conn.cursor()
|
|
print(req)
|
|
c.executemany('INSERT INTO LIST VALUES (?,?,?,?,?,?)', req)
|
|
conn.commit()
|
|
|
|
|
|
def haveGotten(value):
|
|
with sqlite3.connect('list.db') as conn:
|
|
c = conn.cursor()
|
|
c.executemany('UPDATE LIST SET GOTTEN = "TRUE" WHERE id = ?', value)
|
|
conn.commit()
|
|
createDB()
|
|
|
|
app = Flask(__name__)
|
|
app.config['DEBUG'] = True
|
|
|
|
|
|
@app.route('/', methods=['POST', 'GET'])
|
|
def index():
|
|
if request.method == 'POST':
|
|
if 'addItem' in request.form:
|
|
req = [(None, request.form['requester'], request.form['item'],
|
|
request.form['quantity'],
|
|
"False", time.mktime(time.localtime()))]
|
|
addRow(req)
|
|
tmpdata = getData()
|
|
data = parseData(tmpdata)
|
|
return render_template('index.html', data=data)
|
|
if 'gotten' in request.form:
|
|
# update DB
|
|
haveGotten(request.form['id'])
|
|
tmpdata = getData()
|
|
data = parseData(tmpdata)
|
|
return render_template('index.html', data=data)
|
|
|
|
else:
|
|
tmpdata = getData()
|
|
data = parseData(tmpdata)
|
|
return render_template('index.html', data=data)
|
|
|
|
#insTestData(2)
|
|
|
|
if __name__ == '__main__':
|
|
|
|
#ret = c.execute("SELECT * from LIST WHERE requester = 'Tim'")
|
|
#print(ret.fetchall())
|
|
app.run(host='0.0.0.0')
|