Flask-Security user_registered Signal Not Received in Python 3.3, but works in 2.7 -
i'm trying use user_registered signal in order set default roles users when register using flask-security in following link: setting default role in flask security
in searches can see there bug addressed in flask-security: not getting signal flask-security, fix - user_registered signal problem
i've tried following prove if signal received handler without luck:
@user_registered.connect_via(app) def user_registered_sighandler(sender, **extra): print("print-user_registered_sighandler:", extra)
this, however, never gets called though user gets registered , signal should sent.
if helps i've set flask-security configuration follows:
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
signals flask-login , flask-principal working me managed confirm following code snippets print when signals sent:
@user_logged_out.connect_via(app) def on_user_logged_out(sender, user): print('user log out: made in',user) @identity_changed.connect_via(app) def identity_changed_ok(sender,identity): print('identity changed:',identity)
for setup using python 3.3 (anaconda) , using following: flask==0.10.1,flask-login==0.2.11,flask-principal==0.4.0,flask-security==1.7.4,blinker==1.3. having looked @ signals in both flask-login , flask-security i'm not sure why flask-security signals not working.
edit:
if add print(user_registered.receivers)
route in app show have receiver: {139923381372400: <function user_registered_sighandler @ 0x7f42737145f0>}
. if put same print statement within registerable.py of flask-security before user_registered.send(app._get_current_object(),user=user, confirm_token=token)
lists no receivers: {}
edit2:
problem appears related using python 3.3. created python 2.7 environment , user_registered code worked expected.
full code reproduce:
from flask import flask,render_template playhouse.flask_utils import flaskdb import os flask.ext.security import security, peeweeuserdatastore flask.ext.security.signals import user_registered flask.ext.login import user_logged_out peewee import * playhouse.signals import model flask.ext.security import usermixin,rolemixin app = flask(__name__) app.config['admin_password']='secret' app.config['app_dir']=os.path.dirname(os.path.realpath(__file__)) app.config['database']='sqliteext:///%s' % os.path.join(app.config['app_dir'], 'blog.db') app.config['secret_key'] = 'shhh, secret!' app.config['security_registerable'] = true app.config['security_confirmable'] = false app.config['security_send_register_email'] = false flask_db = flaskdb() flask_db.init_app(app) database = flask_db.database class basemodel(model): class meta: database=flask_db.database class user(basemodel, usermixin): email=charfield() password=charfield() active = booleanfield(default=true) confirmed_at = datetimefield(null=true) def is_active(self): return true def is_anonymous(self): return false def is_authenticated(self): return true class role(basemodel, rolemixin): name = charfield(unique=true) description = textfield(null=true) class userroles(basemodel): user = foreignkeyfield(user, related_name='roles') role = foreignkeyfield(role, related_name='users') name = property(lambda self: self.role.name) description = property(lambda self: self.role.description) user_datastore = peeweeuserdatastore(database, user, role, userroles) security = security(app, user_datastore) @user_registered.connect_via(app) def user_registered_sighandler(sender,**extra): print("print-user_registered_sighandler") @user_logged_out.connect_via(app) def on_user_logged_out(sender, user): print('user log out: made in',user) @app.route('/') def index(): print(user_registered.receivers) return render_template('base.html') database.create_tables([user,role,userroles], safe=true) app.run(debug=true)
base.html template:
<!doctype html> <html> <head> <title>blog</title> </head> <body> <ul> {% if current_user.is_authenticated() %} <li><a href="{{ url_for('security.logout',next='/') }}">log out</a></li> <li><a href="{{ url_for('security.register') }}">register</a></li> {% else %} <li><a href="{{ url_for('security.login',next='/') }}">login</a></li> <li><a href="{{ url_for('security.register') }}">register</a></li> {% endif %} {% block extra_header %}{% endblock %} </ul> </body> </html>
i able like:
security = security(app, user_datastore, register_form=extendedregisterform) @user_registered.connect_via(app) def user_registered_sighandler(app, user, confirm_token): default_role = user_datastore.find_role("pending") user_datastore.add_role_to_user(user, default_role) db.session.commit()
Comments
Post a Comment