Skip to content

Instantly share code, notes, and snippets.

@edelooff
Created June 2, 2018 10:14
Show Gist options
  • Save edelooff/7788f7ff7cde32a79d5d7487ba4142f1 to your computer and use it in GitHub Desktop.
Save edelooff/7788f7ff7cde32a79d5d7487ba4142f1 to your computer and use it in GitHub Desktop.
Example uses of Kalpa
from kalpa import Root, Node, branch
USERS = {...}
class Root(Root):
"""Traversal root for Pyramid application."""
users = branch('UserCollection')
class UserCollection(Node):
"""User collection, for listings, or loading single users."""
__branching_node__ = 'User'
def __load__(self, key):
"""Return child resource with requested user included."""
return {'user': USERS[key]} # Load user or raise KeyError.
class User(Node):
"""User resource, a single loaded user."""
gallery = branch('UserGallery', aliases=['images'])
class UserGallery(Node):
"""Gallery of images posted by a user.
Reachable as `/users/:id/gallery` but also `/users/:id/images`.
"""
from kalpa import Root, Branch, Leaf
USERS = {...}
@Root.attach('users')
class UserCollection(Branch):
"""User collection, for listings, or loading single users."""
def __load__(self, key):
"""Return child resource with requested user included."""
user = USERS[key] # Load user or raise KeyError.
return self._child(key, user=user)
@UserCollection.child_resource
class User(Branch):
"""User resource, a single loaded user."""
@User.attach('gallery', aliases=['images'])
class UserGallery(Leaf):
"""Gallery of images posted by a user.
Reachable as `/users/:id/gallery` but also `/users/:id/images`.
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment