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/
|
||||
.vscode/
|
||||
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 pprint import pprint
|
||||
import sqlite3
|
||||
import mysql.connector
|
||||
import configparser
|
||||
import os.path
|
||||
|
||||
DBNAME = "list.db"
|
||||
config = configparser.ConfigParser()
|
||||
MOBILES = ["android", "iphone", "blackberry"]
|
||||
|
||||
|
||||
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
|
||||
config.read(".config")
|
||||
except Exception as E:
|
||||
print(E)
|
||||
|
||||
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")
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
||||
@ -1,9 +1,7 @@
|
||||
<div>
|
||||
<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>
|
||||
<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>
|
||||
</form>
|
||||
</div>
|
||||
@ -3,8 +3,9 @@
|
||||
<h1 class="container" style="text-align: center">{{ data["title"] }}</h1>
|
||||
|
||||
<div class="container">
|
||||
<form style="text-align: center" action="/post" method="POST">
|
||||
<input type="password" name="password"/>
|
||||
<form class="loginform" style="text-align: center" action="/post" method="POST">
|
||||
<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" />
|
||||
</form>
|
||||
|
||||
|
||||
@ -5,5 +5,6 @@
|
||||
<!-- Latest compiled and minified 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">
|
||||
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
|
||||
<link rel="stylesheet" content="text/css" href="/static/css/main.css"/>
|
||||
</head>
|
||||
@ -1,12 +1,22 @@
|
||||
{% include "header.html" %}
|
||||
<body>
|
||||
<h1 class="container" style="text-align: center">{{ data["title"] }}</h1>
|
||||
<aside>
|
||||
<!-- <aside> -->
|
||||
<div id="addForm">
|
||||
{% include "addForm.html" %}
|
||||
</aside>
|
||||
<div class="container" id="memes">
|
||||
</div>
|
||||
</div>
|
||||
<!-- </aside> -->
|
||||
<div class="container" >
|
||||
{% include "list.html" %}
|
||||
</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 -->
|
||||
<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">
|
||||
<thead class="thead-dark">
|
||||
<table class="table table-striped table-dark table-hover">
|
||||
<thead class="">
|
||||
<tr>
|
||||
<th scope="col">Item</th>
|
||||
<th scope="col">Requester</th>
|
||||
@ -7,11 +7,12 @@
|
||||
<th scope="col">Remove</th>
|
||||
</tr>
|
||||
{% for x in data["results"] %}
|
||||
<tr id="row-{{x[0]}}">
|
||||
<tbody>
|
||||
<tr scope="row" id="row-{{x[0]}}">
|
||||
<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>
|
||||
{% else %}
|
||||
<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>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
@ -8,7 +8,13 @@
|
||||
{% include "list.html" %}
|
||||
|
||||
</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 -->
|
||||
<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