Pylons / pyramid_handlers

Pyramid "handlers" emulate Pylons 1 "controllers"
Other
8 stars 9 forks source link

CustomPredicate attached base url is incorrectly applied to generic handler #6

Open trilogysci opened 11 years ago

trilogysci commented 11 years ago

There is a error where if a custompredicate is applied to an action of a base url, then that predicate is also incorrectly applied to generic urls e.g. {action} of the same base url.

config.add_handler('base_index', '/base', handler="dummy:SimpleHandler", action="index")
config.add_handler('base_others', '/base/{action}', handler="dummy:SimpleHandler")

From example below http://localhost:3000/base calls MyPredicate (correct) http://localhost:3000/base/success does not call MyPredicate (correct) http://localhost:3000/base/fail calls MyPredicate (incorrect)

#dummy.__init__.py
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
from pyramid_handlers import *
from pyramid.httpexceptions import HTTPFound, HTTPForbidden, HTTPNotFound, HTTPUnauthorized

class MyPredicate(object):
    __text__ = "Unapproved Role"

    def __init__(self):
        pass

    def __call__(self, info, request):
        print "warning calling MyPredicate for %s",request.url
        request.fail=True
        return True

class SimpleHandler(object):

    def __init__(self, request):
        self.request = request

    @action(renderer="string")
    def fail(self):
        extra='MyPredicate not called.'
        if hasattr(self.request,'fail') and self.request.fail:
            extra='MyPredicate called!'
        return Response('Running fail. '+extra)

    @action(renderer="string")
    def success(self):
        extra='MyPredicate not called.'
        if hasattr(self.request,'fail') and self.request.fail:
            extra='MyPredicate called!'
        return Response('Running success. '+extra)

    @action(renderer="string", custom_predicates=(MyPredicate(), ))
    def index(self):
        extra='MyPredicate not called.'
        if hasattr(self.request,'fail') and self.request.fail:
            extra='MyPredicate called!'
        return Response('Hello World! '+extra)

def hello_world(request):
    return Response('Hello %(name)s!' % request.matchdict)

if __name__ == '__main__':
    config = Configurator()
    config.include('pyramid_handlers')

    config.add_route('hello', '/hello/{name}')
    config.add_view(hello_world, route_name='hello')
    config.add_handler('base_index', '/base', handler="dummy:SimpleHandler", action="index")
    config.add_handler('base_success', '/base/success', handler="dummy:SimpleHandler", action="success")
    config.add_handler('base_others', '/base/{action}', handler="dummy:SimpleHandler")

    app = config.make_wsgi_app()
    print "connect to http://localhost:3000/base"
    server = make_server('0.0.0.0', 3000, app)
    server.serve_forever()