我正在尝试使用user_registered信号,以便在用户使用flask-security注册时为用户设置默认角色,如以下链接所示:在Flask Security中设置默认角色
在我的搜索中,我可以看到在烧瓶安全性中已经解决了这个问题:没有从烧瓶安全性,Fix - user_registered信号问题获得信号
我已尝试以下方法来证明处理程序是否收到信号而没有任何运气:
@user_registered.connect_via(app)
def user_registered_sighandler(sender, **extra):
print("print-user_registered_sighandler:", extra)
Run Code Online (Sandbox Code Playgroud)
然而,即使用户注册并且应该发送信号,也不会被调用.
如果它有助于我设置flask-security配置如下:
app.config['SECURITY_REGISTERABLE'] = True
app.config['SECURITY_CONFIRMABLE'] = False
app.config['SECURITY_SEND_REGISTER_EMAIL'] = False
app.config['SECURITY_CHANGEABLE'] = True
app.config['SECURITY_SEND_PASSWORD_CHANGE_EMAIL'] = False
Run Code Online (Sandbox Code Playgroud)
来自Flask-Login和Flask-Principal的信号正在为我工作,因为我设法确认以下代码片段在发送信号时成功打印:
@user_logged_out.connect_via(app)
def on_user_logged_out(sender, user):
print('USER LOG OUT: made it in',user)
@identity_changed.connect_via(app)
def identity_changed_ok(sender,identity):
print('Identity changed:',identity)
Run Code Online (Sandbox Code Playgroud)
对于我的设置,我使用python 3.3(anaconda)并使用以下内容:Flask == 0.10.1,flask-login == 0.2.11,flask-principal == 0.4.0,flask-security == 1.7.4,信号灯== 1.3.看了瓶子登录和烧瓶安全的信号,我不知道为什么烧瓶安全信号不起作用.
编辑:
如果我print(user_registered.receivers)在我的应用程序中添加路线,它将显示我有一个接收器:{139923381372400: <function user_registered_sighandler at 0x7f42737145f0>}.如果我把这个相同的print语句放在flask-security的registerable.py之前,user_registered.send(app._get_current_object(),user=user, confirm_token=token)那么它就没有列出接收器:{}
EDIT2:
问题似乎与使用python 3.3有关.我创建了一个python 2.7环境,user_registered代码按预期工作.
完整代码重现:
from …Run Code Online (Sandbox Code Playgroud) 我正在为电子商务网站编写注册/登录系统,并使用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)
Run Code Online (Sandbox Code Playgroud)
然后我必须将user_datastore和安全对象导入到views.py模块中,如下所示:
from app.signup import user_datastore, security
Run Code Online (Sandbox Code Playgroud)
问题是,只要我将上面的import语句包含到我的视图模块中,我的整个应用程序崩溃了,当我尝试运行我的单元或行为测试时,我得到以下回溯错误(为了便于阅读而编辑)
======================================================================
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, …Run Code Online (Sandbox Code Playgroud) Flask文件说:
另请注意,信号旨在通知订阅者,不应鼓励订阅者修改数据
我在想,为什么会这样?
我正在使用Flask-User库,我想在用户注册时为用户设置一些默认字段(例如,将displayname设置为等于username),然后更新db.Flask-User user_registered在用户注册时发送信号.为什么订阅信号并在其中更新db是个坏主意?
我想知道我是否可以使用闪烁库(或者可能使用任何库)来完成这些工作。
app.py),我定义了一个名为updated(例如blinker.signal('updated'))的信号。subscriber)连接(订阅)到更新后的信号。这个过程就像守护进程一样永远运行。subscriber调用该函数。所以我写了一些代码:
app.py(烧瓶应用程序)
from flask import Flask
from blinker import signal
app = Flask(__name__)
updated = signal('updated')
@app.route('/update')
def update():
updated.send('nothing')
return 'Updated!'
Run Code Online (Sandbox Code Playgroud)
背景.py
import time
from app import updated
@updated.connect
def subscriber(*args, **kwargs):
print('An update occurred on the web side!')
while True:
print('Waiting for signals...')
time.sleep(1)
Run Code Online (Sandbox Code Playgroud)
并使用flask run命令运行 Web 应用程序。现在,当我访问时localhost:5000/update,我可以Updated!在浏览器中看到消息,但看不到 …