Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 837
anvd Avatar asked Sep 23 '15 22:09

anvd


2 Answers

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.

like image 147
davidism Avatar answered Oct 09 '22 10:10

davidism


You overload is_active to implement your own logic.

What's wrong with it? Nothing IMO. It's correct except that you forgot to make it a property using @property decorator

In Tornado it's similar to current_user for example.

like image 38
DevLounge Avatar answered Oct 09 '22 11:10

DevLounge