-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib_manager.py
330 lines (261 loc) · 8.33 KB
/
lib_manager.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
from peewee import *
import uuid
# database - SQLite3
DB = SqliteDatabase('library.db')
DB.connect()
class Member(Model):
member_id = UUIDField(primary_key=True,
unique=True,
default=uuid.uuid4)
name = CharField(max_length=60)
address = CharField(max_length=255)
email = CharField(max_length=255,
unique=True)
def __str__(self):
return f'Name: {self.name}, \n' \
f'Email: {self.email}, \n' \
f'Address: {self.address} \n'
class Meta:
database = DB
table_name = 'members'
class Book(Model):
title = CharField(max_length=30)
author = CharField(max_length=30)
publisher = CharField(max_length=100)
ISBN = CharField(max_length=13,
primary_key=True,
unique=True)
checked_out = BooleanField(default=False)
def __str__(self):
return f'Title: {self.title}\n' \
f'Author: {self.author}\n'
class Meta:
database = DB
table_name = 'books'
class CheckOutHistory(Model):
member_id = UUIDField()
book_id = ForeignKeyField(Book)
def __iter__(self):
cursor = self.select().execute()
for row in cursor:
yield row
class Meta:
database = DB
table_name = 'check_out_history'
class CheckedOutBook(Model):
member_id = UUIDField()
book_id = ForeignKeyField(Book)
title = CharField()
def __iter__(self):
cursor = self.select().execute()
for row in cursor:
yield row
class Meta:
database = DB
table_name = 'checked_out_books'
class CheckOut:
@classmethod
def check_out(cls,
email: str,
title: str,
):
member = cls._take_member_info(
email=email
)
book = cls._choose_book_by_title(
title=title
)
if not cls._has_current_book(
member_id=member.member_id,
book_title=book.title
):
checked_out_book = CheckedOutBook.create(
member_id=member.member_id,
book_id=book.ISBN,
title=book.title
)
checked_out_book.save()
Book.update(checked_out=True).where(
Book.ISBN == book.ISBN
).execute()
CheckOutHistory.create(
member_id=member.member_id,
book_id=book.ISBN
).save()
else:
raise Exception("You already have this book")
@classmethod
def return_book(cls,
title: str,
email: str):
member = cls._take_member_info(
email=email
)
book = cls._return_book_by_title(
title=title
)
if cls._has_current_book(
member_id=member.member_id,
book_title=title
):
checked_out_book = CheckedOutBook.get(
member_id=member.member_id,
title=title
)
checked_out_book.delete_instance()
Book.update(checked_out=False).where(
Book.ISBN == book.ISBN
).execute()
else:
raise Exception("You do not have this book")
@staticmethod
def _return_book_by_title(title: str):
try:
Book.get(Book.title == title)
except:
raise Exception("Type correct title")
try:
book = Book.select().where(
Book.title == title,
Book.checked_out == True
).get()
return book
except Exception as e:
raise e
@staticmethod
def _choose_book_by_title(title: str):
try:
Book.get(Book.title == title)
except:
raise Exception("Type correct title")
try:
book = Book.select().where(
Book.title == title,
Book.checked_out == False
).get()
return book
except:
raise Exception("There is not available books for the moment")
@staticmethod
def _has_current_book(member_id,
book_title
):
if CheckedOutBook.select().count() == 0:
return False
try:
members = CheckedOutBook.select().where(
CheckedOutBook.title == book_title
).get()
for member in members:
if member.member_id == member_id and member.title == book_title:
member = CheckedOutBook.select().where(
CheckedOutBook.member_id == member_id
).get()
for m in member:
if m.title == book_title:
return True
return False
except:
return False
@staticmethod
def _choose_book_by_author(author: str):
return Book.select().where(
Book.author == author
).get()
@staticmethod
def _take_member_info(email: str):
try:
return Member.get(
Member.email == email
)
except:
raise Exception("Type correct email")
class Library:
@staticmethod
def add_book(title: str,
author: str,
publisher: str,
ISBN: int):
assert len(str(ISBN)) == 13, 'Enter 13 numbers'
new_book = Book.create(
title=title,
author=author,
publisher=publisher,
ISBN=ISBN,
borrowers=0
)
new_book.save()
return new_book
@staticmethod
def add_member(name: str,
email: str,
address: str):
new_member = Member.create(
name=name,
email=email,
address=address
).save()
return new_member
@staticmethod
def delete_member(email: str):
member = Member.get(Member.email == email)
member.delete_instance()
@staticmethod
def check_out(member_email: str,
book_title: str):
CheckOut.check_out(
email=member_email,
title=book_title
)
@staticmethod
def return_book(member_email: str,
book_title: str):
CheckOut.return_book(
title=book_title,
email=member_email
)
@staticmethod
def list_members():
return Member.select()
@staticmethod
def list_books():
return Book.select()
@staticmethod
def search_book_by_title(title: str):
return Book.get(Book.title == title)
@staticmethod
def search_books_by_author(author: str):
return Book.select().where(Book.author == author)
@classmethod
def get_popular_books(cls):
"""Returns 10 last checked out books"""
if CheckOutHistory.select().count() == 0:
raise Exception("Nobody have not checked out book yet")
if CheckOutHistory.select().count() < 10:
books = CheckOutHistory.select().order_by(
CheckOutHistory.id.desc()
)
return [Book.get(Book.ISBN == book.book_id) for book in books]
if CheckOutHistory.select().count() >= 10:
books = CheckOutHistory.select().order_by(
CheckOutHistory.id.desc()
).limit(10)
return [Book.get(Book.ISBN == book.book_id) for book in books]
@staticmethod
def _search_book_by_isbn(ISBN: int):
return Book.get(Book.ISBN == ISBN)
@classmethod
def get_member_checked_out_books(cls,
email: str):
try:
member = Member.get(Member.email == email)
if CheckedOutBook.select().where(
CheckedOutBook.member_id == member.member_id
).count() == 0:
raise Exception("You have not checked out any book yet")
books = CheckedOutBook.select().where(
CheckedOutBook.member_id == member.member_id
)
return [cls._search_book_by_isbn(book.book_id) for book in books]
except Exception as e:
raise f"{e} - type correct email"