-
Notifications
You must be signed in to change notification settings - Fork 1
/
DB.py
112 lines (84 loc) · 3.04 KB
/
DB.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import sqlite3 as sql
import os
from pathlib import Path
path = os.path.join(Path(__file__).parent, "data")
def startup():
if not os.path.exists(path + "/authdata.db"):
with open(path + "/authdata.db", 'w') as file:
pass
if not os.path.exists(path + "/userdata.db"):
with open(path + "/userdata.db", 'w') as file:
pass
authconn = sql.connect(path + "/authdata.db", check_same_thread=False)
dataconn = sql.connect(path + "/userdata.db", check_same_thread=False)
authcursor = authconn.cursor()
datacursor = dataconn.cursor()
def init_auth_table():
query = """
CREATE TABLE IF NOT EXISTS AUTHDATA(
username VARCHAR,
password VARCHAR
);
"""
authcursor.execute(query)
authconn.commit()
def listAllAuthData():
query = "SELECT * FROM AUTHDATA"
authcursor.execute(query)
ans = authcursor.fetchall()
return ans
def addUser(username, password):
query = f"INSERT INTO AUTHDATA(username, password) VALUES(?, ?)"
authcursor.execute(query, (username, password))
authconn.commit()
query = "CREATE TABLE IF NOT EXISTS {}(sitename VARCHAR, email VARCHAR, password VARCHAR, username VARCHAR);".format(username)
datacursor.execute(query)
dataconn.commit()
return listAllAuthData()
def selectiveUsername(username):
query = "SELECT password FROM AUTHDATA WHERE username =:username "
authcursor.execute(query, {'username':username})
return authcursor.fetchall()
def fetchAllUserdata(username):
datacursor.execute('SELECT * FROM {}'.format(username))
return datacursor.fetchall()
def fetchUserDataSite(username, sitename):
datacursor.execute('SELECT * FROM {} WHERE sitename = {}'.format(username, sitename))
return datacursor.fetchall()
def addSite(username, sitename, email, password, site_username):
query = f"INSERT INTO {username}(sitename, email, password, username) VALUES(?,?,?,?)"
datacursor.execute(query, (sitename, email, password, site_username))
dataconn.commit()
return fetchAllUserdata(username)
def correct_login(username, password):
if selectiveUsername(username) != []:
if selectiveUsername(username)[0][0] == username and selectiveUsername(username)[0][0] == password:
return username, password
else:
return "error"
else:
pass
def convertUserDataToJson(username):
data = fetchAllUserdata(username)
ans = []
for i in data:
ans.append({
'sitename':i[0],
'email':i[1],
'password':i[2],
'username':i[3],
})
return ans
def deletesite(username, site):
query = f"DELETE FROM {username} WHERE sitename =:site"
datacursor.execute(query, {'site': site})
dataconn.commit()
def deleteUser(username):
query = f"DELETE FROM AUTHDATA WHERE username =:username"
authcursor.execute(query, {'username' : username})
authconn.commit()
query = f"DROP TABLE {username}"
datacursor.execute(query)
dataconn.commit()
startup()
init_auth_table()