11

我正在为电子商务网站编写注册/登录系统,并使用 flask-security ( http://pythonhosted.org/Flask-Security/ ) 来处理注册功能。部分基本设置需要以下 signup.py 模块:

from flask.ext.security import SQLAlchemyUserDatastore, Security
from app.models import User, Role
from app import app, db

# Setup Flask Security
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(app, user_datastore)

然后我必须将 user_datastore 和安全对象导入到我的 views.py 模块中,如下所示:

from app.signup import user_datastore, security

问题是,一旦我将上述导入语句包含到我的视图模块中,我的整个应用程序就会崩溃,并且当我尝试运行我的单元或行为测试时出现以下回溯错误(为便于阅读而编辑)

======================================================================
ERROR: Failure: AttributeError ('_FakeSignal' object has no attribute 'connect_via')
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/nose/loader.py", line 413, in loadTestsFromName
    addr.filename, addr.module)
  File "/Library/Python/2.7/site-packages/nose/importer.py", line 47, in importFromPath
    return self.importFromDir(dir_path, fqname)
  File "/Library/Python/2.7/site-packages/nose/importer.py", line 94, in importFromDir
    mod = load_module(part_fqname, fh, filename, desc)
  File "/Users/faiyamrahman/programming/Python/WebApps/NibsNWhiskeyFull/tests/test_database.py", line 6, in <module>
    from app import app, db, models
  File "/Users/faiyamrahman/programming/Python/WebApps/NibsNWhiskeyFull/app/__init__.py", line 9, in <module>
    from app import views, models
  File "/Users/faiyamrahman/programming/Python/WebApps/NibsNWhiskeyFull/app/views.py", line 7, in <module>
    from app.signup import user_datastore
  File "/Users/faiyamrahman/programming/Python/WebApps/NibsNWhiskeyFull/app/signup.py", line 7, in <module>
    security = Security(app, user_datastore)
  File "/Users/faiyamrahman/programming/Python/WebApps/NibsNWhiskeyFull/flask/lib/python2.7/site-packages/flask_security/core.py", line 346, in __init__
    self._state = self.init_app(app, datastore, **kwargs)
  File "/Users/faiyamrahman/programming/Python/WebApps/NibsNWhiskeyFull/flask/lib/python2.7/site-packages/flask_security/core.py", line 368, in init_app
    identity_loaded.connect_via(app)(_on_identity_loaded)
AttributeError: '_FakeSignal' object has no attribute 'connect_via'

我不知道这是什么意思。我试过阅读烧瓶安全文档,但我不明白为什么会这样。感谢任何对此进行尝试的人!

4

2 回答 2

16

简短回答:您缺少闪光灯库。 编辑:您确认您的虚拟环境找不到闪烁器并重新安装它。

长答案:

我认为错误来自Flask Signals。从信号看这段代码:

signals_available = False
try:
    from blinker import Namespace
    signals_available = True
except ImportError:
    class Namespace(object):
        def signal(self, name, doc=None):
            return _FakeSignal(name, doc)

所以我认为代码试图找到blinker库,在你的情况下,它无法导入它,因此它试图使用_FakeSignal该类。

该类_FakeSignal没有connect_via定义属性,如下所示

class _FakeSignal(object):
    """If blinker is unavailable, create a fake class with the same
    interface that allows sending of signals but will fail with an
    error on anything else.  Instead of doing anything on send, it
    will just ignore the arguments and do nothing instead.
    """

    def __init__(self, name, doc=None):
        self.name = name
        self.__doc__ = doc
    def _fail(self, *args, **kwargs):
        raise RuntimeError('signalling support is unavailable '
                           'because the blinker library is '
                           'not installed.')
    send = lambda *a, **kw: None
    connect = disconnect = has_receivers_for = receivers_for = \
        temporarily_connected_to = connected_to = _fail
    del _fail

connect_viaFlask-Security 试图加载的属性实际上是由blinker库提供的,因为没有闪烁,所以没有 connect_via。因此它失败了。

所以你应该先安装blinker。但是,我认为 Flask-Security 代码也应该blinker在尝试使用connect_via.

于 2013-12-04T17:59:28.187 回答
0

致未来的互联网人:

我有同样的错误,但原因不同。我正在使用 anaconda 的虚拟环境,我认为这与烧瓶的自动重新加载系统冲突(它无法正确加载一次并且修复错误并没有改变任何东西,甚至编辑 blinker.py 文件)。打开一个新的控制台运行修复它。

抱歉,我没有更多信息可提供,但希望对某人有所帮助。

于 2019-06-05T18:39:53.757 回答