-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
456 lines (319 loc) · 16.2 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
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
# imports - standard imports
from datetime import datetime
from random import randint
import sqlite3
# imports - third party imports
from flask import Flask, render_template, request, redirect, flash
from flask.helpers import url_for
from flask_sqlalchemy import SQLAlchemy
import requests
from werkzeug.wrappers import response
# setting up Flask instance
app = Flask(__name__,static_url_path="", static_folder="static")
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///Library.db'
db = SQLAlchemy(app)
# Creating Tables
# Table Books for storing Books.
class Books(db.Model):
# Table Books => | book_id | book_name | author | publisher | quantity | borrower | isbn | times_issued |
book_id = db.Column(db.Integer , primary_key = True)
book_name = db.Column(db.String(150))
author = db.Column(db.String(75))
publisher = db.Column(db.String(75))
quantity = db.Column(db.Integer , default = 1)
borrower = db.Column(db.Integer , default = -1)
isbn = db.Column(db.String(15))
times_issued = db.Column(db.Integer , default = 0)
# Table Members for storing the member details.
class Members(db.Model):
# Table members => | member_id | member_name | member_balance | member_borrowed | library_fees_given |
member_id = db.Column(db.Integer , primary_key = True)
member_name = db.Column(db.String(150))
member_balance = db.Column(db.Float , default = 1000)
member_borrowed = db.Column(db.Boolean, default = False)
library_fees_given = db.Column(db.Float , default = 0)
# Table Transactions for storing all the transactions details.
class Transactions(db.Model):
# Table Transactions => | _id | book_name | member_name | direction | time |
_id = db.Column(db.Integer , primary_key = True)
book_name = db.Column(db.String(150))
member_name = db.Column(db.String(150))
direction = db.Column(db.Boolean, default = True)
time = db.Column(db.DateTime , default = datetime.utcnow)
# Home Directory
@app.route('/')
def home():
available_books = Books.query.all()
return render_template('home2.html' , books = available_books)
@app.route('/h2')
def home3():
available_books = Books.query.all()
return render_template('home3.html' , books = available_books)
@app.route('/members', methods = ["POST" , "GET"])
def members():
# Table members => | member_id | member_name | member_balance | member_borrowed | library_fees_given |
# it takes the new member's information and adds in the data base.
if request.method == "POST":
user_name = request.form['user_name'] # getting the user Name
member_balance = request.form['balance'] # getting the amount the user wants to add initially
if not is_alphabets(user_name):
# This means the Librarian has entered wrong data
message = "Please enter correct User-Name"
return render_template('error.html', message = message, page = "members")
if not member_balance.isnumeric():
# This means the Librarian has entered wrong balance
message = "Please enter correct balence"
return render_template('error.html',
message = message,
page = "members"
)
# Every Thing is Correct
try:
# Creating record
member = Members(
member_name=user_name,
member_balance = float(member_balance)
)
db.session.add(member) # adding in the DB
db.session.commit() # commiting changes
except:
return render_template('error.html', message = "Unexpected Error, Cannot add Member")
# no matter if the method is GET or POST we need to render the available member list.
members = Members.query.all() # Getting all the members
return render_template('members2.html', members = members,active_members=True) # rendering the page for members
# this function loads all the transactons
@app.route('/transactions')
def transactions():
# Table Transactions => | _id | book_name | member_name | direction | time |
# Taking all the transactions and arranging them from most recent to least recent.
transactions = Transactions.query.order_by(Transactions.time.desc()).all()
return render_template('transactions2.html', transactions = transactions,active_transactions=True)
# Renders Analytics Page
@app.route('/analytics')
def analytics():
# Table Books => | book_id | book_name | author | publisher | quantity | borrower | isbn | times_issued
# Table members => | member_id | member_name | member_balance | member_borrowed | library_fees_given |
# This query takes the top 5 popular Books which were issued most number of times
popular_books = Books.query.order_by(Books.times_issued.desc()).limit(5).all()
# This query takes the top 5 people who have spent the most
top_spenders = Members.query.order_by(Members.library_fees_given.desc()).limit(5).all()
return render_template('analytics2.html', books = popular_books , people = top_spenders,active_analytics=True)
# Logic for renting out a book
@app.route('/rent_out/<int:book_id>', methods = ["POST" , "GET"])
def rent_out(book_id):
# Table Books => | book_id | book_name | author | publisher | quantity | borrower | isbn | times_issued
# Table members => | member_id| member_name | member_balance | member_borrowed | library_fees_given |
# Table Transactions => | _id | book_name | member_name | direction | time |
# Render only those members who have not borrowed a book
all_members = Members.query.filter(
Members.member_borrowed == False
).all()
if request.method == "POST":
# getting the id of the member.
id_of_the_member = request.form['id']
# if The entered member ID is invalid.
if not id_of_the_member.isnumeric():
return render_template('error.html', message = "Enter valid Id")
# Get the member from the form data.
member = Members.query.get(int(id_of_the_member))
# check if member is present or not:
if member == None:
return render_template('error.html', message = "Not A valid Member!")
if member.member_balance < -500:
# Users Balance is less Than 500
message = f'The balance of {member.member_name} is {member.member_balance} \
which is less than -500 so please add money to your wallet before taking books.'
return render_template('error.html', message = message)
if member.member_borrowed==True:
# User has already borrowed a book.
message = 'The User has already Taken Books'
return render_template('error.html' , message= message)
try:
member.member_borrowed = True # Member has borrowed a book.?????????????????????????????????????
member.member_balance -= 500 # members balance Should Decrease by 500.
member.library_fees_given += 500 # Member paid 500 rs to the Library.
book = Books.query.get(book_id) # Get the book from the book_ID.
if book == None:
return render_template('error.html', message = "Unexpected Error Occured")
book.quantity = 0 # Book's quantity is decreased.
book.times_issued += 1 # books Times issued is increased by 1.
book.borrower = member.member_id # Books borrower is set to the id of member's id.
# New transaction is registered and added to transactions.
trans = Transactions(
book_name = book.book_name,
member_name = member.member_name,
direction = False
)
db.session.add(trans)
db.session.commit()
return redirect(url_for('home'))
except:
return render_template(
'error.html',
message = "Unexpected Error Occured"
)
return render_template(
'rent_out2.html',
id = book_id,
members = all_members,
active_return_book=True
)
# This function Makes the API calls and then redirects to the home page.
@app.route('/addBooks', methods = ["POST" , "GET"])
def addBooks():
# Table Books => | book_id | book_name | author | publisher | quantity | borrower | isbn | times_issued |
if request.method == "POST":
# GET the data from the API.
# pass the data in LMS and then redirect to root page.
# render the data recieved.
# Gets the data from The API.???????????????????????????????????????????????????????????????????
response = make_API_call()
# going through data.
for data in response:
# getting the book ID from the form.
book_id = int(data["bookID"])
# Checking if the same data exists in the data base or not.
to_find = Books.query.get(book_id)
if to_find == None:
# If book is not found in the data base then add it in the data base.
book = Books(
book_id = book_id,
book_name = data["title"],
author = data["authors"],
publisher =data["publisher"],
isbn = data["isbn"]
)
db.session.add(book)
db.session.commit()
# rendering page using jinja and then redirect.
return redirect(url_for('home'))
else:
return render_template('add_books2.html',active_addBooks=True)
@app.route('/addCustomBooks', methods=["POST"])
def add_custom_books():
if request.method == "POST":
book_id = request.form['book_id']
book_name = request.form['book_name']
author = request.form['author']
publisher = request.form['publisher']
isbn = request.form['isbn'] or 404
# checking for if book_id already exists or not and values are valid
if not book_id.isnumeric():
return render_template(
'error.html',
message = "Please enter a valid book ID"
)
book_id = int(book_id)
if Books.query.get(book_id) != None:
return render_template('error.html', message = "Book Id already Exists in DB")
# every thing is valid
try:
book = Books(
book_id = book_id,
book_name = book_name,
author = author,
publisher = publisher,
isbn = isbn
)
db.session.add(book)
db.session.commit()
return redirect(url_for('home'))
except:
return render_template('error.html', message = "Unexpected Error")
else:
return render_template('error.html', message = "NOT AUTHORIZED")
# renders all the books which are currently rented Out.
@app.route('/return_book')
def return_book():
# Table Books => | book_id | book_name | author | publisher | quantity | borrower | isbn | times_issued |
books = Books.query.filter(
Books.quantity == 0 # render only those books which have been issued
).all()
return render_template(
'return_book2.html',
books = books,
active_return_book=True
)
# This is the function which reverts the data to its initial stage
@app.route('/summary/<int:id>')
def summary(id):
# Table Books => | book_id | book_name | author | publisher | quantity | borrower | isbn | times_issued |
# Table members => | member_id | member_name | member_balance | member_borrowed | library_fees_given |
# Table Transactions => | _id | book_name | member_name | direction | time |
book = Books.query.get(id) # get The book.
book.quantity = 1 # set its quantity to 1 again.
member = Members.query.get(book.borrower) # get the member using borrower column.
# get the old transaction to get the time at which the book was issued.
old_trans = Transactions.query.filter(
Transactions.book_name == book.book_name
).first()
# create a new transaction for the return of book
trans = Transactions(
book_name = book.book_name,
member_name = member.member_name,
direction = True
)
db.session.add(trans) # Add it to the session.
book.borrower = -1 # set the borrower to -1 again as it has no borrower now.
# Calculate the balance of member by the formula => 10 * number of days for which the book was borrowed
charges = (datetime.utcnow() - old_trans.time).days * 10
# deduct the amount from the members wallet
member.member_balance -= charges
# add the amount deducted by the user wallet to the library profits
member.library_fees_given += charges
member.member_borrowed = False # set this property to False as the member has not borrowed a book.
db.session.commit() # commit changes.
return render_template('summary2.html', member = member)
@app.route('/delete_member/<int:id>')
def delete(id):
# Table members => | member_id | member_name | member_balance | member_borrowed | library_fees_given |
try:
task_to_delete = Members.query.get(id)
db.session.delete(task_to_delete)
db.session.commit()
return redirect('/members')
except:
return render_template('error.html', message = "Unexpected Error Occured")
@app.route('/update/<int:id>', methods = ["POST", "GET"])
def update(id):
# Table members => | member_id | member_name | member_balance | member_borrowed | library_fees_given |
if request.method == "POST":
try:
user = Members.query.get(id)
user.member_balance += float(request.form['amount'])
db.session.commit()
return redirect(url_for('members'))
except:
return render_template('error.html', message = "Unexpected Error Occured")
else:
return render_template('update2.html', id = id)
@app.errorhandler(404)
def page_not_found(e):
# note that we set the 404 status explicitly
return render_template('error2.html'), 404
# Helper Functions
# checks if a string is alphabetical
def is_alphabets(s : str):
return ''.join(s.split()).isalpha()
# Removes unnecessary spaces between characters
def remove_spaces(s : str):
return ' '.join(s.split())
# This Function Makes The API call and returns The response
def make_API_call():#??????????????????
# This is The Base Url from which we will make the imports
BASE_URL = 'https://frappe.io/api/method/frappe-library?'
title = remove_spaces(request.form['title']) # Removing inconsistency in spaces
authors = remove_spaces(request.form['author']) # Removing inconsistency in spaces
publisher = remove_spaces(request.form['publisher']) # Removing inconsistency in spaces
isbn = request.form['isbn']
end = ''
if title or authors or isbn or publisher:
end = f'title={title}&authors={authors}&isbn={isbn}publisher={publisher}'
response = requests.get(BASE_URL + end).json()['message']
else:
# No parameters passed for searching so we will search at random pages between 1 - 200
response = requests.get(BASE_URL + f'page={randint(1,200)}').json()['message']
return response
if __name__ == '__main__':
app.run(debug=True)
db.create_all()