Skip to content

Instantly share code, notes, and snippets.

@dittos dittos/
Created Sep 2, 2014

What would you like to do?
class Handler(BaseHandler):
def db(self):
if not hasattr(self, '_db'):
self._db = Session(
return self._db
def teardown_request(self):
db = getattr(self, '_db', None)
if db:
def serialize_posts(posts):
return dict(data=map(PostSerializer, posts))
class PostSerializer(Serializer):
id = Integer()
title = String()
body = String()
class PostsHandler(Handler):
@argument('limit', type=int)
def get(self, limit):
if limit is None:
limit =['DEFAULT_LIMIT']
return self.db.query(Post).limit(limit).order_by(
class PostHandler(Handler):
responder = PostSerializer
@argument('id', type=int)
def before_request(self, id):
super(PostHandler, self).before_request(self)
self._post = self.db.query(Post).get(id)
if not self._post:
raise NotFound()
def get(self):
return self._post
def put(self, title, body):
if title is not None:
self._post.title = title
if body is not None:
self._post.body = body
return self._post
app = App(handlers=(
('/posts', PostsHandler),
('/posts/<id>', PostHandler),
if __name__ == '__main__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.