Added some stuff

This commit is contained in:
Benjamyn Love 2018-08-16 18:57:42 +10:00
parent d8d4016803
commit 9f076442c8
3 changed files with 64 additions and 13 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
env/
bak/
list.db

50
main.py
View File

@ -2,6 +2,7 @@
from flask import Flask, render_template, request
from pprint import pprint
import sqlite3
import time
import os.path
@ -12,7 +13,8 @@ def createDB():
conn = sqlite3.connect('list.db')
c = conn.cursor()
c.execute('''CREATE TABLE LIST (
requester text, item text, quatity integer, gotten text)''')
id integer PRIMARY KEY, requester text, item text,
quatity integer, gotten text, whenis real)''')
conn.close()
@ -26,11 +28,23 @@ def getData():
c = conn.cursor()
res = c.execute("SELECT * FROM LIST")
data = res.fetchall()
pprint(data)
# pprint(data)
return data
def updateDB(req):
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 == "":
@ -39,7 +53,14 @@ def updateDB(req):
with sqlite3.connect('list.db') as conn:
c = conn.cursor()
print(req)
c.executemany('INSERT INTO LIST VALUES (?,?,?,?)', 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()
@ -50,13 +71,24 @@ app.config['DEBUG'] = True
@app.route('/', methods=['POST', 'GET'])
def index():
if request.method == 'POST':
req = [(request.form['requester'], request.form['item'],
request.form['quantity'], "False")]
updateDB(req)
data = getData()
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:
data = getData()
tmpdata = getData()
data = parseData(tmpdata)
return render_template('index.html', data=data)
#insTestData(2)

View File

@ -4,14 +4,14 @@
</head>
<body style="background: #868686">
<h1>Shopping list </h1>
<table style="width:100%; justify-content: left">
<table >
<tr>
<th>Requester</th>
<th>Item</th>
<th>Quantity</th>
<th>Got It?</th>
</tr>
{% for res in data %}
<!-- {% for res in data %}
<tr>
{% for field in res %}
<td>
@ -19,7 +19,25 @@
</td>
{% endfor %}
</tr>
{% endfor %} -->
{% for lists in data %}
<tr>
<td> {{ lists['req'] }}</td>
<td> {{ lists['item'] }}</td>
<td> {{ lists['quantity'] }}</td>
<td> {% if lists['gotten'] == "False" %}
no
{% else %}
yes
{% endif %}</td>
<td> <form action="/" method="post">
<input type="submit" name="gotten" value="*">
<input type="hidden" name="id" value="{{ lists['id'] }}" />
</form> </td>
</tr>
{% endfor %}
</table>
<table>
<form action="/" method="post">
@ -36,7 +54,7 @@
<td><input type="text" name='quantity'/><br/></td>
</tr>
<tr>
<td><input type="submit" value="Add Item"/></td>
<td><input type="submit" name="addItem" value="Add Item"/></td>
</tr>
</form>
</table>