-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase.py
60 lines (51 loc) · 2.16 KB
/
Database.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import sqlalchemy as sqlal
from dotenv import load_dotenv
import os
import re
load_dotenv("py.env")
connection = os.getenv("DB_CONNECTION_STRING")
engine = sqlal.create_engine(connection, connect_args={'connect_timeout': 60})
def Add_Account(data):
file = open("UserNames.txt","r")
words = file.read()
UserNames = words.split("\n")
file.close()
if data.get("Username") not in UserNames:
with engine.connect() as conn:
query = sqlal.text("INSERT INTO Accounts (username, password) VALUES (:username, :password)")
conn.execute(query,{"username":data.get("Username"),"password":data.get("Password")})
conn.commit()
query = sqlal.text("INSERT INTO TODO (username, Notes) VALUES (:username, :Notes)")
conn.execute(query,{"username":data.get("Username"),"Notes":""})
conn.commit()
return True
else:return False
def Get_Accounts():
with engine.connect() as conn:
result = conn.execute(sqlal.text("select * from Accounts"))
Accounts = []
for row in result.all():
Accounts.append(dict(row._mapping))
return Accounts
def Verify_Cred(data):
for account in Get_Accounts():
if account["username"] == data.get("Username") and account["password"] == data.get("Password"):
return True
return False
def Add_to_Notes(data):
with engine.connect() as conn:
query = sqlal.text("UPDATE TODO set Notes = CONCAT(Notes,:NOTES,:Space) WHERE username = :name")
conn.execute(query,{"NOTES":data.get("Note"),"Space":"/n","name":data.get("Username")})
conn.commit()
def Show_Notes(data):
with engine.connect() as conn:
query = sqlal.text(f"select Notes from TODO where username = :name")
result = conn.execute(query,{"name":data.get("Username")})
return result.first()[0]
def Delete_Note(data):
Note = Show_Notes(data)
Note = re.sub(f"{data.get('Task')}/n",'',Note)
with engine.connect() as conn:
query = sqlal.text("UPDATE TODO set Notes = :NewNote WHERE username = :name")
conn.execute(query,{"NewNote":Note,"name":data.get("Username")})
conn.commit()