matching_user = User.get_one(username=kw['username'], password=kw['password']) if not matching_user: # no user found with this user/pass combination raise Exception("Your username and/or password is incorrect.") # everything looks OK: save this user to their session object kw['_session']['session']['user'] = matching_user # their cookie doesn't need to be updated, since we didn't change the session itself return True def logout(**kw): # we are forcing a session, so kw['_session']['session'] cannot be None if not kw['_session']['session']['user']: # a user object does not exist in this session raise Exception("You are not logged in.") # everything looks OK: remove the user from this session object kw['_session']['session']['user'] = None # their cookie doesn't need to be updated, since we didn't change the session itself return True if __name__ == "__main__": print serve_api(Session, User, Book, login, logout)
Field('isbn', str), Field('condition', str) ] books_table = Table('book', fields) Book = link_table(books_table, con) fields = [ Field('id', int, pk=True), Field('name', str), Field('age', int), Field('happy', bool) ] people_table = Table('person', fields) Person = link_table(people_table, con) Person.has_one(Book) def check_credit(**kw): if 'credit_card' not in kw or not kw['credit_card'].isdigit(): raise Exception("Please enter a valid credit card number.") credit_sum = 0 for digit in kw['credit_card']: credit_sum += int(digit) return {'sum': credit_sum} if __name__ == "__main__": print serve_api(Book, Person, check_credit)