-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
executable file
·481 lines (428 loc) · 11.8 KB
/
model.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
import sqlite3 as sql
from flask import session
from passlib.hash import sha256_crypt
import random
import datetime
def insert_admin(username,phone,password,email):
with sql.connect("database.db") as con:
password = sha256_crypt.encrypt(password)
cur = con.cursor()
cur.execute("INSERT INTO admins (username,phone,password,email) VALUES (?,?,?,?)", (username,phone,password,email) )
con.commit()
def insert_books(name,author,category):
with sql.connect("database.db") as con:
cur = con.cursor()
cur.execute("INSERT INTO books (name,author,category) VALUES (?,?,?)", (name,author,category) )
con.commit()
def getAll():
try:
with sql.connect("database.db") as con:
con.row_factory = sql.Row
cur = con.cursor()
cur.execute("select * from books")
rows = cur.fetchall()
for row in rows:
print "row=" + row["name"]
return (rows)
except:
print "NO ENTRY FOUND"
return ([])
def authenticate(request):
con = sql.connect("database.db")
# username = "Sarthak" set for random admin
username = request.form['username']
password = request.form['password']
sqlQuery = "select password from admins where username = '%s'"%username
cursor = con.cursor()
cursor.execute(sqlQuery)
row = cursor.fetchall()
# status = True set for random admin
status = False
if row:
for i in range(0,len(row)):
status = sha256_crypt.verify(password,row[i][0])
if status == True:
break
if status:
msg = username + "has logged in successfully"
session['username'] = username
else:
# session["username"]="username" set for random admin
msg = username + "login failed"
return status
def authenticatest(request):
con = sql.connect("database.db")
username = request.form['username']
password = request.form['password']
sqlQuery = "select password from students where name = '%s'"%username
print sqlQuery
cur = con.cursor()
cur.execute(sqlQuery)
row = cur.fetchall()
print len(row)
status = False
if row:
for i in range(0,len(row)):
status = sha256_crypt.verify(password,row[i][0])
print status
print i
if status == True:
print "hello"
break
print status
if status:
msg = username + "had logged in successfully"
session['username'] = username
else:
msg = username + "login failed"
return status
def querybook(params):
clause = []
if params.get("name") is not None and params.get("name") is not '':
clause.append("name LIKE '%{}%'".format(params.get("name")))
if params.get("author") is not None and params.get("author") is not '':
clause.append("author LIKE '%{}%'".format(params.get("author")))
if params.get("category") is not None and params.get("category") is not '':
clause.append("category LIKE '%{}%'".format(params.get("category")))
for items in clause:
print items
if clause:
query = "SELECT * FROM books WHERE {}".format(" AND ".join(clause))
else:
query = "SELECT * FROM books"
try:
con = sql.connect("database.db")
cursor = con.cursor()
cursor.execute(query, params)
book_list = cursor.fetchall()
print len(book_list)
return book_list
except Exception as e:
print e
return None
def unique(carnum):
try:
con = sql.connect("database.db")
cursor = con.cursor()
cursor.execute("SELECT count(*) from students where cardnum=?",(carnum, ))
num = cursor.fetchone()
#for items in num:
print num[0]
if num[0] == 0:
return True
else:
return False
except Exception as e:
print e
return None
def insertStudent(form_apply):
try:
con =sql.connect("database.db")
cursor = con.cursor()
print form_apply["password"]
form_apply["password"]=sha256_crypt.encrypt(form_apply["password"])
cursor.execute("INSERT INTO students(cardnum,name,dob,doj,address,phone,borrowlimit,password,email) VALUES (:cardnum,:name,:dob,:doj,:address,:phone,:borrowlimit,:password,:email)",form_apply)
con.commit()
return True
except Exception as e:
print e
return False
def duplicate(form_apply):
try :
con = sql.connect("database.db")
cursor = con.cursor()
clause=[]
clause.append("name='{}'".format(form_apply.get("name")))
clause.append("dob='{}'".format(form_apply.get("dob")))
clause.append("address='{}'".format(form_apply.get("address")))
clause.append("phone='{}'".format(form_apply.get("phone")))
clause.append("borrowlimit='{}'".format(form_apply.get("borrowlimit")))
query="SELECT * FROM students WHERE {}".format(" AND ".join(clause))
print query
cursor.execute(query,form_apply)
list_s = cursor.fetchall()
for items in list_s:
return True
print items
return False
except Exception as e:
print e
return False
def existence_book(bookid):
try:
con= sql.connect("database.db")
cursor = con.cursor()
cursor.execute("SELECT COUNT(*) FROM books WHERE id=?",(bookid,))
flag = cursor.fetchone()
return flag[0] == 1
except Exception as e:
print(e)
return None
def existence_student(studentid):
try:
con = sql.connect("database.db")
cursor = con.cursor()
cursor.execute("SELECT COUNT(*) FROM students WHERE id=?",(studentid,))
flag = cursor.fetchone()
return flag[0] == 1
except Exception as e:
print(e)
return None
def getbookstatus(bookid):
try:
con = sql.connect("database.db")
cursor = con.cursor()
cursor.execute("SELECT COUNT(*) FROM issue WHERE bookid=?",(bookid, ))
flag = cursor.fetchone()
return flag[0] == 0
except Exception as e:
print e
return None
def existence_borrowlimit(studentid):
try:
con = sql.connect("database.db")
cursor = con.cursor()
cursor.execute("SELECT COUNT(*) FROM issue WHERE studentid=?",(studentid, ))
borrows = cursor.fetchone()[0]
cursor.execute("SELECT borrowlimit FROM students WHERE id=?",(studentid, ))
borrowlimit = cursor.fetchone()[0]
return borrows < borrowlimit
except Exception as e:
print(e)
return None
def insertIssue(issue):
try:
con = sql.connect("database.db")
cursor = con.cursor()
cursor.execute("INSERT INTO issue(bookID,studentid,adminid,issueDate)VALUES(:bookID,:studentid,:adminid,:issueDate)",issue)
con.commit()
return True
except Exception as e:
print e
return False
def queryissues():
try:
con = sql.connect("database.db")
cursor = con.cursor()
cursor.execute("SELECT * FROM issue")
row = cursor.fetchall()
return row
except Exception as e:
print e
def queryemail(studentid):
try:
con = sql.connect("database.db")
cursor = con.cursor()
cursor.execute("SELECT email FROM students where id="+str(studentid))
row = cursor.fetchall()
return row
except Exception as e:
print e
def querystudent(params):
clause = []
count =0
if params.get("id") is not None and params.get("id") is not '':
clause.append("id='{}'".format(params.get("id")))
count = count+1
if clause[0] == "id=''":
clause.pop(0)
count=count-1
if params.get("name") is not None and params.get("name") is not '':
clause.append("name LIKE '%{}%'".format(params.get("name")))
count = count+1
if params.get("cardnum") is not None and params.get("name") is not '':
clause.append("cardnum='{}'".format(params.get("cardnum")))
count = count+1
if clause[count-1]=="cardnum=''":
clause.pop(count-1)
count = count-1
if clause:
query = "SELECT * FROM students WHERE {}".format(" AND ".join(clause))
else:
query = "SELECT * FROM students"
print query
try:
con = sql.connect("database.db")
cursor = con.cursor()
cursor.execute(query,params)
student_list = cursor.fetchall()
#for items in student_list:
print student_list
xxx=len(student_list)
return [student_list,xxx]
except Exception as e:
print e
return None
def queryissuebybook(bookid,student=False):
try:
if student:
con = sql.connect("database.db")
cursor = con.cursor()
cursor.execute('''
SELECT transid,bookid,studentid,adminid,issueDate,name
FROM issue,students
WHERE studentid = student.id AND bookid =?''',(bookid, ))
else:
con = sql.connect("database.db")
cursor = con.cursor()
cursor.execute("SELECT * FROM issue WHERE bookid=?",(bookid, ))
issue = cursor.fetchone()
return issue
except Exception as e:
print e
return None
def insertissuehistory(issuehistory):
try:
for keys in issuehistory:
print issuehistory[keys]
con = sql.connect("database.db")
cursor = con.cursor()
cursor.execute('''
INSERT INTO issue_history(transid,bookid,studentid,issuedbyid,issueDate,returnDate,lateFees)
VALUES(:transid,:bookid,:studentid,:issuedbyid,:issueDate,:returnDate,:lateFees)
''',issuehistory)
con.commit()
return True
except Exception as e:
print "hello"
print e
return None
def deleteissue(issueid):
try:
con = sql.connect("database.db")
cursor=con.cursor()
cursor.execute("DELETE FROM issue WHERE transid=?",(issueid, ))
con.commit()
return True
except Exception as e:
print e
return None
def insertstudent_reg(student):
try:
con = sql.connect("database.db")
cursor = con.cursor()
cursor.execute("INSERT INTO student_reg(name, dob, address, phone,password,email) VALUES(:name, :dob, :address, :phone,:password,:email)",student)
con.commit()
return True
except Exception as e:
print e
return False
def querystudentreg():
try:
con = sql.connect("database.db")
cursor = con.cursor()
cursor.execute("SELECT * FROM student_reg")
regs = cursor.fetchall()
for items in regs:
print items
return regs
except Exception as e:
print e
return None
def querysinglestudentreg(regID):
try:
con = sql.connect("database.db")
cursor = con.cursor()
cursor.execute("SELECT * FROM student_reg WHERE regID=:regID",(regID, ))
reg = cursor.fetchone()
return reg
except Exception as e:
print e
return None
def deletestudentreg(regID):
try:
con = sql.connect("database.db")
cursor = con.cursor()
cursor.execute("DELETE FROM student_reg WHERE regID=:regID",(regID, ))
con.commit()
return True
except Exception as e:
print e
return False
def generate():
carNum = random.randint(1000,9999)
if unique(carNum):
return carNum
else:
generate()
#return carNum
def calcLateFees(issueDate, returnDate):
lateFees = 0
date1 = datetime.datetime.strptime(issueDate, "%d-%m-%Y")
date2 = datetime.datetime.strptime(returnDate, "%d-%m-%Y")
print date1
days_issued = (date2-date1).days
print days_issued
if days_issued > 7:
lateFees = (days_issued-7)*LATE_FINE
return lateFees
def getinfo(name):
try:
con = sql.connect("database.db")
cursor = con.cursor()
cursor.execute("SELECT * FROM students WHERE name=:name",(name, ))
info = cursor.fetchone()
return info
except Exception as e:
print e
return None
def findbooks(name):
try:
con = sql.connect("database.db")
cursor = con.cursor()
cursor.execute("SELECT id FROM students WHERE name=:name",(name,))
x = cursor.fetchone()
y = x[0]
cursor.execute("SELECT * FROM issue WHERE studentid=?",(y,))
book = cursor.fetchall()
print book
print book[0]
return book
except Exception as e:
print e
return None
def viewadmins1():
try:
con = sql.connect("database.db")
cursor = con.cursor()
cursor.execute("SELECT * FROM admins")
x = cursor.fetchall()
print x
print len(x)
y=len(x)
return [x,y]
except Exception as e:
print e
return None
def queryst(data):
try:
con = sql.connect("database.db")
row = []
cursor = con.cursor()
for i in range(0,len(data)):
cursor.execute("SELECT * FROM students where id ="+str(data[i][2])+";")
row.append(cursor.fetchone())
return row
except Exception as e:
print e
def querybk(data):
try:
con = sql.connect("database.db")
row = []
cursor = con.cursor()
for i in range(0,len(data)):
cursor.execute("SELECT * FROM books where id ="+str(data[i][1])+";")
row.append(cursor.fetchone())
return row
except Exception as e:
print e
def showissued():
try:
con=sql.connect("database.db")
row = []
cursor = con.cursor()
cursor.execute("SELECT * from issue")
row = cursor.fetchall()
return row
except Exception as e:
print e