librelist archives

« back to archive

How to access confing settings in a view decorator

How to access confing settings in a view decorator

From:
Alex
Date:
2013-03-06 @ 15:25
Hi all.

In a blueprint I created a simple view decorator to restrict access to
some views. The decorator takes as argument a list of allowed IP
addresses and grant access only if the request IP is included in the
list of allowed IP.
I would like to store the list of allowed IP in the application
settings but I don't know how can I pass a value stored in settings
from a decorator since current_app is only accessible in request
context.

Thank in advance.

alex

Re: [flask] How to access confing settings in a view decorator

From:
Jose Ayerdis
Date:
2013-03-06 @ 15:39
I am not quite sure what you want to do if you can provide an example. But
you can access the current_app without any problem the current app is not
request dependant.

http://flask.pocoo.org/docs/appcontext/

couldn't just do current_app.get_current_object() too

Sincerly yours,

[Jose Luis Ayerdis Espinoza]
Necronet.info |
LinkedIn<http://www.linkedin.com/pub/jose-luis-ayerdis-espinoza/28/7b4/704>|
Careers
StackOverflow <http://careers.stackoverflow.com/necronet>


2013/3/6 Alex <thinkpragmatic@gmail.com>

> Hi all.
>
> In a blueprint I created a simple view decorator to restrict access to
> some views. The decorator takes as argument a list of allowed IP
> addresses and grant access only if the request IP is included in the
> list of allowed IP.
> I would like to store the list of allowed IP in the application
> settings but I don't know how can I pass a value stored in settings
> from a decorator since current_app is only accessible in request
> context.
>
> Thank in advance.
>
> alex
>

Re: [flask] How to access confing settings in a view decorator

From:
Alexander E. Patrakov
Date:
2013-03-06 @ 15:36
2013/3/6 Alex <thinkpragmatic@gmail.com>:
> Hi all.
>
> In a blueprint I created a simple view decorator to restrict access to
> some views. The decorator takes as argument a list of allowed IP
> addresses and grant access only if the request IP is included in the
> list of allowed IP.
> I would like to store the list of allowed IP in the application
> settings but I don't know how can I pass a value stored in settings
> from a decorator since current_app is only accessible in request
> context.

Your decorator probably looks like this:

def limit_access(view):
    @functools.wraps(view)
    def wrapped_view(*args, **kwargs):
        if not access_allowed():
            abort(403)
        return view(*args, **kwargs)

    return wrapped_view

As you can see, access_allowed() is called from wrapped_view, in the
request context, and thus has access both to request and current_app.

-- 
Alexander E. Patrakov

Re: [flask] How to access confing settings in a view decorator

From:
Alexander E. Patrakov
Date:
2013-03-06 @ 15:49
2013/3/6 Alex <thinkpragmatic@gmail.com>:
> Hi all.
>
> In a blueprint I created a simple view decorator to restrict access to
> some views. The decorator takes as argument a list of allowed IP
> addresses and grant access only if the request IP is included in the
> list of allowed IP.

Sorry for a double-answer, but I'd like to clarify.

The idea that the list of IP addresses has to be provided as a
decorator argument is wrong. i.e. you are probably trying to do this,
and it can't work:

@bp.route('/admin')
@limit_access(current_app.config['ADMIN_IPS'])
def view():
    pass

You should probably do this instead:

@bp.route('/admin')
@limit_access('ADMIN_IPS')
def view():
    pass

and then inside the inner function of limit_access, use
current_app.config[acl_name] where acl_name is the argument of
limit_access.


-- 
Alexander E. Patrakov

Re: [flask] How to access confing settings in a view decorator

From:
Alex
Date:
2013-03-06 @ 21:47
Thank you, this works and is exactly what I needed.

Alex

On Wed, Mar 6, 2013 at 4:49 PM, Alexander E. Patrakov
<patrakov@gmail.com> wrote:
> 2013/3/6 Alex <thinkpragmatic@gmail.com>:
>> Hi all.
>>
>> In a blueprint I created a simple view decorator to restrict access to
>> some views. The decorator takes as argument a list of allowed IP
>> addresses and grant access only if the request IP is included in the
>> list of allowed IP.
>
> Sorry for a double-answer, but I'd like to clarify.
>
> The idea that the list of IP addresses has to be provided as a
> decorator argument is wrong. i.e. you are probably trying to do this,
> and it can't work:
>
> @bp.route('/admin')
> @limit_access(current_app.config['ADMIN_IPS'])
> def view():
>     pass
>
> You should probably do this instead:
>
> @bp.route('/admin')
> @limit_access('ADMIN_IPS')
> def view():
>     pass
>
> and then inside the inner function of limit_access, use
> current_app.config[acl_name] where acl_name is the argument of
> limit_access.
>
>
> --
> Alexander E. Patrakov

Re: [flask] How to access confing settings in a view decorator

From:
Jose Ayerdis
Date:
2013-03-06 @ 16:10
That seem more logical!

@bp.route('/admin')
@limit_access('ADMIN_IPS')
def view():
    pass

Sincerly yours,

[Jose Luis Ayerdis Espinoza]
Necronet.info |
LinkedIn<http://www.linkedin.com/pub/jose-luis-ayerdis-espinoza/28/7b4/704>|
Careers
StackOverflow <http://careers.stackoverflow.com/necronet>


2013/3/6 Alexander E. Patrakov <patrakov@gmail.com>

> 2013/3/6 Alex <thinkpragmatic@gmail.com>:
> > Hi all.
> >
> > In a blueprint I created a simple view decorator to restrict access to
> > some views. The decorator takes as argument a list of allowed IP
> > addresses and grant access only if the request IP is included in the
> > list of allowed IP.
>
> Sorry for a double-answer, but I'd like to clarify.
>
> The idea that the list of IP addresses has to be provided as a
> decorator argument is wrong. i.e. you are probably trying to do this,
> and it can't work:
>
> @bp.route('/admin')
> @limit_access(current_app.config['ADMIN_IPS'])
> def view():
>     pass
>
> You should probably do this instead:
>
> @bp.route('/admin')
> @limit_access('ADMIN_IPS')
> def view():
>     pass
>
> and then inside the inner function of limit_access, use
> current_app.config[acl_name] where acl_name is the argument of
> limit_access.
>
>
> --
> Alexander E. Patrakov
>