def get(self, book_id=None): #if a book id is sent, return the book if book_id: #if book not found will cause an error try: book = ndb.Key(urlsafe=book_id).get() #make sure is book and not customer assert Book.is_book(book) self.write_json(book.to_json()) except: #error on not found self.response.set_status(404) #return list of all books else: #if request parameter not sent, will be empty string #convert to lowercase so we get case insensitive string comparison #http://stackoverflow.com/questions/319426/how-do-i-do-a-case-insensitive-string-comparison-in-python checked_out_parameter = self.request.get('checkedIn').lower() #just show books that are not checked in if checked_out_parameter == 'false': books = Book.query(Book.checkedIn == False).fetch() #just show books that are checkedIn elif checked_out_parameter == 'true': books = Book.query(Book.checkedIn == True).fetch() #show all books else: books = Book.all() self.write_json(Book.all_to_json(books))
def get(self, customer_id, book_id=None): #if customer not found will cause an error #or if key of non-customer object (such as a book) will also cause error try: customer = ndb.Key(urlsafe=customer_id).get() #get a single book if book_id: book = ndb.Key(urlsafe=book_id).get() #check to make sure customer checked out this book if book.key not in customer.checked_out: #not found self.response.set_status(404) return response_data = book.to_json() #get all of customer's checked out books else: books = map(lambda book_key: book_key.get(), customer.checked_out) response_data = Book.all_to_json(books) except: #error on customer not found, or book_id was invalid self.response.set_status(404) return self.write_json(response_data)