Fix AttributeError: can't set attribute 'is_active' in User model
Flask-Login's UserMixin provides is_active as a read-only property. Attempting to set it as an instance attribute caused a conflict. Solution: - Store active status in private attribute _is_active - Override is_active property to return custom value - Update to_dict() to use _is_active This allows proper Flask-Login integration while maintaining custom active status tracking. Bug found during login testing: AttributeError when calling User.get_by_username() which triggered from_dict() → __init__(). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
+7
-2
@@ -25,12 +25,17 @@ class User(UserMixin):
|
||||
self.password_hash = password_hash
|
||||
self.role = role
|
||||
self.product_ids = product_ids or []
|
||||
self.is_active = is_active
|
||||
self._is_active = is_active
|
||||
|
||||
def get_id(self):
|
||||
"""Get user ID for Flask-Login"""
|
||||
return self.user_id
|
||||
|
||||
@property
|
||||
def is_active(self):
|
||||
"""Check if user account is active (Flask-Login property)"""
|
||||
return self._is_active
|
||||
|
||||
@property
|
||||
def is_authenticated(self):
|
||||
"""Check if user is authenticated"""
|
||||
@@ -78,7 +83,7 @@ class User(UserMixin):
|
||||
'password_hash': self.password_hash,
|
||||
'role': self.role,
|
||||
'product_ids': self.product_ids,
|
||||
'is_active': self.is_active
|
||||
'is_active': self._is_active
|
||||
}
|
||||
|
||||
@classmethod
|
||||
|
||||
Reference in New Issue
Block a user