bottlepy / bottle-redis

27 stars 3 forks source link

Custom Decorators stopped working using apply #6

Closed bazeli closed 9 years ago

bazeli commented 9 years ago

Using the latest version of bottle-redis available here, I'm encountering a strange case where custom decorators are not being called if the bottle-redis plugin is installed. This behaviour does not affect the version of bottle-redis available on pypi (v0.2.1), nor does it occur with using other plugins (tested with bottle-sqlite, bottle-session). Tested on Bottle v0.12.8, Python 2.7.8 on OSX 10.10.1.

Example code below. If you run the app and attempt to go to http://localhost:8888/test, the expected behaviour should be a redirection to http://localhost:8888/unauthorized, with debug output in the console showing "authorizing request". Instead, nothing is output from the decorator and you are met with an "authorized" message.

Note that the code does not use the decorator as is, but via the apply parameter on the route.get function. Again, all works fine with the previous version v0.2.1 on pypi


import bottle
import bottle_redis
import redis
import functools

app = bottle.Bottle()

redis_plugin = bottle_redis.RedisPlugin()

cp1 = redis.ConnectionPool(host='localhost', port=6379, db=1)
redis_plugin.redisdb = cp1

app.install(redis_plugin) 

VALID_AUTH_ROLES = ('api','session','simple')

def authorizeRequest(authroles=None):
    print "authorizing request"
    if authroles not in VALID_AUTH_ROLES:
        bottle.redirect('/unauthorized')
    return True

def make_auth_decorator(authroles=None):
    def auth_require(authroles=authroles):
        def decorator(func):
            @functools.wraps(func)
            def wrapper(*a, **ka):
                authorizeRequest(authroles=authroles)
                f = func(*a, **ka)
                return f
            return wrapper
        return decorator
    return auth_require

authorize = make_auth_decorator()

@app.get('/unauthorized')
def do_fail(rdb):
    return "authorization failed"

@app.get('/test', apply=[authorize(authroles='test')])
def test(rdb):
    return "authorized"

app.run(host='localhost', port=8888, debug=True, reloader=True)
e1ty commented 9 years ago

Here's my pull request, that fixes this -> https://github.com/bottlepy/bottle-redis/pull/5

bazeli commented 9 years ago

Much thanks for the comment and fix.

@avelino Any luck getting this pull request accepted and pushed out to pypi? As it stands now, the master branch is broken.

avelino commented 9 years ago

Today generate release!