[rewrite] Minor fixes to models, session generator and example .env

This commit is contained in:
Thastertyn 2025-03-10 22:02:30 +01:00
parent 71e916586e
commit 272e765ca8
4 changed files with 16 additions and 10 deletions

View File

@ -30,8 +30,10 @@ SMTP_HOST=
SMTP_PORT=587 SMTP_PORT=587
SMTP_USER= SMTP_USER=
SMTP_PASSWORD= SMTP_PASSWORD=
SMTP_TLS=True # Use TLS for email security # Use TLS for email security
SMTP_SSL=False # Set to True if using SSL instead of TLS SMTP_TLS=True
# Set to True if using SSL instead of TLS
SMTP_SSL=False
# Email sender information # Email sender information
EMAILS_FROM_EMAIL= EMAILS_FROM_EMAIL=

View File

@ -1,8 +1,12 @@
import logging
from sqlmodel import select from sqlmodel import select
from fastapi import APIRouter from fastapi import APIRouter
from app.api.dependencies import SessionDep from app.api.dependencies import SessionDep
logger = logging.getLogger(__name__)
router = APIRouter(prefix="/utils", tags=["utils"]) router = APIRouter(prefix="/utils", tags=["utils"])
@ -10,11 +14,12 @@ router = APIRouter(prefix="/utils", tags=["utils"])
async def health_check() -> bool: async def health_check() -> bool:
return True return True
@router.get("/test-db/") @router.get("/test-db/")
async def test_db(session: SessionDep) -> bool: async def test_db(session: SessionDep) -> bool:
try: try:
with session: session.exec(select(1))
session.exec(select(1)) return True
return True except Exception as e:
except Exception: logger.error(e)
return False return False

View File

@ -9,7 +9,7 @@ from app.core.config import settings
from app.database.exceptions import DatabaseError from app.database.exceptions import DatabaseError
import app.database.models import app.database.models # pylint: disable=unused-import
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -34,7 +34,6 @@ def cleanup() -> None:
engine.dispose() engine.dispose()
@contextmanager
def get_session() -> Generator[Session, None, None]: def get_session() -> Generator[Session, None, None]:
with Session(engine) as session: with Session(engine) as session:
yield session yield session

View File

@ -26,7 +26,7 @@ class User(SQLModel, table=True):
owned_shops: List["Shop"] = Relationship(back_populates="owner") owned_shops: List["Shop"] = Relationship(back_populates="owner")
registered_shop: List["Shop"] = Relationship(back_populates="registered_users") registered_shop: List["Shop"] = Relationship(back_populates="registered_users")
role: Optional["UserRole"] = Relationship(back_populates="users") user_assigned_role: Optional["UserRole"] = Relationship(back_populates="role_users", sa_relationship_kwargs={"foreign_keys": "[User.user_role_id]"})
preferences: Optional["UserPreferences"] = Relationship(back_populates="user") preferences: Optional["UserPreferences"] = Relationship(back_populates="user")
statistics: Optional["UserStatistics"] = Relationship(back_populates="user_statistics") statistics: Optional["UserStatistics"] = Relationship(back_populates="user_statistics")
@ -45,7 +45,7 @@ class UserRole(SQLModel, table=True):
id: int = Field(primary_key=True) id: int = Field(primary_key=True)
name: str = Field(nullable=False, unique=True, max_length=50) name: str = Field(nullable=False, unique=True, max_length=50)
users = Relationship(back_populates="role") role_users: List["User"] = Relationship(back_populates="user_assigned_role")
class UserStatistics(SQLModel, table=True): class UserStatistics(SQLModel, table=True):