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
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
Method 2
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0