Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate() decorator cannot work with sqlite.plugin #248

Closed
andycjw opened this issue Nov 7, 2011 · 6 comments
Closed

validate() decorator cannot work with sqlite.plugin #248

andycjw opened this issue Nov 7, 2011 · 6 comments

Comments

@andycjw
Copy link

andycjw commented Nov 7, 2011

the @Validate decorator will not work if used together with sqlite plugin since it requires the use of non-keyword argument
using the web todo list tutorial from official website as example

...
@route('/edit/:no', method='GET', apply=sqlite.Plugin(dbfile='todo.db'))
@Validate(no=int)
def edit_item(db, no):
...

calling the url @ /edit/2 will return http error 500
TypeError('edit_item() takes exactly 2 non-keyword arguments (0 given)',)

@iurisilvio
Copy link
Member

Add validate decorator to apply parameter.

...
@route('/edit/:no', method='GET', apply=[validate(no=int), sqlite.Plugin(dbfile='todo.db')])
def edit_item(db, no):
    ...

@andycjw
Copy link
Author

andycjw commented Nov 8, 2011

this works, but can anyone explain why this works while the decorator style doesn't?

@iurisilvio
Copy link
Member

The decorator style wraps the callback with another function with different arguments.

The apply parameter in @route decorator use functools.update_wrapper to apply these decorators, so it maintains the callback signature.

http://docs.python.org/library/functools.html#functools.update_wrapper

@andycjw
Copy link
Author

andycjw commented Nov 8, 2011

is it possible to add @wraps decorator inside the validate decorator in bottle.py to fix it?
this way the validate decorator will works without breaking anything, please correct me if i'm wrong

@iurisilvio
Copy link
Member

I never did it, but it is what bottle do. Probably it works.

@defnull
Copy link
Member

defnull commented Nov 10, 2011

Should work now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants