From cf077fe8923f91b122ad888e25c1504850e3bad1 Mon Sep 17 00:00:00 2001 From: benjamyn Date: Wed, 22 Jan 2020 03:06:20 +1100 Subject: [PATCH] Initial commit --- .gitignore | 2 + shop.py | 87 ++++++++++++++++++++++++++++++++++++++++++++ static/css/index.css | 9 +++++ templates/index.html | 59 ++++++++++++++++++++++++++++++ 4 files changed, 157 insertions(+) create mode 100644 .gitignore create mode 100644 shop.py create mode 100644 static/css/index.css create mode 100644 templates/index.html diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..94fce4f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +env/ +list.db \ No newline at end of file diff --git a/shop.py b/shop.py new file mode 100644 index 0000000..6bed573 --- /dev/null +++ b/shop.py @@ -0,0 +1,87 @@ +from flask import Flask, render_template, make_response, request, redirect, url_for +from pprint import pprint +import sqlite3 +import os.path + +DBNAME = "list.db" + +def doesDBExist(DBNAME): + try: + if not os.path.isfile(DBNAME): + conn = sqlite3.connect(DBNAME) + conn.cursor().execute('''CREATE TABLE SHOPLIST (item text, cost real, gotten integer, requester text)''') + conn.close() + except: + return False + return True + + +def readFromDB(): + conn = sqlite3.connect(DBNAME) + results = conn.cursor().execute('''select rowid,* from shoplist''').fetchall() + conn.close() + return results + +def findByName(name): + conn = sqlite3.connect(DBNAME) + query = '''select * from shoplist where requester like ''' + name + results = conn.cursor().execute(query).findall() + conn.close() + return results + +def insertToDB(data): + query = f"INSERT INTO SHOPLIST VALUES (\"{data['item']}\", 0, 0, \"{data['name']}\")" + print(query) + conn = sqlite3.connect(DBNAME) + c = conn.cursor() + c.execute(query) + conn.commit() + conn.close() + + +def deleteRow(rowID): + query = f"DELETE FROM SHOPLIST WHERE ROWID = {rowID}" + conn = sqlite3.connect(DBNAME) + c = conn.cursor() + c.execute(query) + conn.commit() + conn.close() + +def getItem(rowID): + query = f"UPDATE shoplist set gotten = 1 where rowid = {rowID}" + conn = sqlite3.connect(DBNAME) + c = conn.cursor() + c.execute(query) + conn.commit() + conn.close() + +app = Flask(__name__) +app.config["DEBUG"] = True + +@app.route('/') +def index(): + query = readFromDB() + data = {"title":"Cart", "results":query} + return render_template('index.html', data=data) + +@app.route('/post', methods=['POST']) +def handle_data(): + pprint(request.form) + if "addValue" in request.form: + for x in request.form: + # print(x) + if request.form[x] == '': + return redirect(url_for('index')) + insertToDB(request.form) + if "rem" in request.form: + print(request.form["rem"]) + deleteRow(request.form["rem"]) + + if "got" in request.form: + getItem(request.form["got"]) + + return redirect(url_for('index')) + +if __name__ == '__main__': + doesDBExist(DBNAME) + app.run(host="0.0.0.0") \ No newline at end of file diff --git a/static/css/index.css b/static/css/index.css new file mode 100644 index 0000000..c89c51d --- /dev/null +++ b/static/css/index.css @@ -0,0 +1,9 @@ +aside { + width: 20%; + padding-left: .5rem; + margin-left: .5rem; + float: right; + box-shadow: inset 5px 0 5px -5px #000000; + font-style: italic; + color: #000000; +} \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..23031da --- /dev/null +++ b/templates/index.html @@ -0,0 +1,59 @@ + + + + This is my site + + + + + + +

{{ data["title"] }}

+ +
+ + + + + + + + + {% for x in data["results"] %} + + + + + + + + {% endfor %} +
ItemRequesterGottem?Remove
{{x[1]}}{{x[4]}} {% if x[3] == 0 %} +
+ {% else %} + yes + {% endif %}
+ +
+
+ +
+ + + + + + + + + + +