-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_users.py
110 lines (89 loc) · 4.57 KB
/
create_users.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
from main import Member, Session, engine, Book, Borrow, Reservation, Isbn, Fine, Author
from datetime import datetime, date, timedelta
local_session = Session(bind = engine)
# to add single member
def membership_creation(member, name ,email,faculty,hp):
new_member = Member(membershipid = member, name = name, email = email, faculty = faculty, phonenumber = hp)
local_session.add(new_member)
local_session.commit()
def book_acquisition(acc_no, isbn, authors, publication_year, publisher, title):
authorslist = authors.split(",")
new_book = Book(accessionNo = acc_no, isbn = isbn)
new_isbn = Isbn(isbn = isbn, publisherYear = publication_year, publisher = publisher, title = title)
if not local_session.query(Book).filter(Book.accessionNo == acc_no).first():
local_session.add(new_book)
if not local_session.query(Isbn).filter(Isbn.isbn == isbn).first():
local_session.add(new_isbn)
for author in authorslist:
new_author = Author(isbn = isbn, authorname = author)
local_session.add(new_author)
local_session.commit()
def book_reservation(acc_no, member, reservedate):
new_reservation = Reservation(accessionNo = acc_no, membershipid = member, reservedate = reservedate)
if not local_session.query(Reservation).filter(Reservation.accessionNo == acc_no).first() and not local_session.query(Fine).filter(Fine.membershipid == member).first() and \
local_session.query(Reservation).filter(Reservation.membershipid == member).count() <= 1:
local_session.add(new_reservation)
local_session.commit()
def borrow_book(acc_num, member):
new_borrow = Borrow(accessionNo = acc_num, membershipid = member)
if not local_session.query(Borrow).filter(Borrow.accessionNo == acc_num).first() and not local_session.query(Reservation).filter(Reservation.accessionNo == acc_num).first() \
and not local_session.query(Fine).filter(Fine.membershipid == member).first() and local_session.query(Borrow).filter(Borrow.membershipid == member).count() <= 1:
local_session.add(new_borrow)
local_session.commit()
def return_book(acc_num, memid, returnDate):
old_book = local_session.query(Borrow).filter(Borrow.accessionNo == acc_num).first()
returndate = datetime.strptime(returnDate, "%Y-%m-%d").date()
if returndate > old_book.returndate:
if local_session.query(Fine).filter(Fine.membershipid == memid).first():
new_fine = returndate - old_book.returndate
mem = local_session.query(Fine).filter(Fine.membershipid == memid).first()
mem.paymentAmount += new_fine.days
elif not local_session.query(Fine).filter(Fine.membershipid == memid).first():
new_fine = Fine(membershipid = memid, paymentAmount = (returndate - old_book.returndate).days)
local_session.add(new_fine)
local_session.delete(old_book)
local_session.commit()
def reservation_cancellation(acc_num, memid, canceldate):
if local_session.query(Reservation).filter(Reservation.membershipid == memid, Reservation.accessionNo == acc_num).first() \
and local_session.query(Borrow).filter(Borrow.accessionNo == acc_num):
to_delete = local_session.query(Reservation).filter(Reservation.membershipid == memid, Reservation.accessionNo == acc_num).first()
local_session.delete(to_delete)
local_session.commit()
def fine_payment(memid, paymentdate, amount):
mem = local_session.query(Fine).filter(Fine.membershipid == memid).first()
if mem and mem.paymentAmount == amount:
mem.paymentDate = paymentdate
local_session.commit()
#membership_creation("A100", "Ryu", "hrhr", "science", 81234567)
#borrow_book("A03", "A100")
#borrow_book("A01", "A100")
#return_book("A03", "A100", "2022-03-29")
#book_acquisition("122", 123, "asd,aasd,dksk", 123, "askd", "asddas")
#book_reservation("A04", "A10", date.today())
#reservation_cancellation("A04", "A10")
#fine_payment("A111", date.today(), 12)
# to add list of users
""" users = [
{
"username" : "jerry",
"email" : "[email protected]"
},
{
"username" : "jordan",
"email" : "[email protected]"
},{
"username" : "james",
"email" : "[email protected]"
},{
"username" : "jane",
"email" : "[email protected]"
},{
"username" : "john",
"email" : "[email protected]"
},
] """
""" for u in users:
new_user = User(username = u["username"], email = u["email"])
local_session.add(new_user)
local_session.commit()
print(f"Added {new_user}") """