-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
249e90b
commit ade87cc
Showing
15 changed files
with
422 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from src.auth.models import Group, Permission, Role, RolePermission, User # noqa: F401 | ||
from src.db.base import Base | ||
|
||
|
||
def orm_by_table_name(table_name: str) -> type[Base] | None: | ||
for m in Base.registry.mappers: | ||
if getattr(m.class_, "__tablename__", None) == table_name: | ||
return m.class_ | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,19 @@ | ||
from typing import ClassVar | ||
from typing import Any, ClassVar | ||
from uuid import UUID | ||
|
||
from sqlalchemy import ForeignKey | ||
from sqlalchemy.orm import DeclarativeBase, Mapped, declared_attr, mapped_column | ||
from fastapi.encoders import jsonable_encoder | ||
from sqlalchemy.orm import DeclarativeBase | ||
|
||
from src.context import tenant_ctx | ||
from src.db._types import GUID | ||
|
||
|
||
class Base(DeclarativeBase): | ||
__multi_tenant__: bool = True | ||
__visible_name__: ClassVar = {} | ||
__search_fields__: ClassVar = set() | ||
type_annotation_map: ClassVar = {UUID: GUID} | ||
|
||
@declared_attr | ||
@classmethod | ||
def tenant_id(cls) -> Mapped[UUID]: | ||
if not cls.__multi_tenant__: | ||
return None | ||
return mapped_column(UUID, ForeignKey("tenant.id", ondelete="CASCADE"), index=True, default=tenant_ctx.get) | ||
def dict(self, exclude: set[str] | None = None, native_dict: bool = False) -> dict[str, Any]: | ||
"""Return dict representation of model.""" | ||
if not native_dict: | ||
return jsonable_encoder(self, exclude=exclude) | ||
return {c.name: getattr(self, c.name) for c in self.__table__.columns if c.name not in exclude} |
Oops, something went wrong.