refactor(models): Use the SQLAlchemy base model. (#19435)
Signed-off-by: -LAN- <laipz8200@outlook.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import enum
|
||||
import json
|
||||
from typing import cast
|
||||
|
||||
from flask_login import UserMixin # type: ignore
|
||||
from sqlalchemy import func
|
||||
@@ -46,7 +47,6 @@ class Account(UserMixin, Base):
|
||||
|
||||
@property
|
||||
def current_tenant(self):
|
||||
# FIXME: fix the type error later, because the type is important maybe cause some bugs
|
||||
return self._current_tenant # type: ignore
|
||||
|
||||
@current_tenant.setter
|
||||
@@ -64,25 +64,23 @@ class Account(UserMixin, Base):
|
||||
def current_tenant_id(self) -> str | None:
|
||||
return self._current_tenant.id if self._current_tenant else None
|
||||
|
||||
@current_tenant_id.setter
|
||||
def current_tenant_id(self, value: str):
|
||||
try:
|
||||
tenant_account_join = (
|
||||
def set_tenant_id(self, tenant_id: str):
|
||||
tenant_account_join = cast(
|
||||
tuple[Tenant, TenantAccountJoin],
|
||||
(
|
||||
db.session.query(Tenant, TenantAccountJoin)
|
||||
.filter(Tenant.id == value)
|
||||
.filter(Tenant.id == tenant_id)
|
||||
.filter(TenantAccountJoin.tenant_id == Tenant.id)
|
||||
.filter(TenantAccountJoin.account_id == self.id)
|
||||
.one_or_none()
|
||||
)
|
||||
),
|
||||
)
|
||||
|
||||
if tenant_account_join:
|
||||
tenant, ta = tenant_account_join
|
||||
tenant.current_role = ta.role
|
||||
else:
|
||||
tenant = None
|
||||
except Exception:
|
||||
tenant = None
|
||||
if not tenant_account_join:
|
||||
return
|
||||
|
||||
tenant, join = tenant_account_join
|
||||
tenant.current_role = join.role
|
||||
self._current_tenant = tenant
|
||||
|
||||
@property
|
||||
@@ -191,7 +189,7 @@ class TenantAccountRole(enum.StrEnum):
|
||||
}
|
||||
|
||||
|
||||
class Tenant(db.Model): # type: ignore[name-defined]
|
||||
class Tenant(Base):
|
||||
__tablename__ = "tenants"
|
||||
__table_args__ = (db.PrimaryKeyConstraint("id", name="tenant_pkey"),)
|
||||
|
||||
@@ -220,7 +218,7 @@ class Tenant(db.Model): # type: ignore[name-defined]
|
||||
self.custom_config = json.dumps(value)
|
||||
|
||||
|
||||
class TenantAccountJoin(db.Model): # type: ignore[name-defined]
|
||||
class TenantAccountJoin(Base):
|
||||
__tablename__ = "tenant_account_joins"
|
||||
__table_args__ = (
|
||||
db.PrimaryKeyConstraint("id", name="tenant_account_join_pkey"),
|
||||
@@ -239,7 +237,7 @@ class TenantAccountJoin(db.Model): # type: ignore[name-defined]
|
||||
updated_at = db.Column(db.DateTime, nullable=False, server_default=func.current_timestamp())
|
||||
|
||||
|
||||
class AccountIntegrate(db.Model): # type: ignore[name-defined]
|
||||
class AccountIntegrate(Base):
|
||||
__tablename__ = "account_integrates"
|
||||
__table_args__ = (
|
||||
db.PrimaryKeyConstraint("id", name="account_integrate_pkey"),
|
||||
@@ -256,7 +254,7 @@ class AccountIntegrate(db.Model): # type: ignore[name-defined]
|
||||
updated_at = db.Column(db.DateTime, nullable=False, server_default=func.current_timestamp())
|
||||
|
||||
|
||||
class InvitationCode(db.Model): # type: ignore[name-defined]
|
||||
class InvitationCode(Base):
|
||||
__tablename__ = "invitation_codes"
|
||||
__table_args__ = (
|
||||
db.PrimaryKeyConstraint("id", name="invitation_code_pkey"),
|
||||
|
Reference in New Issue
Block a user