librelist archives

« back to archive

Help with Flask-Security

Help with Flask-Security

From:
Matt Shields
Date:
2015-01-30 @ 16:50
I'm getting the following error when I try to implement Flask-Security.
Below is the error and I've uploaded my code to here:
https://github.com/mattboston/flask-security-test  I would appreciate
anyone who can lend a hand.  Thanks

Traceback (most recent call last):
  File "runserver.py", line 1, in <module>
    from dashboard import app
  File "/var/www/dashboard-dev/dashboard/__init__.py", line 10, in <module>
    from models import db
  File "/var/www/dashboard-dev/dashboard/models.py", line 36, in <module>
    security = Security(app, user_datastore)
NameError: name 'app' is not defined


Matt

Re: [flask] Help with Flask-Security

From:
Scott Werner
Date:
2015-01-30 @ 17:06
app is not defined in models.py, trying the following:
from flask import current_app
…
                security = Security(current_app, user_datastore)

However, I recommend initializing all flask plugins/addons together:
from flask import Flask

from flask_mail import Mail
from flask_security import SQLAlchemyUserDatastore
from flask_sqlalchemy import SQLAlchemy

from models import User, Role

db = SQLAlchemy()
mail = Mail()
security = Security()

app = Flask()
db.init_app(app)
mail.init_app(app)
security.init_app(app, SQLAlchemyUserDatastore(db, User, Role))

I really like the app structure presented here 
https://github.com/mattupstate/overholt. You can see db, mail, security 
are loaded in core.py and factory.py initializes them.

From: flask@librelist.com [mailto:flask@librelist.com] On Behalf Of Matt Shields
Sent: Friday, January 30, 2015 11:51 AM
To: flask@librelist.com
Subject: [flask] Help with Flask-Security

I'm getting the following error when I try to implement Flask-Security.  
Below is the error and I've uploaded my code to here: 
https://github.com/mattboston/flask-security-test  I would appreciate 
anyone who can lend a hand.  Thanks

Traceback (most recent call last):
  File "runserver.py", line 1, in <module>
    from dashboard import app
  File "/var/www/dashboard-dev/dashboard/__init__.py", line 10, in <module>
    from models import db
  File "/var/www/dashboard-dev/dashboard/models.py", line 36, in <module>
    security = Security(app, user_datastore)
NameError: name 'app' is not defined


Matt

Re: [flask] Help with Flask-Security

From:
Matt Shields
Date:
2015-01-30 @ 17:24
The first recommendation didn't work, see error below.  Unfortunately,
changing to the second suggestion would require a lot of work.  There's a
lot I cut out of the Git repo.  This is an existing app that uses MySQLdb
so I've already had to throw Flask-SQLAlchemy in there for the security
part since there didn't seem to be an easy way to implement without it.
I'm trying to very quickly with very few changes add the ability to add
login/logout with users and roles to limit what our employees can do on
this site.  I'd really rather not rewrite the entire site to have it.

Is there an easier way to implement user/roles than Flask-Security?


Traceback (most recent call last):
  File "runserver.py", line 1, in <module>
    from dashboard import app
  File "/var/www/dashboard-dev/dashboard/__init__.py", line 10, in <module>
    from models import db
  File "/var/www/dashboard-dev/dashboard/models.py", line 37, in <module>
    security = Security(current_app, user_datastore)
  File "/usr/lib/python2.6/site-packages/flask_security/core.py", line 395,
in __init__
    self._state = self.init_app(app, datastore, **kwargs)
  File "/usr/lib/python2.6/site-packages/flask_security/core.py", line 412,
in init_app
    app.config.setdefault('SECURITY_' + key, value)
  File "/usr/lib/python2.6/site-packages/werkzeug/local.py", line 338, in
__getattr__
    return getattr(self._get_current_object(), name)
  File "/usr/lib/python2.6/site-packages/werkzeug/local.py", line 297, in
_get_current_object
    return self.__local()
  File "/usr/lib/python2.6/site-packages/flask/globals.py", line 34, in
_find_app
    raise RuntimeError('working outside of application context')
RuntimeError: working outside of application context

Matt

On Fri, Jan 30, 2015 at 12:06 PM, Scott Werner <scott.werner@autodesk.com>
wrote:

>  app is not defined in models.py, trying the following:
>
> from flask import current_app
>
> …
>
>                 security = Security(current_app, user_datastore)
>
>
>
> However, I recommend initializing all flask plugins/addons together:
>
> from flask import Flask
>
>
>
> from flask_mail import Mail
>
> from flask_security import SQLAlchemyUserDatastore
>
> from flask_sqlalchemy import SQLAlchemy
>
>
>
> from models import User, Role
>
>
>
> db = SQLAlchemy()
>
> mail = Mail()
>
> security = Security()
>
>
>
> app = Flask()
>
> db.init_app(app)
>
> mail.init_app(app)
>
> security.init_app(app, SQLAlchemyUserDatastore(db, User, Role))
>
>
>
> I really like the app structure presented here
> https://github.com/mattupstate/overholt. You can see db, mail, security
> are loaded in core.py and factory.py initializes them.
>
>
>
> *From:* flask@librelist.com [mailto:flask@librelist.com] *On Behalf Of *Matt
> Shields
> *Sent:* Friday, January 30, 2015 11:51 AM
> *To:* flask@librelist.com
> *Subject:* [flask] Help with Flask-Security
>
>
>
> I'm getting the following error when I try to implement Flask-Security.
> Below is the error and I've uploaded my code to here:
> https://github.com/mattboston/flask-security-test  I would appreciate
> anyone who can lend a hand.  Thanks
>
>
>
> Traceback (most recent call last):
>
>   File "runserver.py", line 1, in <module>
>
>     from dashboard import app
>
>   File "/var/www/dashboard-dev/dashboard/__init__.py", line 10, in <module>
>
>     from models import db
>
>   File "/var/www/dashboard-dev/dashboard/models.py", line 36, in <module>
>
>     security = Security(app, user_datastore)
>
> NameError: name 'app' is not defined
>
>
>
>
>
> Matt
>

Re: [flask] Help with Flask-Security

From:
Joshua J. Kugler
Date:
2015-02-03 @ 05:11
You could use Flask-Login and Flask-Principal (I think it is) on their own 
instead of throwing in the totality of Flask-Security.

See https://pythonhosted.org/Flask-Security/ for the list of modules it uses.

j

On Friday, January 30, 2015 12:24:25 Matt Shields wrote:
> The first recommendation didn't work, see error below.  Unfortunately,
> changing to the second suggestion would require a lot of work.  There's a
> lot I cut out of the Git repo.  This is an existing app that uses MySQLdb
> so I've already had to throw Flask-SQLAlchemy in there for the security
> part since there didn't seem to be an easy way to implement without it.
> I'm trying to very quickly with very few changes add the ability to add
> login/logout with users and roles to limit what our employees can do on
> this site.  I'd really rather not rewrite the entire site to have it.
> 
> Is there an easier way to implement user/roles than Flask-Security?
> 
> 
> Traceback (most recent call last):
>   File "runserver.py", line 1, in <module>
>     from dashboard import app
>   File "/var/www/dashboard-dev/dashboard/__init__.py", line 10, in <module>
>     from models import db
>   File "/var/www/dashboard-dev/dashboard/models.py", line 37, in <module>
>     security = Security(current_app, user_datastore)
>   File "/usr/lib/python2.6/site-packages/flask_security/core.py", line 395,
> in __init__
>     self._state = self.init_app(app, datastore, **kwargs)
>   File "/usr/lib/python2.6/site-packages/flask_security/core.py", line 412,
> in init_app
>     app.config.setdefault('SECURITY_' + key, value)
>   File "/usr/lib/python2.6/site-packages/werkzeug/local.py", line 338, in
> __getattr__
>     return getattr(self._get_current_object(), name)
>   File "/usr/lib/python2.6/site-packages/werkzeug/local.py", line 297, in
> _get_current_object
>     return self.__local()
>   File "/usr/lib/python2.6/site-packages/flask/globals.py", line 34, in
> _find_app
>     raise RuntimeError('working outside of application context')
> RuntimeError: working outside of application context
> 
> Matt
> 
> On Fri, Jan 30, 2015 at 12:06 PM, Scott Werner <scott.werner@autodesk.com>
> 
> wrote:
> >  app is not defined in models.py, trying the following:
> > from flask import current_app
> > 
> > …
> > 
> >                 security = Security(current_app, user_datastore)
> > 
> > However, I recommend initializing all flask plugins/addons together:
> > 
> > from flask import Flask
> > 
> > 
> > 
> > from flask_mail import Mail
> > 
> > from flask_security import SQLAlchemyUserDatastore
> > 
> > from flask_sqlalchemy import SQLAlchemy
> > 
> > 
> > 
> > from models import User, Role
> > 
> > 
> > 
> > db = SQLAlchemy()
> > 
> > mail = Mail()
> > 
> > security = Security()
> > 
> > 
> > 
> > app = Flask()
> > 
> > db.init_app(app)
> > 
> > mail.init_app(app)
> > 
> > security.init_app(app, SQLAlchemyUserDatastore(db, User, Role))
> > 
> > 
> > 
> > I really like the app structure presented here
> > https://github.com/mattupstate/overholt. You can see db, mail, security
> > are loaded in core.py and factory.py initializes them.
> > 
> > 
> > 
> > *From:* flask@librelist.com [mailto:flask@librelist.com] *On Behalf Of
> > *Matt Shields
> > *Sent:* Friday, January 30, 2015 11:51 AM
> > *To:* flask@librelist.com
> > *Subject:* [flask] Help with Flask-Security
> > 
> > 
> > 
> > I'm getting the following error when I try to implement Flask-Security.
> > Below is the error and I've uploaded my code to here:
> > https://github.com/mattboston/flask-security-test  I would appreciate
> > anyone who can lend a hand.  Thanks
> > 
> > Traceback (most recent call last):
> >   File "runserver.py", line 1, in <module>
> >   
> >     from dashboard import app
> >   
> >   File "/var/www/dashboard-dev/dashboard/__init__.py", line 10, in
> >   <module>
> >   
> >     from models import db
> >   
> >   File "/var/www/dashboard-dev/dashboard/models.py", line 36, in <module>
> >   
> >     security = Security(app, user_datastore)
> > 
> > NameError: name 'app' is not defined
> > 
> > 
> > 
> > 
> > 
> > Matt

-- 
Joshua J. Kugler - Fairbanks, Alaska
Azariah Enterprises - Programming and Website Design
joshua@azariah.com - Jabber: pedahzur@gmail.com
PGP Key: http://pgp.mit.edu/  ID 0x73B13B6A