在flask-security中扩展RegisterForm时出错 [英] Error extending RegisterForm in flask-security

查看:356
本文介绍了在flask-security中扩展RegisterForm时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经扩展了RegisterForm:



pre> class ExtendedRegisterForm(RegisterForm):
payroll_no = IntegerField('Payroll Number',validators = [DataRequired(message =(u'Please include a payroll number'))])
姓名= TextField的( '名字')
姓= TextField的( '姓')
位置= QuerySelectField(u'Position,query_factory =为getPosition,get_label = '名称',allow_blank = False)
mobile = TextField('Mobile',[InputRequired(),is_10])

我试图从视图(我已禁用SECURITY_REGISTERABLE)调用register_user()并直接到我自己的注册页面。

虽然它是验证罚款,我得到出现以下错误:

$ p $ TypeError:register_user()有一个意外的关键字参数'surname'

我的用户模型匹配我的表单:

  class User(db.Model,UserMixin): 
id = db.Column(db.Integer,primary_key = True)
email = db.Column(db.String(80),unique = True)
password = db.Column(db .String)
active = db.Column(db.Boolean())
confirmed_at = db.Column(db.DateTime())
firstname = db.Column(db.String(20 ))
surname = db.Column(db.String(30))
position_id = db.Column(db.Integer,db.ForeignKey('position.id'))
mobile = db.Column(db.String)
payroll_no = db.Column(db.Integer)



不幸的是,答案(我相当肯定)不会转移到上下文处理器或其他解决方案:我的项目有多个飞溅页面我需要一种方法来根据他们到注册页面的方式为该用户设置一个特定的角色; IE浏览器。发现网站A的用户需要角色A,发现网站B的用户需要角色B.解决方案非常简单。
我导入了所有必要的注册组件,然后调用下面的函数,将它传递给表单。
由于我有多个帐户(因为该应用程序充当多个站点的后端),所以我还将其传递给account_id整数,以便用户与正确的帐户关联。

 从项目导入应用程序,db,security,user_datastore 
from project.models从用户
导入用户
从flask.ext.security.utils .ext.security.confirmable进口generate_confirmation_link
导入do_flash,get_message,CONFIG_VALUE,send_mail,ENCRYPT_PASSWORD
从flask.ext.security.signals导入从烧瓶user_registered
import current_app

$ b def register_my_user(form,account_id):
form_data = form.to_dict()
form_data ['password'] = encrypt_password(form_data ['password '))
user = user_datastore.create_user(** form_data)
user.account_id = account_id
db.session.commit()
confirmation_link,token = generate_confirmation_link(user)
do_flash(* get_message( 'CONFIRM_REGISTRATION',电子邮件= user.email))
user_registered.send(current_app._get_current_object(),
用户=用户,confirm_token =令牌)
如果CONFIG_VALUE('SEND_REGIST ER_EMAIL '):
send_mail(CONFIG_VALUE(' EMAIL_SUBJECT_REGISTER'),user.email, '欢迎',
用户=用户,CONFIRMATION_LINK = CONFIRMATION_LINK)

粘贴在这里,希望对试图解构安全瓶用户注册组件的用户有所帮助

I am experiencing difficulty extending the registration aparatus of flask-security.

I have extended the RegisterForm:

class ExtendedRegisterForm(RegisterForm):
    payroll_no = IntegerField('Payroll Number', validators=[DataRequired(message=(u'Please include a payroll number'))])
    firstname = TextField('First Name')
    surname = TextField('Surname')
    position = QuerySelectField(u'Position', query_factory=getPosition, get_label='name', allow_blank=False)
    mobile = TextField('Mobile', [InputRequired(), is_10])

I am attempting to call register_user() from a view (I have disabled SECURITY_REGISTERABLE) and direct to my own registration page.

Whilst it is validating fine, I am getting the following error:

TypeError: "register_user() got an unexpected keyword argument 'surname'"

My user model matches my Form:

class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(80), unique=True)
    password = db.Column(db.String)
    active = db.Column(db.Boolean())
    confirmed_at = db.Column(db.DateTime())
    firstname = db.Column(db.String(20))
    surname = db.Column(db.String(30))
    position_id = db.Column(db.Integer, db.ForeignKey('position.id'))
    mobile = db.Column(db.String)
    payroll_no = db.Column(db.Integer)

Assistance most appreciated.

Unfortunately the answer (I am fairly sure) will not lie in moving to a context processor or other solution: My project has multiple 'splash' pages and I need a way to set a specific role to that user based on the way they got to the registration page; Ie. user who found site A needs Role A, user who found site B needs Role B.

解决方案

The solution was very simple. I imported all of the necessary registration components and called the following function, passing it the form. Since I have multiple accounts (as the app acts as a backend for multiple sites) I also pass it an account_id integer so the user becomes associated with the correct account.

from project import app, db, security, user_datastore
from project.models import User
from flask.ext.security.confirmable import generate_confirmation_link
from flask.ext.security.utils import do_flash, get_message, config_value, send_mail, encrypt_password
from flask.ext.security.signals import user_registered
from flask import current_app


def register_my_user(form, account_id):
    form_data = form.to_dict()
    form_data['password']=encrypt_password(form_data['password'])
    user = user_datastore.create_user(**form_data)
    user.account_id = account_id
    db.session.commit()
    confirmation_link, token = generate_confirmation_link(user)
    do_flash(*get_message('CONFIRM_REGISTRATION', email=user.email))
    user_registered.send(current_app._get_current_object(),
                     user=user, confirm_token=token)
    if config_value('SEND_REGISTER_EMAIL'):
        send_mail(config_value('EMAIL_SUBJECT_REGISTER'), user.email, 'welcome',
              user=user, confirmation_link=confirmation_link)  

Pasted here in the hope that it can be of use to people trying to deconstruct the user registration components of flask-security

这篇关于在flask-security中扩展RegisterForm时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆