Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
549 views
in Technique[技术] by (71.8m points)

python - Flask-Login raises TypeError: 'bool' object is not callable when trying to override is_active property

I want to modify is_active in Flask-Login so that users are not always active.

The default always returns True, but I changed it to return the value of the banned column.

Based on the docs, is_active should be a property. However, the internal Flask-Login code raises:

TypeError: 'bool' object is not callable 

When trying to use is_active.

How do I correctly use is_active to deactivate some users?

class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    banned = db.Column(db.Boolean, default=False)

    @property
    def is_active(self):
        return self.banned

login_user(user, form.remember_me.data)

if not force and not user.is_active():
TypeError: 'bool' object is not callable
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

is_active, is_anonymous, and is_authenticated are all properties as of Flask-Login 0.3. If you want to use them, treat them as attributes, don't call them. If you want to override them, remember to decorate them with @property.

# change from
current_user.is_authenticated()
# to
current_user.is_authenticated

It appears you are reading the docs for the most recent version (0.3), but using an older version of the library. Version 0.3 contains a breaking change which changed these attributes from methods to properties. You should upgrade to the latest version of Flask-Login and treat them as properties.

You deactivate the user by causing its is_active property to return False. Your idea to return the value of a column is fine.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...