def BooksForUser(user_id): #応用機能 AutoUpdateLendInfo() session = Session() booklist = session.query(Book).all() session.commit() borrowset = Borrowing(user_id) store = [] if booklist != []: for book in booklist: if not IsOwn(user_id, book.id): status = "still" deadline = None if book.id in borrowset: status = "borrowing" deadline = GetDeadLine(user_id, book.id) store.append({ "id": book.id, "name": book.name, "price": book.price, "image": book.image, "info": book.info, "auther": book.auther, "url": book.url.split(','), "status": status, "deadline": deadline }) return store
def IsOwnBookAndId(own_book_id, user_id): session = Session() own_book = session.query(Own_Book).filter(Own_Book.id == own_book_id) session.commit() if (own_book.count() == 0): return False return own_book.first().user_id == int(user_id)
def GetLenderId(user_id_data, book_id_data): session = Session() borrowed_list = session.query(Lend_info).filter( Lend_info.borrower_id == user_id_data) # 以前に貸し出された本のリスト # 貸し出されたリストの中から、今回の本のidを探索する borrowed_data_list = [] for borrowed in borrowed_list: list = [] list.append(borrowed.id) ower_user_id, ower_book_id = GetLendUserAndBookIdByOwnBookId( borrowed.own_book_id) # own_book_idから情報を取得 list.append(ower_user_id) list.append(ower_book_id) borrowed_data_list.append(list) session.commit() #print(borrowed_data_list) # [[lend_id,ower_user_id,ower_book_id][...]] # 過去に貸し出されたかどうかを探す for i in range(len(borrowed_data_list)): if borrowed_data_list[i][2] == book_id_data: # 貸し出しの履歴の中に本の履歴があったら lend_user_id = borrowed_data_list[i][1] print("貸してくれた人:", lend_user_id, "|貸し出された本", book_id_data) return lend_user_id # 貸し出されていなかった場合 print("貸し出されていなかったです。") return None
def IsOwn(user_id, book_id): #所有判定 session = Session() own_inf = session.query(Own_Book).filter(Own_Book.user_id == user_id, Own_Book.book_id == book_id) session.commit() if own_inf.count() == 0: return False return True
def IsLending(own_book_id): session = Session() user_lend_info = session.query(Lend_info).filter( and_(Lend_info.is_valid, Lend_info.own_book_id == own_book_id)) session.commit() if (user_lend_info.count() == 0): return False return True
def IsContainBook(own_book_id, book_id): session = Session() cnt = session.query(Own_Book).filter(Own_Book.id == own_book_id, Own_Book.book_id == book_id).count() session.commit() if cnt == 0: return False return True
def GetLendUserAndBookIdByOwnBookId(own_book_id_data): # ownBookからowerとbookを選択 session = Session() bookdata = session.query(Own_Book).filter( Own_Book.id == own_book_id_data).all() for book in bookdata: ower_user_id = book.user_id ower_book_id = book.book_id return ower_user_id, ower_book_id
def IsHaveBook(book_id, user_id): session = Session() cnt = session.query(Own_Book).filter(Own_Book.book_id == book_id, Own_Book.user_id == user_id).count() session.commit() if cnt == 0: return False return True
def IsOwnBook(own_book_id, book_id): session = Session() own_info = session.query(Own_Book).filter(Own_Book.id == own_book_id, Own_Book.book_id == book_id) session.commit() if own_info.count() == 0: return False return True
def GetNotificationByUserId(user_id): #全ての通知を取得する session = Session() notification = session.query(Notification).filter(Notification.user_id == user_id) session.commit() notification_list = [] if notification != []: for noti in notification: notification_list.append( {"user_id":noti.user_id,"message":noti.message,"created_at":noti.created_at}) notification_list.reverse() # 通知を反転する(上が新しい物にする) print(notification_list) return notification_list
def GetDeadLine(borrower_id, book_id): session = Session() lend_info = session.query(Lend_info).filter( Lend_info.is_valid, Lend_info.borrower_id == borrower_id).all() session.commit() if lend_info != []: for lend in lend_info: if IsOwnBook(lend.own_book_id, book_id): return lend.deadline return "no date"
def GetOwnBookIDByUseridAndBookid(user_id, book_id): session = Session() own_book_list = session.query(Own_Book).filter( Own_Book.user_id == user_id, Own_Book.book_id == book_id).all() if own_book_list == []: return "Non" else: for own_book in own_book_list: own_book_id = own_book.id print(own_book_id) session.commit() return own_book_id
def Borrowing(borrower_id): session = Session() lend_info = session.query(Lend_info).filter( Lend_info.is_valid, Lend_info.borrower_id == borrower_id).all() session.commit() bookset = set() if lend_info != []: for lend in lend_info: own_book = GetBookIdByOwn(lend.own_book_id) if own_book != []: bookset.add(own_book[0].book_id) return bookset
def IsSameBookLending(borrower_id, book_id): session = Session() lend_info = session.query(Lend_info).filter( Lend_info.borrower_id == borrower_id).all() session.commit() if lend_info == []: return False for lend in lend_info: if IsContainBook(lend.own_book_id, book_id): return True return False
def AllBooks(): #基本機能全てを出す session = Session() booklist = session.query(Book).all() session.commit() store = [] if booklist != []: for book in booklist: store.append({ "id": book.id, "name": book.name, "price": book.price, "image": book.image, "info": book.info, "auther": book.auther, "url": book.url.split(',') }) return store
def AutoUpdateLendInfo(): session = Session() lend_info = session.query(Lend_info).filter( Lend_info.is_valid, # datetime.datetime.now() > datetime.datetime.strptime(Lend_info.deadline, '%Y/%m/%d %H:%M:%S') # ).query.update({ is_valid: False }) ).all() if lend_info != []: for lend in lend_info: if IsLendInfoUpdate(lend): print("Update " + str(lend.id) + "\n") lend.is_valid = False own_info = GetBookIdByOwn(lend.own_book_id) user_id = own_info[0].user_id book_id = own_info[0].book_id borrower_id = lend.borrower_id AddNotificationInAutoReturn(user_id, borrower_id, book_id) session.commit()
def AddNotification(user_id_data,message_data): # 通知を追加する session = Session() now_date = (datetime.datetime.now()) now_date_str = now_date.strftime('%Y/%m/%d %H:%M:%S.%f') session.add_all([ Notification( user_id = user_id_data , message = message_data ,created_at = now_date_str) ]) session.commit() print("通知の追加が完了しました")
def CntLendBook(book_id): session = Session() own_list = session.query(Own_Book).filter( Own_Book.book_id == book_id).all() cnt = 0 if own_list != []: for own in own_list: cnt += session.query(Lend_info).filter( Lend_info.own_book_id == own.id).count() session.commit() return cnt
def CntOwnBook(book_id): session = Session() cnt = session.query(Own_Book).filter(Own_Book.book_id == book_id).count() session.commit() return cnt
def CntLendBook(book_id): session = Session() own_list = session.query(Own_Book).filter( Own_Book.book_id == book_id).all() cnt = 0 if own_list != []: for own in own_list: cnt += session.query(Lend_info).filter( Lend_info.own_book_id == own.id).count() session.commit() return cnt session = Session() booklist = session.query(Book).all() rank_list = [] if booklist != []: for book in booklist: rank_list.append({ 'CntOwn': CntOwnBook(book.id), 'CntLend': CntLendBook(book.id), 'Rank': CntOwnBook(book.id) + CntLendBook(book.id), 'book': book }) print("id: " + str(book.id) + "\ncnt: " + str(CntOwnBook(book.id)) + "\nlend: " + str(CntLendBook(book.id)) + "\n") sorted_book_list = sorted(rank_list, key=lambda x: x['Rank'], reverse=True)
def AddOwnBooks(user_id_data, book_id_data): #own_bookにデータを追加する session = Session() session.add_all([Own_Book(user_id=user_id_data, book_id=book_id_data)]) print("購入ができました") session.commit()
def GetBookById(book_id): session = Session() book = session.query(Book).filter(Book.id == book_id).all() session.commit() return book #リスト
def AddPoint(user_id, add_point): session = Session() session.query(User).filter(User.id == user_id).update( {User.point: User.point + add_point}) session.commit()
def GetOwnBookById(own_book_id): session = Session() own_book = session.query(Own_Book).filter(Own_Book.id == own_book_id).all() session.commit() return own_book #リスト
def GetBookIdByOwn(own_id): session = Session() booklist = session.query(Own_Book).filter(Own_Book.id == own_id).all() session.commit() return booklist
def GetBookListByUser(user_id): session = Session() #こちらの方が適切かもしれない #Lend_infoの更新関数をここで呼び出す予定............................................................................................ AutoUpdateLendInfo() #is_validを利用して判断する. #貸し出し中の本( いらないかも ) user_lend_info_valid = session.query(Lend_info).filter( and_(Lend_info.is_valid, # IsOwnBookAndId( Lend_info.own_book_id, user_id ) )).all() user_lend_info = [] if user_lend_info_valid != []: for user_lend in user_lend_info_valid: if IsOwnBookAndId(user_lend.own_book_id, user_id): user_lend_info.append(user_lend) #借りている本 user_borrow_info = session.query(Lend_info).filter( and_(Lend_info.is_valid, Lend_info.borrower_id == user_id)).all() booklist_user = session.query(Own_Book).filter( and_(Own_Book.user_id == user_id, # not IsLending( Own_Book.id ) )).all() booklist = [] if booklist_user != []: for book in booklist_user: if not IsLending(book.id): booklist.append(book) session.commit() str = [] print(booklist) print(user_lend_info) print(user_borrow_info) if booklist != []: books = ChangeBooksFromOwnBook(booklist) for book in books: str.append({ "id": book.id, "name": book.name, "price": book.price, "image": book.image, "info": book.info, "auther": book.auther, "url": book.url.split(','), "status": "having", "deadline": None }) # print(str) if user_borrow_info != []: books = ChangeBooksFromLendInfo(user_borrow_info) for book_info in books: book = book_info["book"] str.append({ "id": book.id, "name": book.name, "price": book.price, "image": book.image, "info": book.info, "auther": book.auther, "url": book.url.split(','), "status": "borrowing", "deadline": book_info["deadline"] }) #print(str) if user_lend_info != []: books = ChangeBooksFromLendInfo(user_lend_info) for book_info in books: book = book_info["book"] str.append({ "id": book.id, "name": book.name, "price": book.price, "image": book.image, "info": book.info, "auther": book.auther, "url": book.url.split(','), "status": "lending", "deadline": book_info["deadline"] }) #print(str) return str