-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrary.py
More file actions
129 lines (110 loc) · 4.55 KB
/
Library.py
File metadata and controls
129 lines (110 loc) · 4.55 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
import os
import json
import requests
url = "https://wolnelektury.pl/api/books/?format=json"
response = requests.get(f"{url}")
data = response.json()
class Library:
def __init__(self):
self.database = {"books": [], "users": [{"login": "admin", "password": "admin"}], "registry": []}
self.load_books()
self.setup()
def load_books(self):
# API can return ~6000 books, for this project i take only 60 of them to test it.
for book in data[:60]:
self.database["books"].append({"title": book['title'], "author": book['author'], "genre": book['genre'], "borrow": "No"})
def setup(self):
cwd = os.getcwd()
if os.path.exists(f"{cwd}\\database.json"):
print("Library successful loaded! ")
with open("database.json", "r") as f:
self.database = json.load(f)
print(self.database)
else:
with open("database.json", "a") as f:
f.write(json.dumps(self.database))
print(self.database)
print(type(self.database))
print(self.database["users"])
def open_json_file(self):
with open("database.json", "w") as f:
f.write(json.dumps(self.database))
def available_books_to_borrow(self):
books = []
print("Books available to borrow:")
for i in self.database["books"]:
if i["borrow"] == "No":
print(i["title"])
books.append(i["title"])
else:
pass
return books
def borrow(self, user,books_to_borrow):
for book in books_to_borrow:
for i in self.database["books"]:
if i["title"] == book:
i["borrow"] = "Yes"
self.open_json_file()
self.database['registry'].append({'user': user, 'book': book})
self.open_json_file()
print(f"{user} just borrow book: {book}")
def available_books_to_deposit(self, user):
books = []
print("Books available to deposit:")
for i in self.database["registry"]:
if i["user"] == user:
print(i["book"])
books.append(i["book"])
else:
pass
return books
def deposit(self, user, book):
for i in self.database["registry"]:
if i["book"] == book:
self.database["registry"].remove(i)
for b in self.database["books"]:
if b["title"] == book:
b["borrow"] = "No"
self.open_json_file()
print(f"{user} just deposit book: {book}")
def user_log_in(self, login, password):
if login != "" and password != "":
for user in self.database["users"]:
if login == user["login"] and password == user["password"]:
if login == "admin" and password == "admin":
return "admin"
return True
return False
def password_available(self, user_password):
for user in self.database["users"]:
if user["password"] == user_password:
return False
return True
def login_available(self, user_name):
for user in self.database["users"]:
if user["login"] == user_name:
return False
return True
def change_password(self,user_name, old_user_password,new_password):
for user in self.database["users"]:
if user["login"] == user_name:
if user["password"] == old_user_password:
user["password"] = new_password
self.open_json_file()
return True
def register_user(self,user_name,user_password):
if self.login_available(user_name) and self.password_available(user_password):
self.database["users"].append({"login": user_name, "password": user_password})
self.open_json_file()
print(self.database)
return True
def can_be_add(self, book):
for i in self.database["books"]:
if i["title"] == book:
return False
return True
def add_new_book(self,book_title,book_author,book_genre):
self.database['books'].append({"title": book_title, "author": book_author, "genre": book_genre, "borrow": "No"})
print(self.database)
self.open_json_file()
print(f"Title:'{book_title}', Author:'{book_author}', genre:'{book_genre}' - successful added to Library")