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

anvd picture anvd · Sep 24, 2015 · Viewed 7.7k times · Source

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

Answer

davidism picture davidism · Sep 24, 2015

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.