本文整理汇总了Python中books.models.Book.get_by_key方法的典型用法代码示例。如果您正苦于以下问题:Python Book.get_by_key方法的具体用法?Python Book.get_by_key怎么用?Python Book.get_by_key使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类books.models.Book
的用法示例。
在下文中一共展示了Book.get_by_key方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: library_requests
# 需要导入模块: from books.models import Book [as 别名]
# 或者: from books.models.Book import get_by_key [as 别名]
def library_requests(OLKey):
cur_user = current_user()
if not cur_user:
logging.info("there is not a user logged in")
return "<a href='%s' >Login</a>" %users.create_login_url(dest_url=url_for('library_requests',ISBN=ISBN))
if request.method == 'GET':
#check the database to see if the book is in the user's library
book = Book.get_by_key(OLKey)
if book:
if cur_user.get_book(book):
return book.title
else:
return "You do not have this book in your library"
else:
return "This book was not found"
elif request.method == 'POST':
#add the book to the user's library
#If not found, add it to the cache, then to the user's library
book = Book.get_by_key(OLKey)
if not book:
return "Book " + OLKey + " was not found"
else:
if cur_user.get_book(book):
return "This book is already in your library"
cur_user.add_book(book)
return "Book " + OLKey + " was added to your library"
elif request.method == 'DELETE':
#remove the book from the user's library
book = Book.get_by_key(OLKey)
if not book:
return "Book not found"
else:
if cur_user.get_book(book):
cur_user.remove_book(book)
return "Successfully deleted " + OLKey + " from your library"
else:
#this should never be reached
return "Error: http request was invalid"