librelist archives

« back to archive

Flask-Classy view and decorator

Flask-Classy view and decorator

From:
Jesaja Everling
Date:
2013-02-06 @ 21:29
Hi all,

I'm looking for advice on how to best implement a decorator that can
decorate a flask-classy view method.
I tried to implement such a decorator to be able to check for some
condition before a view is called, quite similar to the recipe by
Ishbir Singh: 
http://flask.pocoo.org/mailinglist/archive/2011/4/8/flask-principal-question/#3b873c48c48516624a3916fabf1e0cf5

Alas, I can make the decorator work either with views that do not take
a parameter, or with views that take a parameter - but not with both.
Flask-classy doesn't generate the url_rule for the other option,
respectively.

The reason is inspect.getargspec(method)[0], line 210 in the current
master revision. If I accept *args in the function returned by my
decorator to make it work for views with and without arguments (POST
and GET for example), this returns only ['self'], which means no route
for GET is created.
Without the decorator, it returns e.g. ['self', 'oid'], and the
correct route for GET is created.

If i define the route manually, it works as expected. It would of
course also be possible to use different decorators for different
views.

Can anybody think of an easy way that I'm overlooking that would work
both with views that accepts parameters and views without parameters?
It's no big problem, just manually defining the route works fine as a
workaround, it's only the automatic route generation that doesn't work
when using the decorator.


Thanks in advance,

Jesaja Everling

Re: [flask] Flask-Classy view and decorator

From:
Freedom Dumlao
Date:
2013-02-07 @ 01:21
Hey Jesaja -

Do you have some code I could use to test this? It sounds like I might have
a bug in Flask-Classy and if so I'd be happy to work on getting it fixed!

- Freedom


On Wed, Feb 6, 2013 at 4:29 PM, Jesaja Everling <jeverling@gmail.com> wrote:

> Hi all,
>
> I'm looking for advice on how to best implement a decorator that can
> decorate a flask-classy view method.
> I tried to implement such a decorator to be able to check for some
> condition before a view is called, quite similar to the recipe by
> Ishbir Singh:
> 
http://flask.pocoo.org/mailinglist/archive/2011/4/8/flask-principal-question/#3b873c48c48516624a3916fabf1e0cf5
>
> Alas, I can make the decorator work either with views that do not take
> a parameter, or with views that take a parameter - but not with both.
> Flask-classy doesn't generate the url_rule for the other option,
> respectively.
>
> The reason is inspect.getargspec(method)[0], line 210 in the current
> master revision. If I accept *args in the function returned by my
> decorator to make it work for views with and without arguments (POST
> and GET for example), this returns only ['self'], which means no route
> for GET is created.
> Without the decorator, it returns e.g. ['self', 'oid'], and the
> correct route for GET is created.
>
> If i define the route manually, it works as expected. It would of
> course also be possible to use different decorators for different
> views.
>
> Can anybody think of an easy way that I'm overlooking that would work
> both with views that accepts parameters and views without parameters?
> It's no big problem, just manually defining the route works fine as a
> workaround, it's only the automatic route generation that doesn't work
> when using the decorator.
>
>
> Thanks in advance,
>
> Jesaja Everling
>

Re: [flask] Flask-Classy view and decorator

From:
Jesaja Everling
Date:
2013-02-07 @ 09:28
Hi Freedom,

sorry, I should have attached some code in the first place. I wouldn't
say this is a bug in Flask-Classy. At import time, where the routes
are set up, you cannot really know what parameters a view will accept
if it uses *args. I think the best solution would be to write a
decorator that inspects the parameters that are passed to the wrapped
function, and then returns a function that accepts the right number of
arguments. But manually defining the route or using different
decorators work fine.
I have made a simple example here: http://pastebin.com/7qwwCpQR
http://127.0.0.1:5000/dec/1 should return status 401, but instead it's
404 since the route never get's set up.
Hopes that makes it clear.


Best Regards,

Jesaja Everling

On Thu, Feb 7, 2013 at 2:21 AM, Freedom Dumlao <freedomdumlao@gmail.com> wrote:
> Hey Jesaja -
>
> Do you have some code I could use to test this? It sounds like I might have
> a bug in Flask-Classy and if so I'd be happy to work on getting it fixed!
>
> - Freedom
>
>
> On Wed, Feb 6, 2013 at 4:29 PM, Jesaja Everling <jeverling@gmail.com> wrote:
>>
>> Hi all,
>>
>> I'm looking for advice on how to best implement a decorator that can
>> decorate a flask-classy view method.
>> I tried to implement such a decorator to be able to check for some
>> condition before a view is called, quite similar to the recipe by
>> Ishbir Singh:
>> 
http://flask.pocoo.org/mailinglist/archive/2011/4/8/flask-principal-question/#3b873c48c48516624a3916fabf1e0cf5
>>
>> Alas, I can make the decorator work either with views that do not take
>> a parameter, or with views that take a parameter - but not with both.
>> Flask-classy doesn't generate the url_rule for the other option,
>> respectively.
>>
>> The reason is inspect.getargspec(method)[0], line 210 in the current
>> master revision. If I accept *args in the function returned by my
>> decorator to make it work for views with and without arguments (POST
>> and GET for example), this returns only ['self'], which means no route
>> for GET is created.
>> Without the decorator, it returns e.g. ['self', 'oid'], and the
>> correct route for GET is created.
>>
>> If i define the route manually, it works as expected. It would of
>> course also be possible to use different decorators for different
>> views.
>>
>> Can anybody think of an easy way that I'm overlooking that would work
>> both with views that accepts parameters and views without parameters?
>> It's no big problem, just manually defining the route works fine as a
>> workaround, it's only the automatic route generation that doesn't work
>> when using the decorator.
>>
>>
>> Thanks in advance,
>>
>> Jesaja Everling
>
>