什么在Flask-Security中运行activate_user()

时间:2014-11-13 19:19:00

标签: flask flask-sqlalchemy flask-security

我查看了Flask-Security的代码,无法弄清楚运行的是什么 datastore.activate_user()所以current_user.is_active返回True?

from flask.ext.security.utils import login_user

class LoginView(BaseView):
    methods = ['GET', 'POST']

    def dispatch_request(self):
        form = CustomLoginForm()
        if form.validate_on_submit():
            login_user(form.user, remember=True)
            flash("Logged in successfully.")
            return redirect(request.args.get('next') or url_for('main'))
        return render_template("login.html", form=form, **self.content)

我使用自定义LoginForm。表单已经过验证,但login_user返回False。

摘自main manage.py:

app = create_app()
manager = Manager(app)

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

以下是models.py

roles_users = db.Table(
    'roles_users',
    db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
    db.Column('role_id', db.Integer(), db.ForeignKey('role.id')))


class Role(RoleMixin, db.Model):
    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.String(80), unique=True, info={'verbose_name': 'Role name'})
    description = db.Column(db.String(255))

    def __repr__(self):
        return '<Role %r>' % (self.name)


class User(UserMixin, db.Model):
    id = Column(Integer, primary_key=True)
    name = Column(String(128), info={'verbose_name': 'User name', })
    password = Column(String(64))
    company = Column(String(64))
    location = Column(String(64))
    email = Column(String(64))
    active = Column(Boolean)
    roles = db.relationship('Role', secondary=roles_users, backref=db.backref('users', lazy='dynamic'))

    last_login_at = Column(DateTime)
    current_login_at = Column(DateTime)
    last_login_ip = Column(String(64))
    current_login_ip = Column(String(64))
    login_count = Column(Integer)

    def __repr__(self):
        return '<User %r>' % (self.name)

接受表单时应该自己运行datastore.activate_user()

0 个答案:

没有答案