librelist archives

« back to archive

Accessing Request & Session from Blueprints

Accessing Request & Session from Blueprints

From:
Andy Bowes
Date:
2013-11-04 @ 15:32
Hi

We have a common component for the handling of Session Expiry that we would
like to use across all of our Flask Web Applications.  This primarily
consists of a small JavaScript file and a registered URL which checks the
TimeToLive of the current session.

I was hoping to be able to use a Blueprint which could be registered in
each application that requires this functionality but I am hitting an issue
as I don't seem to be able to access the 'request' or 'session' object from
within the function which is registered with the Route in the Blueprint.

My current Blueprint looks something like this & I would like to be able to
access the request, session & application objects from within the
sessionstate() function:

from flask import Blueprint
sessionexpiry = Blueprint('sessionexpiry', __name__, 'static')

@sessionexpiry.route('/sessionstate')
def sessionstate():
    """
    Implementation of the get request for this view.
    """
    # Get the Session ID from the request/session object
    # Use the Session Interface on the Application to get the Time To Live
for the Session ID
    return str(secondsToLive)

Is there a better way to register the Route so that it can access the
current application, HttpRequest & HttpSession?

Any ideas would be very useful as I don't want to replicate this piece of
code with all of our Web Applications.

Thanks
Andy Bowes

Re: [flask] Accessing Request & Session from Blueprints

From:
gioi
Date:
2013-11-04 @ 15:56
You only need to write |from flask import request, session|, as you
would normally do.

You can check out
http://flask.pocoo.org/docs/quickstart/#accessing-request-data.

Re: [flask] Accessing Request & Session from Blueprints

From:
Andy Bowes
Date:
2013-11-04 @ 16:06
Thanks, got that bit :)

Is there a way to access the Flask App that the BluePrint has been
registered with?

We have registered a Redis Session Interface with our Flask App and need to
access this to get the Time to Live from the Redis Session object.  If I
import the flask.app then the app.Flask.session_interface property is
returning the default Secure Cookie implementation.



On Mon, Nov 4, 2013 at 3:56 PM, gioi <g@gioi.tk> wrote:

>  You only need to write from flask import request, session, as you would
> normally do.
>
> You can check out
> http://flask.pocoo.org/docs/quickstart/#accessing-request-data.
>

Re: [flask] Accessing Request & Session from Blueprints

From:
Emanuil Tolev
Date:
2013-11-04 @ 16:41
Brief summary: give your Blueprint access to your app object. The one you
create by calling app = Flask(__name__) or any other way to initialise a
Flask app.

* * *

So you want to access "your" app object. The one you've configured
(including registering your blueprint) and which is actually serving your
users.

I've seen my colleagues do this as follows.

Here's an excerpt of the setup of the app itself:

openarticlegauge  # "top" package directory
├── __init__.py
├── core.py
└── view
    ├── __init__.py
    └── query.py

# start of core.py excerpt
from flask import Flask

def create_app():
    app = Flask(__name__)
    # ... do some config
    return app

app = create_app()
# end of core.py excerpt


view/query.py defines a Blueprint called "query".

Now, view/query.py imports a couple of Flask modules:
from flask import Blueprint, request, abort, make_response

but not Flask.app, since (as you found out) that wouldn't give us *our* app
object.

view/query.py also imports something else:
from openarticlegauge.core import app

which gives us *our* app object, configured by us and modified elsewhere in
the code to register several Blueprints.

I've no idea whether this is good practice, Flask probably includes a
reference / shortcut of some sort to deal with this in a more elegant way.


Greetings,
Emanuil

P.S. For the sake of the mailing list archives and people stumbling upon
this thread while trying to solve some other problem, our code is here:
https://github.com/CottageLabs/OpenArticleGauge/tree/master/openarticlegauge

P.S.2. Thanks gioi, that's a better way.

On 4 November 2013 16:06, Andy Bowes <andy.bowes@gmail.com> wrote:

> Thanks, got that bit :)
>
> Is there a way to access the Flask App that the BluePrint has been
> registered with?
>
> We have registered a Redis Session Interface with our Flask App and need
> to access this to get the Time to Live from the Redis Session object.  If I
> import the flask.app then the app.Flask.session_interface property is
> returning the default Secure Cookie implementation.
>
>
>
> On Mon, Nov 4, 2013 at 3:56 PM, gioi <g@gioi.tk> wrote:
>
>>  You only need to write from flask import request, session, as you would
>> normally do.
>>
>> You can check out
>> http://flask.pocoo.org/docs/quickstart/#accessing-request-data.
>>
>
>

Re: [flask] Accessing Request & Session from Blueprints

From:
Andy Bowes
Date:
2013-11-04 @ 17:00
Thanks Guys

That's fantastic, I can now access my Redis Session.



On Mon, Nov 4, 2013 at 4:41 PM, Emanuil Tolev <emanuil@cottagelabs.com>wrote:

> Brief summary: give your Blueprint access to your app object. The one you
> create by calling app = Flask(__name__) or any other way to initialise a
> Flask app.
>
> * * *
>
> So you want to access "your" app object. The one you've configured
> (including registering your blueprint) and which is actually serving your
> users.
>
> I've seen my colleagues do this as follows.
>
> Here's an excerpt of the setup of the app itself:
>
> openarticlegauge  # "top" package directory
> ├── __init__.py
> ├── core.py
> └── view
>     ├── __init__.py
>     └── query.py
>
> # start of core.py excerpt
> from flask import Flask
>
> def create_app():
>     app = Flask(__name__)
>     # ... do some config
>     return app
>
> app = create_app()
> # end of core.py excerpt
>
>
> view/query.py defines a Blueprint called "query".
>
> Now, view/query.py imports a couple of Flask modules:
> from flask import Blueprint, request, abort, make_response
>
> but not Flask.app, since (as you found out) that wouldn't give us *our*
> app object.
>
> view/query.py also imports something else:
> from openarticlegauge.core import app
>
> which gives us *our* app object, configured by us and modified elsewhere
> in the code to register several Blueprints.
>
> I've no idea whether this is good practice, Flask probably includes a
> reference / shortcut of some sort to deal with this in a more elegant way.
>
>
> Greetings,
> Emanuil
>
> P.S. For the sake of the mailing list archives and people stumbling upon
> this thread while trying to solve some other problem, our code is here:
> https://github.com/CottageLabs/OpenArticleGauge/tree/master/openarticlegauge
>
> P.S.2. Thanks gioi, that's a better way.
>
> On 4 November 2013 16:06, Andy Bowes <andy.bowes@gmail.com> wrote:
>
>> Thanks, got that bit :)
>>
>> Is there a way to access the Flask App that the BluePrint has been
>> registered with?
>>
>> We have registered a Redis Session Interface with our Flask App and need
>> to access this to get the Time to Live from the Redis Session object.  If I
>> import the flask.app then the app.Flask.session_interface property is
>> returning the default Secure Cookie implementation.
>>
>>
>>
>> On Mon, Nov 4, 2013 at 3:56 PM, gioi <g@gioi.tk> wrote:
>>
>>>  You only need to write from flask import request, session, as you
>>> would normally do.
>>>
>>> You can check out
>>> http://flask.pocoo.org/docs/quickstart/#accessing-request-data.
>>>
>>
>>
>

Re: [flask] Accessing Request & Session from Blueprints

From:
gioi
Date:
2013-11-04 @ 16:34
Oh, sorry ;-)

|from flask import current_app|

Note: this is a proxy; you can access it only during request — view
functions included. The same applies to request and session.