def borrows(self, data_list): successful_borrows = [] user = User.get_by_id(data_list[0]["user"]["user_id"]) with database.atomic(): num_book_borrowing = BookCirculation.select().where( (BookCirculation.user == user) & (BookCirculation.return_time.is_null(True))).count() if num_book_borrowing + len(data_list) > 5: raise BorrowingExceededError() for data in data_list: book = Book.get_by_id(data["book"]["book_id"]) data['book'] = book data['user'] = user if BookCirculation.select().where( (BookCirculation.book == book) & (BookCirculation.return_time.is_null(True))).count() != 0: raise AlreadyBorrowedError() successful_borrows.append(BookCirculation.create(**data)) SendBorrowNotification(successful_borrows).start() return successful_borrows
def search_history(self, keyword: str): user_query = self.__search_user(keyword) book_query = self.__search_book(keyword) return DatabaseManager.get_list(BookCirculation.select().where( (BookCirculation.user << user_query) | (BookCirculation.book << book_query)))
def search_borrowing(self, keyword: str): user_query = self.__search_user(keyword) book_query = self.__search_book(keyword) return DatabaseManager.get_list(BookCirculation.select().where( ((BookCirculation.user << user_query) | (BookCirculation.book << book_query)) & BookCirculation.return_time.is_null(True)))
def return_book(self, borrow_id: int): BookCirculation.set_by_id(borrow_id, {"return_time": datetime.now()})
def get_specific_record(self, borrow_id): return BookCirculation.get_by_id(borrow_id)
def get_all_being_borrowed(self): return DatabaseManager.get_list(BookCirculation.select().where( BookCirculation.return_time.is_null(True)))
def get_complete_history(self): return DatabaseManager.get_list(BookCirculation.select())
def __init__(self): DatabaseManager.__init__(self) BookCirculation.create_table()