Created
April 8, 2015 17:39
-
-
Save coryvirok/62517094e542a9c7006f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Instead of causing a 404 for cat IDs that are not found, return the parent resource. | |
""" | |
class Root(object): | |
def __getitem__(self, key): | |
if key == 'cats': | |
res = CatList() | |
res.__name__ = key | |
res.__parent__ = self | |
raise KeyError() | |
class CatList(object): | |
def __getitem__(self, key): | |
# Find a cat | |
cat = get_cat(key) | |
if cat: | |
res = CatResource(cat) | |
res.__name__ = key | |
res.__parent__ = self | |
return res | |
else: | |
# You can also raise an HTTPNotFound() here or | |
# create a CatResource(None) and set __name__ = '' | |
# to signal that the cat identifier wasn't found. | |
return self.__parent__ | |
class CatResource(object): | |
def __init__(self, cat): | |
self.cat = cat | |
def __getitem__(self, key): | |
return key | |
site_root = Root() | |
@view_config(context=CatList, renderer='cat_list.mako') | |
def cat_list(context, request): | |
return dict() | |
@view_config(context=Cat, name='', renderer='cat_view.mako') | |
def cat_view(context, request): | |
return dict(cat=context.cat) | |
@view_config(context=Cat, name='owners', renderer='cat_owners.mako') | |
def cat_owners(context, request): | |
return dict(cat=context.cat) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment