fix: Add reconstructor to initialize transient fields

Introduces a reconstructor method to ensure that transient fields
such as 'role' and '_current_tenant' are initialized upon loading
an instance of the Account model. This change improves the
object's state consistency, especially when instances are
reloaded from the database, ensuring these fields are set
to 'None' by default.

Signed-off-by: -LAN- <laipz8200@outlook.com>
pull/19980/head
-LAN- 1 year ago
parent 1d4f4fcefd
commit d12de3d532
No known key found for this signature in database
GPG Key ID: 6BA0D108DED011FF

@ -4,7 +4,7 @@ from typing import Optional, cast
from flask_login import UserMixin # type: ignore from flask_login import UserMixin # type: ignore
from sqlalchemy import func from sqlalchemy import func
from sqlalchemy.orm import Mapped, mapped_column from sqlalchemy.orm import Mapped, mapped_column, reconstructor
from models.base import Base from models.base import Base
@ -81,8 +81,6 @@ class AccountStatus(enum.StrEnum):
class Account(UserMixin, Base): class Account(UserMixin, Base):
role: Optional["TenantAccountRole"] = None
_current_tenant: Optional["Tenant"] = None
__tablename__ = "accounts" __tablename__ = "accounts"
__table_args__ = (db.PrimaryKeyConstraint("id", name="account_pkey"), db.Index("account_email_idx", "email")) __table_args__ = (db.PrimaryKeyConstraint("id", name="account_pkey"), db.Index("account_email_idx", "email"))
@ -103,6 +101,11 @@ class Account(UserMixin, Base):
created_at = db.Column(db.DateTime, nullable=False, server_default=func.current_timestamp()) created_at = db.Column(db.DateTime, nullable=False, server_default=func.current_timestamp())
updated_at = db.Column(db.DateTime, nullable=False, server_default=func.current_timestamp()) updated_at = db.Column(db.DateTime, nullable=False, server_default=func.current_timestamp())
@reconstructor
def init_on_load(self):
self.role: Optional[TenantAccountRole] = None
self._current_tenant: Optional[Tenant] = None
@property @property
def is_password_set(self): def is_password_set(self):
return self.password is not None return self.password is not None

Loading…
Cancel
Save