diff --git a/.gitignore b/.gitignore index 94fce4f..9839a7b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ env/ -list.db \ No newline at end of file +.vscode/ +list.db +.config \ No newline at end of file diff --git a/config b/config new file mode 100644 index 0000000..2956e3e --- /dev/null +++ b/config @@ -0,0 +1,4 @@ +[mysql] +Username = SQLUsernam +Password = SQLPassword +Database = DBNAME \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..1caab0c --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +flask +mysql-connector \ No newline at end of file diff --git a/shop.py b/shop.py index f458f6d..e646b7f 100644 --- a/shop.py +++ b/shop.py @@ -1,55 +1,74 @@ from flask import Flask, render_template, make_response, request, redirect, url_for, session from pprint import pprint -import sqlite3 +import mysql.connector +import configparser import os.path -DBNAME = "list.db" +config = configparser.ConfigParser() MOBILES = ["android", "iphone", "blackberry"] +try: + config.read(".config") +except Exception as E: + print(E) -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 dbConnect(): + mydb = mysql.connector.connect( + host="localhost", + user=config["mysql"]["Username"], + passwd=config["mysql"]["Password"], + database=config["mysql"]["Database"] + ) + return mydb + + +def doesTableExist(): + mydb = dbConnect() + mycursor = mydb.cursor() + mycursor.execute('''SHOW TABLES''') + tables = mycursor.fetchall() + if len(tables) == 0: + mycursor.execute('''CREATE TABLE SHOPLIST (id INT AUTO_INCREMENT PRIMARY KEY, item VARCHAR(255), gotten BOOLEAN, user_id INT, FOREIGN KEY (`user_id`) REFERENCES `USERS`(`id`) + ON DELETE CASCADE)''') + mycursor.execute('''CREATE TABLE USERS (id INT AUTO_INCREMENT PRIMARY KEY, username VARCHAR(255), password VARCHAR(255))''') + mydb.close() def runQuery(query): - conn = sqlite3.connect(DBNAME) - c = conn.cursor() - ret = c.execute(query) - conn.commit() + mydb = dbConnect() + c = mydb.cursor() + c.execute(query) + print(f'Excuting {query}') + if "select" in query.lower(): + ret = c.fetchall() + else: + ret = [] + mydb.commit() + mydb.close() return ret def readFromDB(): - query = '''select rowid,* from shoplist''' + # query = '''select * from SHOPLIST''' + query = '''select SHOPLIST.id, SHOPLIST.item, SHOPLIST.gotten, USERS.username from SHOPLIST inner join USERS on SHOPLIST.user_id = USERS.id''' return runQuery(query) -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']}\")" + query = f"INSERT INTO SHOPLIST (item, gotten, user_id) VALUES (\"{data['item']}\", 0, {data['name']})" + # print(query) runQuery(query) def deleteRow(rowID): - query = f"DELETE FROM SHOPLIST WHERE ROWID = {rowID}" + query = f"DELETE FROM SHOPLIST WHERE id = {rowID}" runQuery(query) def getItem(rowID): - query = f"UPDATE shoplist set gotten = 1 where rowid = {rowID}" + query = f"UPDATE SHOPLIST set gotten = 1 where id = {rowID}" + print(query) runQuery(query) def unGetItem(rowID): - query = f"UPDATE shoplist set gotten = 0 where rowid = {rowID}" + query = f"UPDATE SHOPLIST set gotten = 0 where id = {rowID}" + runQuery(query) app = Flask(__name__) @@ -58,11 +77,12 @@ app.secret_key = b'*$#@U9423jr92jioJKL_)_;dasfj()12' @app.route('/') def index(): - if 'allowed' not in session: + if session.get('id') is None: data = {"title":"Login"} return render_template("auth.html", data=data) - + query = readFromDB() + pprint(query) data = {"title":"Shopping List", "results":query} for device in MOBILES: if device in request.user_agent.platform: @@ -76,23 +96,31 @@ def handle_data(): # print(x) if request.form[x] == '': return redirect(url_for('index')) - insertToDB(request.form) + print(request.form) + insertToDB({"item": request.form["item"], "name": session["id"]}) if "rem" in request.form: print(request.form["rem"]) deleteRow(request.form["rem"]) if "got" in request.form: + print(request.form["got"]) getItem(request.form["got"]) if "ungot" in request.form: unGetItem(request.form["ungot"]) if "loginform" in request.form: - if request.form["password"] == "theloves2020": - session["allowed"] = "allowed" + query = "select id, username from USERS where username = '%s' and password = md5('%s')" % (request.form["username"], request.form["password"]) + res = runQuery(query) + if len(res) != 0: + session["id"] = res[0][0] + session["username"] = res[0][1] + + if "logout" in request.form: + session.clear() return redirect(url_for('index')) if __name__ == '__main__': - doesDBExist(DBNAME) + doesTableExist() app.run(host="0.0.0.0") diff --git a/static/css/main.css b/static/css/main.css index 48ddc44..74275b3 100644 --- a/static/css/main.css +++ b/static/css/main.css @@ -1,3 +1,6 @@ +* { + color: azure; +} aside { width: 20%; padding-left: .5rem; @@ -8,3 +11,26 @@ aside { font-style: italic; color: #000000; } + +#logout { + position: fixed; + top: 2px; + left: 2px; +} + +#addForm { + position: fixed; + top: 2em; + right: 2px; + padding-right: 10px; +} + +#addbox { + background-color: #2a2a2a; + color: azure; +} + +body { + background-color: #2a2a2a; +} + diff --git a/templates/addForm.html b/templates/addForm.html index 0d20fd7..2440c1c 100644 --- a/templates/addForm.html +++ b/templates/addForm.html @@ -1,9 +1,7 @@
-

Name:

-

Item:

-

+

\ No newline at end of file diff --git a/templates/auth.html b/templates/auth.html index a0d40ee..5d4622a 100644 --- a/templates/auth.html +++ b/templates/auth.html @@ -3,8 +3,9 @@

{{ data["title"] }}

-
- + + +
diff --git a/templates/header.html b/templates/header.html index db08c1f..c2f60d0 100644 --- a/templates/header.html +++ b/templates/header.html @@ -5,5 +5,6 @@ + \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index 4ba6f15..4c7316e 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,12 +1,22 @@ {% include "header.html" %}

{{ data["title"] }}

- -
+
+
+ +
{% include "list.html" %}
+
+
+ +
+
diff --git a/templates/list.html b/templates/list.html index cd523be..1c71d82 100644 --- a/templates/list.html +++ b/templates/list.html @@ -1,5 +1,5 @@ - - +
+ @@ -7,11 +7,12 @@ {% for x in data["results"] %} - + + - + - {% endfor %} +
Item RequesterRemove
{{x[1]}}{{x[4]}}{{x[3].title()}} {% if x[3] == 0 %} + {% if x[2] == 0 %}
{% else %}
@@ -22,4 +23,5 @@
\ No newline at end of file diff --git a/templates/mobile.html b/templates/mobile.html index 17d783a..ea8f0bb 100644 --- a/templates/mobile.html +++ b/templates/mobile.html @@ -8,7 +8,13 @@ {% include "list.html" %}
- +
+
+ +
+