librelist archives

« back to archive

Selectively applying before_request to view functions

Selectively applying before_request to view functions

From:
Calvin Young
Date:
2012-08-31 @ 17:38
What's the best way to define a before_request function that only gets
executed for a few view functions in an application?

For example, in Pylons, one can define a __before__ method in a controller
class that only gets executed before the methods in that controller.

Thanks!

Re: [flask] Selectively applying before_request to view functions

From:
Steven Kryskalla
Date:
2012-08-31 @ 18:05
On Fri, Aug 31, 2012 at 10:38 AM, Calvin Young <calvinwyoung@gmail.com> wrote:
> What's the best way to define a before_request function that only gets
> executed for a few view functions in an application?
>
> For example, in Pylons, one can define a __before__ method in a controller
> class that only gets executed before the methods in that controller.

You can group the view methods into a blueprint (which are sort of
like controllers in Flask), then use @blueprint.before_request.

Re: [flask] Selectively applying before_request to view functions

From:
Calvin Young
Date:
2012-08-31 @ 18:21
Does it make sense for view functions to be in multiple blueprints?  I'm
already using blueprints to split up different parts of my app (e.g., main
site, admin console, etc).  If I want to use blueprints to organize view
functions within each component, then I'll need to apply more than one
blueprint to each view function.  Is this the recommended design?  (This
seems a bit strange to me because it uses blueprints for two fundamentally
different purposes.)

And as a follow-up question, if I do end up using blueprints to organize my
views into controllers, is there any way to easily register all blueprints
on the app without having to import and call register_blueprint() on each
one manually?  I have a large application with many controllers, and it'd
be nice if I didn't have to do this each time I create a new controller.

On Fri, Aug 31, 2012 at 11:05 AM, Steven Kryskalla <skryskalla@gmail.com>wrote:

> On Fri, Aug 31, 2012 at 10:38 AM, Calvin Young <calvinwyoung@gmail.com>
> wrote:
> > What's the best way to define a before_request function that only gets
> > executed for a few view functions in an application?
> >
> > For example, in Pylons, one can define a __before__ method in a
> controller
> > class that only gets executed before the methods in that controller.
>
> You can group the view methods into a blueprint (which are sort of
> like controllers in Flask), then use @blueprint.before_request.
>

Re: [flask] Selectively applying before_request to view functions

From:
Steven Kryskalla
Date:
2012-08-31 @ 18:40
On Fri, Aug 31, 2012 at 11:21 AM, Calvin Young <calvinwyoung@gmail.com> wrote:
> Does it make sense for view functions to be in multiple blueprints?  I'm
> already using blueprints to split up different parts of my app (e.g., main
> site, admin console, etc).  If I want to use blueprints to organize view
> functions within each component, then I'll need to apply more than one
> blueprint to each view function.  Is this the recommended design?  (This
> seems a bit strange to me because it uses blueprints for two fundamentally
> different purposes.)

Wouldn't you have the same problem with controllers in pylons? You
can't have a function be a member of two different controllers, right?
That wasn't what I was recommending. What I was recommending was that
if for example you wanted to verify only authenticated users could
access your admin methods, that you organize all your admin methods
into a blueprint, then you could do the user check in the
before_request of that blueprint.

If you've already split your app up into blueprints and before_request
wouldn't work cleanly, then I would just write a decorator and apply
that decorator specifically to the views you want it to run on. Like
this:

http://flask.pocoo.org/docs/patterns/viewdecorators/

> And as a follow-up question, if I do end up using blueprints to organize my
> views into controllers, is there any way to easily register all blueprints
> on the app without having to import and call register_blueprint() on each
> one manually?  I have a large application with many controllers, and it'd be
> nice if I didn't have to do this each time I create a new controller.

I don't think there is a standard way to automatically mount
blueprints on your app. You would have to write your own "plugin
system" to do that kind of thing, e.g. by subclassing the Blueprint
class to add this behavior, or with a config file that specified all
the blueprints to mount, etc. This article has some really good
information on plugin systems in python:


http://eli.thegreenplace.net/2012/08/07/fundamental-concepts-of-plugin-infrastructures/

-steve

Re: [flask] Selectively applying before_request to view functions

From:
Calvin Young
Date:
2012-09-01 @ 03:03
Awesome, thanks Steve!

Calvin

On Fri, Aug 31, 2012 at 11:40 AM, Steven Kryskalla <skryskalla@gmail.com>wrote:

> On Fri, Aug 31, 2012 at 11:21 AM, Calvin Young <calvinwyoung@gmail.com>
> wrote:
> > Does it make sense for view functions to be in multiple blueprints?  I'm
> > already using blueprints to split up different parts of my app (e.g.,
> main
> > site, admin console, etc).  If I want to use blueprints to organize view
> > functions within each component, then I'll need to apply more than one
> > blueprint to each view function.  Is this the recommended design?  (This
> > seems a bit strange to me because it uses blueprints for two
> fundamentally
> > different purposes.)
>
> Wouldn't you have the same problem with controllers in pylons? You
> can't have a function be a member of two different controllers, right?
> That wasn't what I was recommending. What I was recommending was that
> if for example you wanted to verify only authenticated users could
> access your admin methods, that you organize all your admin methods
> into a blueprint, then you could do the user check in the
> before_request of that blueprint.
>
> If you've already split your app up into blueprints and before_request
> wouldn't work cleanly, then I would just write a decorator and apply
> that decorator specifically to the views you want it to run on. Like
> this:
>
> http://flask.pocoo.org/docs/patterns/viewdecorators/
>
> > And as a follow-up question, if I do end up using blueprints to organize
> my
> > views into controllers, is there any way to easily register all
> blueprints
> > on the app without having to import and call register_blueprint() on each
> > one manually?  I have a large application with many controllers, and
> it'd be
> > nice if I didn't have to do this each time I create a new controller.
>
> I don't think there is a standard way to automatically mount
> blueprints on your app. You would have to write your own "plugin
> system" to do that kind of thing, e.g. by subclassing the Blueprint
> class to add this behavior, or with a config file that specified all
> the blueprints to mount, etc. This article has some really good
> information on plugin systems in python:
>
>
> 
http://eli.thegreenplace.net/2012/08/07/fundamental-concepts-of-plugin-infrastructures/
>
> -steve
>