Implemented users
Changed DB type to MySQL Cried
This commit is contained in:
parent
a450e8aea2
commit
22d030edd6
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,2 +1,4 @@
|
|||||||
env/
|
env/
|
||||||
|
.vscode/
|
||||||
list.db
|
list.db
|
||||||
|
.config
|
||||||
4
config
Normal file
4
config
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
[mysql]
|
||||||
|
Username = SQLUsernam
|
||||||
|
Password = SQLPassword
|
||||||
|
Database = DBNAME
|
||||||
2
requirements.txt
Normal file
2
requirements.txt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
flask
|
||||||
|
mysql-connector
|
||||||
92
shop.py
92
shop.py
@ -1,55 +1,74 @@
|
|||||||
from flask import Flask, render_template, make_response, request, redirect, url_for, session
|
from flask import Flask, render_template, make_response, request, redirect, url_for, session
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
import sqlite3
|
import mysql.connector
|
||||||
|
import configparser
|
||||||
import os.path
|
import os.path
|
||||||
|
|
||||||
DBNAME = "list.db"
|
config = configparser.ConfigParser()
|
||||||
MOBILES = ["android", "iphone", "blackberry"]
|
MOBILES = ["android", "iphone", "blackberry"]
|
||||||
|
|
||||||
|
|
||||||
def doesDBExist(DBNAME):
|
|
||||||
try:
|
try:
|
||||||
if not os.path.isfile(DBNAME):
|
config.read(".config")
|
||||||
conn = sqlite3.connect(DBNAME)
|
except Exception as E:
|
||||||
conn.cursor().execute('''CREATE TABLE SHOPLIST (item text, cost real, gotten integer, requester text)''')
|
print(E)
|
||||||
conn.close()
|
|
||||||
except:
|
def dbConnect():
|
||||||
return False
|
mydb = mysql.connector.connect(
|
||||||
return True
|
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):
|
def runQuery(query):
|
||||||
conn = sqlite3.connect(DBNAME)
|
mydb = dbConnect()
|
||||||
c = conn.cursor()
|
c = mydb.cursor()
|
||||||
ret = c.execute(query)
|
c.execute(query)
|
||||||
conn.commit()
|
print(f'Excuting {query}')
|
||||||
|
if "select" in query.lower():
|
||||||
|
ret = c.fetchall()
|
||||||
|
else:
|
||||||
|
ret = []
|
||||||
|
mydb.commit()
|
||||||
|
mydb.close()
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def readFromDB():
|
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)
|
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):
|
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)
|
runQuery(query)
|
||||||
|
|
||||||
|
|
||||||
def deleteRow(rowID):
|
def deleteRow(rowID):
|
||||||
query = f"DELETE FROM SHOPLIST WHERE ROWID = {rowID}"
|
query = f"DELETE FROM SHOPLIST WHERE id = {rowID}"
|
||||||
runQuery(query)
|
runQuery(query)
|
||||||
|
|
||||||
def getItem(rowID):
|
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)
|
runQuery(query)
|
||||||
|
|
||||||
def unGetItem(rowID):
|
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)
|
runQuery(query)
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
@ -58,11 +77,12 @@ app.secret_key = b'*$#@U9423jr92jioJKL_)_;dasfj()12'
|
|||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
def index():
|
def index():
|
||||||
if 'allowed' not in session:
|
if session.get('id') is None:
|
||||||
data = {"title":"Login"}
|
data = {"title":"Login"}
|
||||||
return render_template("auth.html", data=data)
|
return render_template("auth.html", data=data)
|
||||||
|
|
||||||
query = readFromDB()
|
query = readFromDB()
|
||||||
|
pprint(query)
|
||||||
data = {"title":"Shopping List", "results":query}
|
data = {"title":"Shopping List", "results":query}
|
||||||
for device in MOBILES:
|
for device in MOBILES:
|
||||||
if device in request.user_agent.platform:
|
if device in request.user_agent.platform:
|
||||||
@ -76,23 +96,31 @@ def handle_data():
|
|||||||
# print(x)
|
# print(x)
|
||||||
if request.form[x] == '':
|
if request.form[x] == '':
|
||||||
return redirect(url_for('index'))
|
return redirect(url_for('index'))
|
||||||
insertToDB(request.form)
|
print(request.form)
|
||||||
|
insertToDB({"item": request.form["item"], "name": session["id"]})
|
||||||
if "rem" in request.form:
|
if "rem" in request.form:
|
||||||
print(request.form["rem"])
|
print(request.form["rem"])
|
||||||
deleteRow(request.form["rem"])
|
deleteRow(request.form["rem"])
|
||||||
|
|
||||||
if "got" in request.form:
|
if "got" in request.form:
|
||||||
|
print(request.form["got"])
|
||||||
getItem(request.form["got"])
|
getItem(request.form["got"])
|
||||||
|
|
||||||
if "ungot" in request.form:
|
if "ungot" in request.form:
|
||||||
unGetItem(request.form["ungot"])
|
unGetItem(request.form["ungot"])
|
||||||
|
|
||||||
if "loginform" in request.form:
|
if "loginform" in request.form:
|
||||||
if request.form["password"] == "theloves2020":
|
query = "select id, username from USERS where username = '%s' and password = md5('%s')" % (request.form["username"], request.form["password"])
|
||||||
session["allowed"] = "allowed"
|
|
||||||
|
|
||||||
|
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'))
|
return redirect(url_for('index'))
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
doesDBExist(DBNAME)
|
doesTableExist()
|
||||||
app.run(host="0.0.0.0")
|
app.run(host="0.0.0.0")
|
||||||
|
|||||||
@ -1,3 +1,6 @@
|
|||||||
|
* {
|
||||||
|
color: azure;
|
||||||
|
}
|
||||||
aside {
|
aside {
|
||||||
width: 20%;
|
width: 20%;
|
||||||
padding-left: .5rem;
|
padding-left: .5rem;
|
||||||
@ -8,3 +11,26 @@ aside {
|
|||||||
font-style: italic;
|
font-style: italic;
|
||||||
color: #000000;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,9 +1,7 @@
|
|||||||
<div>
|
<div>
|
||||||
<form action="/post" method="post">
|
<form action="/post" method="post">
|
||||||
<p style="display: inline">Name: </p>
|
|
||||||
<input class="form-control" style="display: inline" type="text" name="name" value=""><br>
|
|
||||||
<p style="display: inline">Item: </p>
|
<p style="display: inline">Item: </p>
|
||||||
<input class="form-control" style="display: inline" type="text" name="item" value=""><br><br>
|
<input class="form-control" id="addbox" style="display: inline" type="text" name="item" value=""><br><br>
|
||||||
<div style="text-align: center; padding-bottom: 2px"><input class="btn btn-secondary" type="submit" style="text-align: center" name="addValue" value="Submit"></form>
|
<div style="text-align: center; padding-bottom: 2px"><input class="btn btn-secondary" type="submit" style="text-align: center" name="addValue" value="Submit"></form>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
@ -3,8 +3,9 @@
|
|||||||
<h1 class="container" style="text-align: center">{{ data["title"] }}</h1>
|
<h1 class="container" style="text-align: center">{{ data["title"] }}</h1>
|
||||||
|
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<form style="text-align: center" action="/post" method="POST">
|
<form class="loginform" style="text-align: center" action="/post" method="POST">
|
||||||
<input type="password" name="password"/>
|
<input id="addbox" type="text" name="username" />
|
||||||
|
<input id="addbox" type="password" name="password"/>
|
||||||
<input class="btn btn-secondary" type="submit" name="loginform" value="Login" />
|
<input class="btn btn-secondary" type="submit" name="loginform" value="Login" />
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
|||||||
@ -5,5 +5,6 @@
|
|||||||
<!-- Latest compiled and minified CSS -->
|
<!-- Latest compiled and minified CSS -->
|
||||||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
|
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||||
|
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
|
||||||
<link rel="stylesheet" content="text/css" href="/static/css/main.css"/>
|
<link rel="stylesheet" content="text/css" href="/static/css/main.css"/>
|
||||||
</head>
|
</head>
|
||||||
@ -1,12 +1,22 @@
|
|||||||
{% include "header.html" %}
|
{% include "header.html" %}
|
||||||
<body>
|
<body>
|
||||||
<h1 class="container" style="text-align: center">{{ data["title"] }}</h1>
|
<h1 class="container" style="text-align: center">{{ data["title"] }}</h1>
|
||||||
<aside>
|
<!-- <aside> -->
|
||||||
|
<div id="addForm">
|
||||||
{% include "addForm.html" %}
|
{% include "addForm.html" %}
|
||||||
</aside>
|
</div>
|
||||||
<div class="container" id="memes">
|
</div>
|
||||||
|
<!-- </aside> -->
|
||||||
|
<div class="container" >
|
||||||
{% include "list.html" %}
|
{% include "list.html" %}
|
||||||
</div>
|
</div>
|
||||||
|
<div id="logout">
|
||||||
|
<form action="/post" method="POST">
|
||||||
|
<button type="submit" name="logout" class="btn btn-secondary"><i style="padding-top: 3px;" class="material-icons">
|
||||||
|
exit_to_app
|
||||||
|
</i></button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
<!-- jQuery library -->
|
<!-- jQuery library -->
|
||||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
|
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
<table class="table-striped table">
|
<table class="table table-striped table-dark table-hover">
|
||||||
<thead class="thead-dark">
|
<thead class="">
|
||||||
<tr>
|
<tr>
|
||||||
<th scope="col">Item</th>
|
<th scope="col">Item</th>
|
||||||
<th scope="col">Requester</th>
|
<th scope="col">Requester</th>
|
||||||
@ -7,11 +7,12 @@
|
|||||||
<th scope="col">Remove</th>
|
<th scope="col">Remove</th>
|
||||||
</tr>
|
</tr>
|
||||||
{% for x in data["results"] %}
|
{% for x in data["results"] %}
|
||||||
<tr id="row-{{x[0]}}">
|
<tbody>
|
||||||
|
<tr scope="row" id="row-{{x[0]}}">
|
||||||
<td>{{x[1]}}</td>
|
<td>{{x[1]}}</td>
|
||||||
<td>{{x[4]}}</td>
|
<td>{{x[3].title()}}</td>
|
||||||
|
|
||||||
<td> {% if x[3] == 0 %}
|
<td> {% if x[2] == 0 %}
|
||||||
<form><button class="btn btn-secondary" type="submit" formaction="/post" formmethod="POST" name="got" value="{{x[0]}}">No</button></form>
|
<form><button class="btn btn-secondary" type="submit" formaction="/post" formmethod="POST" name="got" value="{{x[0]}}">No</button></form>
|
||||||
{% else %}
|
{% else %}
|
||||||
<form><button class="btn btn-secondary" type="submit" formaction="/post" formmethod="POST" name="ungot" value="{{x[0]}}">Yes</button></form>
|
<form><button class="btn btn-secondary" type="submit" formaction="/post" formmethod="POST" name="ungot" value="{{x[0]}}">Yes</button></form>
|
||||||
@ -22,4 +23,5 @@
|
|||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
@ -8,7 +8,13 @@
|
|||||||
{% include "list.html" %}
|
{% include "list.html" %}
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
<div id="logout">
|
||||||
|
<form action="/post" method="POST">
|
||||||
|
<button type="submit" name="logout" class="btn btn-secondary"><i style="padding-top: 3px;" class="material-icons">
|
||||||
|
exit_to_app
|
||||||
|
</i></button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
<!-- jQuery library -->
|
<!-- jQuery library -->
|
||||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
|
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user