Repository navigation
Expand file tree
/
Copy pathbook.py
More file actions
201 lines (131 loc) · 8.02 KB
/
Copy pathbook.py
File metadata and controls
201 lines (131 loc) · 8.02 KB
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
import pyrebase
import sys
import datetime
import time
import smtplib
import hashlib
class Book:
def __init__(self):
self.username = 'sender-emailid'
self.password = 'sender-email-password'
self.smtpserver = 'smtp.gmail.com:587'
config = {
"apiKey": "AIzaSyBxhmLuQ30yH1gVnTTPC7LYuQrpzfxEK4E",
"authDomain": "librany-dde7b.firebaseapp.com",
"databaseURL": "https://librany-dde7b.firebaseio.com/",
"storageBucket": "librany-dde7b.appspot.com",
"serviceAccount": "librany-dde7b-firebase-adminsdk-fct8b-9a20d0a7ee.json"
}
#Create a pyrebase object with the required configurations
firebase = pyrebase.initialize_app(config)
#Create database object which will be referenced for all the required transactions to be performed.
self.db = firebase.database()
#Create an authorization object
auth = firebase.auth()
try:
self.user = auth.sign_in_with_email_and_password("dhavalbagal99@gmail.com", "Dhaval123")
except :
print("Invalid credentials or no Internet connectivity!")
sys.exit(1)
def notify_issue(self, to, name, title, bookid):
header= 'From: '+self.username+'\n'
header+='To: %s\n' % to
header+='Subject: Book issued successfully\n\n'
bookid = hashlib.md5(bookid.encode()).hexdigest()
msg = "Dear "+name+"\n\nThe book '"+title+"' with ID-"+bookid+" has been successfully issued!.\nThank you for using Librany.\n\nThanks and Regards,\nLibrany"
msg = header+ msg
server = smtplib.SMTP(self.smtpserver)
server.starttls()
server.login(self.username,self.password)
problems = server.sendmail(self.username, to, msg)
server.quit()
def notify_return(self, to, name, title, bookid):
header= 'From: '+self.username+'\n'
header+='To: %s\n' % to
header+='Subject: Book returned successfully\n\n'
bookid = hashlib.md5(bookid.encode()).hexdigest()
msg = "Dear "+name+"\n\nThe book '"+title+"' with ID-"+bookid+" has been successfully returned!.\nThank you for using Librany.\n\nThanks and Regards,\nLibrany"
msg = header+ msg
server = smtplib.SMTP(self.smtpserver)
server.starttls()
server.login(self.username, self.password)
problems = server.sendmail(self.username, to, msg)
server.quit()
#Input is userid and bookid. The task is to update book and user details
def update_details(self,userid, bookid):
#Get details of the book associated with bookid
book = self.db.child("BOOKS").child(bookid).get(self.user['idToken']).val()
#Since the book is issued, we need to change its availability to Unavailable and set Issued_By to the userid who has issued the book.
self.db.child("BOOKS").child(bookid).update({'Availability':'Unavailable', 'Issued_By':userid, 'Location':' '},self.user['idToken'])
#Get details of the user associated with userid
usr = self.db.child("USERS").child(userid).get(self.user['idToken']).val()
#Get the number of books issued currently by the user.
count = usr["No_of_Books_Issued"]
#Increment the count by 1, since the No_of_Books_Issued should now be incremented
temp = {'Book'+str(count+1) : bookid}
self.db.child("USERS").child(userid).update({'No_of_Books_Issued':(count+1)}, self.user['idToken'])
#Update the bookid of the issued book in that user's node
self.db.child("USERS").child(userid).child("Books_Issued").update(temp, self.user['idToken'])
def issue(self, userid, bookid):
book = self.db.child("BOOKS").child(bookid).get(self.user['idToken']).val()
#If the bookid that is recognised by the cam is present in the database, then do the following
if book is not None:
avail = book["Availability"]
#If book is available, then do the following, else return 'IssueFailed'
if avail == 'Available':
usr = self.db.child("USERS").child(userid).get(self.user['idToken']).val()
#If the identified userid exists in the database, then do the following,
if usr is not None:
allowance = usr["Issue_Allowance"]
issued = usr["No_of_Books_Issued"]
#If user has already issued say 2 books, then he cannot issue any book further
#Since those 2 books need to be returned first.
if issued < allowance:
#Add the transaction details to the TRANSACTION table
Issue_ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d@%H:%M:%S')
key=bookid+"@"+userid+"@"+Issue_ts
transaction ={'UserId':userid, 'BookId':bookid, 'Issue_ts':Issue_ts, 'Return_ts':' '}
self.db.child("TRANSACTIONS").child(key).set(transaction, self.user['idToken'])
#Update book and user details after the TRANSACTION table is updated
self.update_details(userid, bookid)
to= usr["Email"]
name =usr['Name'].split()[0]
title = book["Title"]
self.notify_issue(to, name, title, bookid)
return "IssueDone"
else :
return "AllowanceExceeded"
return "IssueFailed"
def returnbook(self, bookid):
book = self.db.child("BOOKS").child(bookid).get(self.user['idToken']).val()
#If bookid recognised is present in the database and is currently issued by someone (i.e its availability is 'Unavailable')
#This is because in subsequent frames acquired by the cam the same qrcode will be detected and hence to avoid DB transactions each time for each frame, we check the availability.
if book is not None and book["Availability"]=="Unavailable":
uid = book["Issued_By"]
#Update the availability status and issued by field
self.db.child("BOOKS").child(bookid).update({'Availability':'Available', 'Issued_By':' '}, self.user['idToken'])
usr = self.db.child("USERS").child(uid).get(self.user['idToken']).val()
#Decrement the count which maintains the number of books currently the user has.
n = usr["No_of_Books_Issued"]-1
self.db.child("USERS").child(uid).update({'No_of_Books_Issued':n},self.user['idToken'])
#Remove the bookid which is returned from the Books_Issued field
for k, val in usr["Books_Issued"].items():
if val==bookid:
self.db.child("USERS").child(uid).child("Books_Issued").child(k).remove(self.user['idToken'])
transactions = self.db.child("TRANSACTIONS").get(self.user['idToken']).val()
#Update the return timestamp for the current transaction associated with that book in the TRANSACTION table.
for k1, k2 in transactions.items():
temp = bookid+"@"+uid
if temp in k1 and k2["Return_ts"]==" ":
Return_ts = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d@%H:%M:%S')
self.db.child('TRANSACTIONS').child(k1).update({'Return_ts':Return_ts}, self.user['idToken'])
to= usr["Email"]
name =usr['Name'].split()[0]
title = book["Title"]
self.notify_return(to, name, title, bookid)
def updatelocation(self, bookid, loc):
#Get details of the book associated with bookid
book = self.db.child("BOOKS").child(bookid).get(self.user['idToken']).val()
if book['Availability']=='Available':
#Update location of the bookid
self.db.child("BOOKS").child(bookid).update({'Location':loc},self.user['idToken'])