Created
January 3, 2011 19:39
-
-
Save reedobrien/763833 to your computer and use it in GitHub Desktop.
a view mapper fantasy for pyramid
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
# framework | |
# from http://paste.ofcode.org/bnQGsYx8FWNGTBbYPS6W4k | |
class PylonsControllerViewMapper(object): | |
def __init__(self, **kw): | |
self.kw = kw | |
def __call__(self, view): | |
attr = self.kw['attr'] | |
def wrapper(context, request): | |
matchdict = request.matchdict.copy() | |
matchdict.pop('action', None) | |
inst = view() | |
meth = getattr(inst, attr) | |
return meth(**matchdict) | |
return wrapper | |
class BaseController(object): | |
__view_mapper__ = PylonsControllerViewMapper | |
# user application | |
from webob import Response | |
from pyramid.config import Configurator | |
from paste.httpserver import serve | |
class MyController(BaseController): | |
def index(self, id): | |
return Response(id) | |
if __name__ == '__main__': | |
config = Configurator() | |
config.add_handler('one', '/{id}', MyController, action='index') | |
config.add_handler('two', '/{action}/{id}', MyController) | |
serve(config.make_wsgi_app()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment