librelist archives

« back to archive

Efficient usage of Flask-Principal

Efficient usage of Flask-Principal

From:
Juha Mustonen
Date:
2011-06-23 @ 09:16
Hi all,

With help of documentation I got the Flask-Principal extension working as 
expected, looking as follows:

----

@identity_loaded.connect_via(app)
def on_identity_loaded(sender, identity):
    '''
    - sender: app
    - identity: identity
    '''
    # Get the user information from the db
    account = models.Account.objects(id=identity.name).first()

    # Update the roles that a user can provide
    if account:
        for role in account.roles:
            identity.provides.add(RoleNeed(role))

        identity.provides.add(ItemNeed('account', 'view', account.id))
        identity.account = account


@app.route("/account/<int:aid>")
def show_account(aid):
    view_account_permission = Permission(ItemNeed('account', 'view', 
str(aid)))
    
    if view_account_permission.can():
        account = models.Account.get_by_id(aid)
        if not account:
            return flask.abort(404)

        return flask.render_template('user/show_account.html', account=account)

    return flask.make_response('Nogo')

----

Now the remaining question is how to store/load the identity information 
efficiently? The current implementation does re-construct the identity 
information for all of the incoming requests (including static files!) So, how 
have you done it? Database, memcache, both?

Br,
Juha


Re: [flask] Efficient usage of Flask-Principal

From:
Ali Afshar
Date:
2011-06-24 @ 07:25
https://bitbucket.org/s0undt3ch/flask-principal-main/changeset/525ca42f995c

Seems to solve the static file problem, at least.

On 23 June 2011 11:16, Juha Mustonen <juha.p.mustonen@gmail.com> wrote:
> Hi all,
>
> With help of documentation I got the Flask-Principal extension working as
> expected, looking as follows:
>
> ----
>
> @identity_loaded.connect_via(app)
> def on_identity_loaded(sender, identity):
>    '''
>    - sender: app
>    - identity: identity
>    '''
>    # Get the user information from the db
>    account = models.Account.objects(id=identity.name).first()
>
>    # Update the roles that a user can provide
>    if account:
>        for role in account.roles:
>            identity.provides.add(RoleNeed(role))
>
>        identity.provides.add(ItemNeed('account', 'view', account.id))
>        identity.account = account
>
>
> @app.route("/account/<int:aid>")
> def show_account(aid):
>    view_account_permission = Permission(ItemNeed('account', 'view',
> str(aid)))
>
>    if view_account_permission.can():
>        account = models.Account.get_by_id(aid)
>        if not account:
>            return flask.abort(404)
>
>        return flask.render_template('user/show_account.html', account=account)
>
>    return flask.make_response('Nogo')
>
> ----
>
> Now the remaining question is how to store/load the identity information
> efficiently? The current implementation does re-construct the identity
> information for all of the incoming requests (including static files!) So, how
> have you done it? Database, memcache, both?
>
> Br,
> Juha
>
>
>
>