Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.