-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
158 lines (139 loc) · 5.48 KB
/
app.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
import traceback
from random import randint
import security.dumps
from config import dbconfig as dbcfg
import sys
import logging
from util.dataVault import DataVault
from util.queryCollection import QueryCollection
from util.twoFAUtil import TwoFactor
logger = logging.getLogger()
mem_id = None
class App:
def __init__(self):
self.mydb = None
self.mycursor = None
self.member_id = None
def setMemberId(self, id):
global mem_id
mem_id = id
def getMemberId(self):
global mem_id
return mem_id
def populate(self):
try:
# create librarian db if it doesn't exist
mydb, mycursor = QueryCollection.connectDB(QueryCollection)
try:
mycursor.execute("SHOW DATABASES")
dblist = []
for db in mycursor:
dblist.append(db)
logger.info("Currently we have databases: " + str(dblist))
if not ('Librarian',) in dblist:
mycursor.execute("CREATE DATABASE Librarian")
mydb.close()
self.createTables()
except Exception as e:
logger.error("Error in creating cursor: " + str(e) + traceback.format_exc())
sys.exit(-1)
except Exception as e:
logger.error("Error in populate: " + str(e) + traceback.format_exc())
sys.exit(-1)
def createTables(self):
# if table does not exist, create it
creates = dbcfg.createtable
tables = security.dumps.tables
for k, v in creates.items():
try:
mydb, mycursor = QueryCollection.connectDB(QueryCollection)
mycursor.execute("USE Librarian")
mycursor.execute(v)
mydb.close()
except Exception as e:
logger.error("Error in createTables: " + str(e) + traceback.format_exc())
sys.exit(-2)
def validateLogin(self, values):
sql = dbcfg.sql['memberlogin'].replace('{_login}', values[0]).replace('{_input}', '"' + values[1] + '"').replace(
'{_pass}', '"' + values[2] + '"')
try:
mydb, mycursor = QueryCollection.connectDB(QueryCollection)
mycursor.execute(sql)
rows = mycursor.fetchall()
logger.info("Validation SQL: " + sql)
logger.info("Found " + str(mycursor.rowcount) + " rows")
TwoFactor.Phone = rows[0][6]
mydb.close()
if len(rows)>0:
return rows[0][-1], rows[0][0], True
else:
return None, None, False
except Exception as e:
logger.error("Validate Error" + str(e) + traceback.format_exc())
return None, None, False
class Librarian:
def __init__(self):
self.lib_id = None
def createStaffAccount(self, data):
# random 6 digit int as ID
self.lib_id = ''.join(["{}".format(randint(0, 9)) for num in range(0, 5)])
DataVault.createdStaffId = self.lib_id
DataVault.loggedinID = self.lib_id
DataVault.twoFAid = self.lib_id
DataVault.twoFAtype = "Staff"
data[0] = self.lib_id
sql = dbcfg.sql['insertStaff']
try:
mydb, mycursor = QueryCollection.connectDB(QueryCollection)
mycursor.execute(sql, data)
mydb.commit()
logger.info("Inserted into Staff number of rows: " + str(mycursor.rowcount))
mydb.close()
except Exception as e:
logger.error("Error in insertStaff : " + str(e) + traceback.format_exc())
sys.exit(-1)
def createMemberAccount(self, data):
# random 6 digit int as ID
DataVault.mem_id = ''.join(["{}".format(randint(0, 9)) for num in range(0, 5)])
DataVault.twoFAid = DataVault.mem_id
DataVault.twoFAtype = "Member"
data[0] = DataVault.mem_id
TwoFactor.id = data[0]
data.append(0)
sql = dbcfg.sql['insertMember']
try:
mydb, mycursor = QueryCollection.connectDB(QueryCollection)
mycursor.execute(sql, data)
mydb.commit()
logger.info("Inserted into Staff number of rows: " + str(mycursor.rowcount))
mydb.close()
except Exception as e:
logger.error("Error in insertStaff : " + str(e) + traceback.format_exc())
sys.exit(-1)
def validateLogin(self, values):
return QueryCollection.validateStaff(self, values)
# def addDocument(self, doctype):
#
# def editDocument(self, doctype):
#
# def deleteDocument(self, doctype):
#
# def viewDocument(self, doctype, task):
# # Check overdue issues
#
# def manageUsers(self, doctype):
# # TO-DO: Add or remove members
#
def viewMembers(self, memid=None):
try:
mydb, mycursor = QueryCollection.connectDB(QueryCollection)
if memid != None:
where = " WHERE Member_Id=" + str(memid) + ';'
mycursor.execute(dbcfg.sql['viewMembers'] + where)
mycursor.execute(dbcfg.sql['viewMembers'])
rows = mycursor.fetchall()
rows.insert(0, ('Member Id', 'First Name', 'Last Name', 'DOB', 'Books Borrowed', '# Borrows', 'Phone', 'Email'))
return rows
except Exception as e:
logger.error("Error in QueryCollection viewMembers: " + str(e) + traceback.format_exc())
sys.exit(-1)