def borrow_book(isbn: str, created_by: str, borrowed_by: str): """ Borrow the book """ try: book = Books.get_book_by_isbn(isbn) except: return common_response.DATABASE_ERROR if len(book) == 0: return common_response.BOOK_NOT_FOUND if book[0].get("status") != AVAILABLE: return {"status": "failed", "error_message": "Book is not available"} try: Books.update_book(isbn=book[0]['isbn'], status="Borrowed") except Exception as e: return common_response.DATABASE_ERROR try: History.add(transaction_id=str(uuid.uuid4()), isbn=isbn, transaction_date=datetime.utcnow(), action="Borrowed", created_by=created_by, action_by=borrowed_by) except: # Rollback Books.update_book(isbn=book.isbn, status=AVAILABLE) return common_response.DATABASE_ERROR return common_response.SUCCESS
def search(typ: str, isbn: str = "", title: str = ""): """ search book history by isbn or title """ if typ not in ["isbn", "title"]: return { "status": "failed", "error_message": "invalid typ" }, 400 if typ == "isbn": try: history = History.search_history_by_isbn(isbn) except Exception as e: return {'e': str(e)} if not history: return common_response.BOOK_NOT_FOUND else: try: history = History.search_history_by_title(title) except: return common_response.DATABASE_ERROR if not history: return common_response.BOOK_NOT_FOUND return {"history": history}
def add_usage(self, config: str, access_id: str): req_time = time.time() new_record = History(access_id=access_id, resource_name=config, access_at=req_time) self.db.add(new_record) self.db.commit()
def chart2(): form =ChartForm().check_param() today = datetime.date.today() target = (today - datetime.timedelta(days=8)) target_res = target.strftime("%Y-%m-%d") date_list = func(today,[])[::-1] result= History.get_chat2(target_res,today,date_list) if result: return jsonify(true_return(msg="请求成功",data={"data":result,"date_list":date_list})) else: return jsonify(false_return(msg="请求失败"))
def return_book(isbn: str, created_by: str, returned_by: str): """ return book to library """ try: book = Books.get_book_by_isbn(isbn) except: return common_response.DATABASE_ERROR if len(book) == 0: return common_response.BOOK_NOT_FOUND if book[0].get("status") != BORROWED: return { "status": "failed", "error_message": "Book is not borrowed" }, 500 try: Books.update_book(isbn=book[0]['isbn'], status=AVAILABLE) except Exception as e: return common_response.DATABASE_ERROR try: History.add(transaction_id=str(uuid.uuid4()), isbn=isbn, transaction_date=datetime.utcnow(), action=AVAILABLE, created_by=created_by, action_by=returned_by) except: # Rollback Books.update_book(isbn=book.isbn, status=BORROWED) return common_response.DATABASE_ERROR return common_response.SUCCESS
def chart(): form =ChartForm().check_param() # if form.date.data: # start =form.date.data[0][:10] # end =form.date.data[1][:10] # date_list = [] # date_list = func_days(start, end, date_list)[::-1] # result, result1, result2 = History.get_chat(date_list) # else: today = datetime.date.today() date_list=[x for x in range(24)] result,result1,result2,result3,sy = History.get_chat1(today.__str__()) if result: return jsonify(true_return(msg="请求成功",data={"data":result,"date_list":date_list,"data1":result1,"data2":result2,"data3":result3,"sy":sy})) else: return jsonify(false_return(msg="请求失败"))
def meslist(): form = ListForm().check_param() query =History.get_mes(form) page = int(form.page_index.data) page_size = form.page_size.data result= [x.to_dict() for x in query.items] length_query = query.total total_count = math.ceil(length_query / page_size) pagination = { 'pagination': { 'total_count': length_query, 'page_count': total_count, 'count': page_size, "page_index": int(page), }, } if result: return jsonify(true_return(msg="请求成功",data={"pagination":pagination["pagination"],"data":result})) else: return jsonify(false_return(msg="请求失败"))