librelist archives

« back to archive

Intercept all requests

Intercept all requests

From:
Daniele Nicolodi
Date:
2011-05-25 @ 20:47
Hello,

I'm writing a small application for database management. I would like to
intercept all requests and check if the database schema version, stored
in the database itself, is the one the application expects, and redirect
to a page with database schema upgrade procedure instructions if it is not.

Looking at the documentation I'm unable to find how to do it.

I can do the check in a 'before_request' handler, but looks like in such
functions i'm unable to modify the response or the request to redirect
to the instructions page.

How can this be done?

Thank you. Cheers,
-- 
Daniele

Re: [flask] Intercept all requests

From:
Ron DuPlain
Date:
2011-05-25 @ 21:30
Hi Daniele,

On Wed, May 25, 2011 at 4:47 PM, Daniele Nicolodi <daniele@grinta.net> wrote:
> I'm writing a small application for database management. I would like to
> intercept all requests and check if the database schema version, stored
> in the database itself, is the one the application expects, and redirect
> to a page with database schema upgrade procedure instructions if it is not.
>
> Looking at the documentation I'm unable to find how to do it.
>
> I can do the check in a 'before_request' handler, but looks like in such
> functions i'm unable to modify the response or the request to redirect
> to the instructions page.
>
> How can this be done?

You can return a redirect in a before_request function.  To
illustrate, in the app below, navigating to the root URL will redirect
to /help.  You'll never see the index view function in this case, so
redirect carefully.

-Ron


    from flask import Flask, redirect, request, url_for

    app = Flask(__name__)

    @app.before_request
    def redirect_me():
        if request.endpoint != 'instructions':
            return redirect(url_for('instructions'))

    @app.route('/')
    def index():
        return 'Hello, world!'

    @app.route('/help/')
    def instructions():
        return 'How to ...'

Re: [flask] Intercept all requests

From:
Daniele Nicolodi
Date:
2011-05-25 @ 22:48
On 25/05/11 23:30, Ron DuPlain wrote:
> On Wed, May 25, 2011 at 4:47 PM, Daniele Nicolodi <daniele@grinta.net> wrote:
>> I'm writing a small application for database management. I would like to
>> intercept all requests and check if the database schema version, stored
>> in the database itself, is the one the application expects, and redirect
>> to a page with database schema upgrade procedure instructions if it is not.

> You can return a redirect in a before_request function.

Thank Ron, it works perfectly.

I was pretty sure I did try that and it didn't work. I need more sleep.

Cheers
-- 
Daniele

Re: [flask] Intercept all requests

From:
Ron DuPlain
Date:
2011-05-26 @ 12:04
On Wed, May 25, 2011 at 6:48 PM, Daniele Nicolodi <daniele@grinta.net> wrote:
> On 25/05/11 23:30, Ron DuPlain wrote:
>> On Wed, May 25, 2011 at 4:47 PM, Daniele Nicolodi <daniele@grinta.net> wrote:
>>> I'm writing a small application for database management. I would like to
>>> intercept all requests and check if the database schema version, stored
>>> in the database itself, is the one the application expects, and redirect
>>> to a page with database schema upgrade procedure instructions if it is not.
>
>> You can return a redirect in a before_request function.
>
> Thank Ron, it works perfectly.
>
> I was pretty sure I did try that and it didn't work. I need more sleep.

If you have suggestions for docs or snippets, let me know.

-Ron