-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase.py
314 lines (285 loc) · 11.9 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import sqlite3
import random
import string
from passlib.hash import sha256_crypt
class Database:
def __init__(self, databaseName):
self.databaseName = databaseName
with sqlite3.connect(self.databaseName) as con:
cur = con.cursor()
queries = [
"""
CREATE TABLE IF NOT EXISTS AUTHOR (
AuthID TEXT,
FirstName TEXT,
LastName TEXT,
Birthday TIMESTAMP,
CountryOfResidence TEXT,
HrsWritingPerDay REAL,
PRIMARY KEY(AuthID));
""",
"""
CREATE TABLE IF NOT EXISTS Book (
BookID TEXT,
Title TEXT,
OnlineSalesWebsite TEXT,
AvaliableSalesAreas TEXT,
Stock INTEGER,
TotalNumberOfOrders INTEGER,
AuthID TEXT,
Img BLOB,
FOREIGN KEY(AuthID) REFERENCES AUTHOR(AuthID) ON DELETE CASCADE,
PRIMARY KEY(BookID)
);
""",
"""
CREATE TABLE IF NOT EXISTS Users (
user_id Integer,
name char NOT NULL,
username char NOT NULL UNIQUE,
password char NOT NULL,
email char NOT NULL,
wishList char,
FOREIGN KEY(wishList) REFERENCES Book(BookID) ON DELETE SET NULL,
PRIMARY KEY(user_id AUTOINCREMENT)
);
""",
]
for query in queries:
cur.execute(query)
print("ok")
def __connect_database(self):
con = sqlite3.connect(self.databaseName)
con.row_factory = sqlite3.Row
return con
def show_all_books(self):
cur = self.__connect_database().cursor()
cur.execute("SELECT * From Book")
rows = cur.fetchall()
return rows
def make_fav(self, username, key):
message = ""
try:
with sqlite3.connect(self.databaseName) as con:
message = ""
cur = con.cursor()
query = "Update Users set WishList=? where username = ?"
cur.execute(query, (key, username))
message = "Sucess"
except Exception as exp:
message = str(exp.args) + "Failed"
return message
# TODO: doldur
def get_fav_book(self, username):
message = ""
try:
with sqlite3.connect(self.databaseName) as con:
cur = con.cursor()
query = "Select Title from Users join Book on BookID = wishlist Where wishlist = BookID and username = ? "
cur.execute(query, (username,))
message = "Success"
return cur.fetchone()
except Exception as e:
message = "Failed " + str(e.args)
return message
def show_all_authors(self):
cur = self.__connect_database().cursor()
cur.execute("SELECT * FROM Author")
rows = cur.fetchall()
return rows
def register(self, name, username, email, password):
message = ""
try:
with sqlite3.connect(self.databaseName) as con:
cur = con.cursor()
query = (
"Insert Into Users(name,username,password,email) Values(?,?,?,?)"
)
cur.execute(query, (name, username, password, email))
message = "Success"
except Exception as exp:
message = str(exp.args) + "Failed"
return message
def login(self, username, password):
message = ""
success = False
try:
with sqlite3.connect(self.databaseName) as con:
cur = con.cursor()
# query = "Select From Author Where name = ? and password = ?"
query = "Select password From Users Where username = ?"
cur.execute(query, (username,))
uname_response = cur.fetchone()
if uname_response:
if sha256_crypt.verify(password, uname_response[0]):
message = "Success"
success = True
else:
message = "Wrong Password"
else:
message = "Wrong username"
except Exception as exp:
message = "Failed" + str(exp.args)
return message, success
def add_author(self, firstName, lastName, birthday, country, hrs):
if not (firstName and lastName):
message = "First Name and Last Name must be proivded."
else:
try:
letters = string.ascii_uppercase
digits = string.digits
newId = "".join(random.choice(letters) for i in range(2)) + "".join(
random.choice(digits) for i in range(3)
)
with sqlite3.connect(self.databaseName) as con:
cur = con.cursor()
id_query = "Select AuthID from Author"
cur.execute(id_query)
id_list = list(cur.fetchall())
while (newId,) in id_list:
newId = "".join(
random.choice(letters) for i in range(2)
) + "".join(random.choice(digits) for i in range(3))
add_query = "Insert Into AUTHOR Values(?,?,?,?,?,?)"
cur.execute(
add_query, (newId, firstName, lastName, birthday, country, hrs)
)
message = "Successful"
except Exception as exp:
message = "Failed" + exp.args
return message
def remove_author(self, authorId):
try:
with sqlite3.connect(self.databaseName) as con:
cur = con.cursor()
query = "Select * From Author Where (AuthID = ? )"
cur.execute(query, (str(authorId),))
if len(cur.fetchall()) == 0:
exception_meessage = "No such author"
raise Exception(exception_meessage)
cur.execute("PRAGMA foreign_keys = ON;")
query = "Delete From AUTHOR Where(AuthID = ?)"
cur.execute(query, (str(authorId),))
message = "Success"
except Exception as exp:
message = exp.args
return message
def delete_user(self, username):
message = ""
try:
with sqlite3.connect(self.databaseName) as con:
cur = con.cursor()
query = "Delete From Users Where username = ? "
cur.execute(query, (username,))
message = "Success"
except Exception as e:
message = "Failed " + str(e.args)
return message
def update_author(self, key, firstName, lastName, birthday, country, hrs):
authorId = key
try:
with sqlite3.connect(self.databaseName) as con:
cur = con.cursor()
old_data_query = "Select * From Author Where (AuthId = ?)"
cur.execute(old_data_query, (str(key),))
author = cur.fetchall()
qFname = firstName if firstName != "" else author[0][1]
qLname = lastName if lastName != "" else author[0][2]
qBirthday = birthday if birthday != "" else author[0][3]
qCountry = country if country != "" else author[0][4]
qHrs = hrs if hrs != "" else author[0][5]
update_query = "Update AUTHOR Set FirstName = ?, LastName = ?,Birthday = ?,CountryOfResidence = ?, HrsWritingPerDay= ? Where AuthID = ?"
cur.execute(
update_query, (qFname, qLname, qBirthday, qCountry, qHrs, authorId)
)
message = "Successful"
except Exception as e:
message = "Failed"
message += str(e.args)
return message
def show_author_info(self, key):
row = {}
message = ""
try:
with sqlite3.connect(self.databaseName) as con:
cur = con.cursor()
query = """Select Author.AuthID, FirstName, LastName,Birthday,CountryOfResidence,HrsWritingPerDay
From Book Join Author on (Book.AuthID = Author.AuthID)
Where Author.AuthID = ? """
cur.execute(query, (key,))
row = cur.fetchall()
return row, message
except Exception as exp:
message = "This author cannot be found in the database " + str(exp.args)
return row, message
def update_book(self, key, website, area, stock, title, order):
bookId = key
try:
with sqlite3.connect(self.databaseName) as con:
cur = con.cursor()
old_data_query = "Select * From Book Where (BookID = ?)"
cur.execute(old_data_query, (str(key),))
book = cur.fetchall()
q_website = website if website != "" else book[0][2]
q_area = area if area != "" else book[0][3]
q_stock = stock if stock != "" else book[0][4]
q_title = title if title != "" else book[0][1]
q_order = order if order != "" else book[0][5]
update_query = "Update Book Set Title = ?, OnlineSalesWebSite = ?,AvaliableSalesAreas = ?,Stock = ?, TotalNumberOfOrders= ? Where BookID = ?"
cur.execute(
update_query,
(q_title, q_website, q_area, q_stock, q_order, bookId),
)
message = "Successful"
except Exception as e:
message = "Failed"
message += str(e.args)
return message
def add_book(self, title, website, area, stock, authId):
if not (title and authId):
message = "Title and AuthorID must be proivded."
else:
try:
letters = string.ascii_uppercase
digits = string.digits
newId = "".join(random.choice(letters) for i in range(2)) + "".join(
random.choice(digits) for i in range(3)
)
with sqlite3.connect(self.databaseName) as con:
cur = con.cursor()
auth_query = "Select * From AUTHOR Where AuthID = ?"
if len(cur.execute(auth_query, (authId,)).fetchall()) == 0:
raise Exception("No such author")
id_query = "Select BookID from Book"
cur.execute(id_query)
id_list = list(cur.fetchall())
while (newId,) in id_list:
newId = "".join(
random.choice(letters) for i in range(2)
) + "".join(random.choice(digits) for i in range(3))
add_query = "Insert Into Book (BookID,Title,OnlineSalesWebsite,AvaliableSalesAreas,Stock,TotalNumberOfOrders,AuthID) Values (?,?,?,?,?,?,?)"
cur.execute(
add_query,
(newId, title, website, area, stock, 0, authId),
)
message = "Successful"
except Exception as exp:
message = "Failed" + str(exp.args)
return message
def remove_book(self, bookId):
message = ""
try:
with sqlite3.connect(self.databaseName) as con:
cur = con.cursor()
query = "Select * From Book Where (BookID = ?)"
cur.execute(query, (str(bookId),))
if len(cur.fetchall()) == 0:
exception_meessage = "No such book"
raise Exception(exception_meessage)
cur.execute("PRAGMA foreign_keys = ON;")
query = "Delete From Book Where(BookID = ?)"
cur.execute(query, (str(bookId),))
message = "Success"
except Exception as exp:
message = exp.args
return message