3d3c2bcc57
Collapse the three data layers into one. app/db.py now exposes a single Base, a cached engine bound to app_database_url with SQLite WAL enabled, and get_engine/get_session_local/reset_db_caches/get_db_session. Delete app/auth_db.py, app/poo_db.py and app/models/base.py. All models (auth, config, public_ip, location, poo) inherit the one Base and register on a single metadata. Dependencies converge to a single get_db; all routes use it. Also update the alembic env.py files (app/location/poo) and tests that imported the removed modules so the suite stays green, and drop the obsolete test_legacy_style_location_db test whose flow (app reading a separate location DB) no longer exists. Location/poo Alembic chains, adopt scripts and adoption tests remain for M1-T04; config fields remain for M1-T05. pytest 109 passed; ruff clean (pre-existing only); WAL verified; single Base.metadata holds all seven tables.
79 lines
2.8 KiB
Python
79 lines
2.8 KiB
Python
import logging
|
|
|
|
from fastapi import APIRouter, Depends, Request, status
|
|
from fastapi.responses import PlainTextResponse, RedirectResponse, Response
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.config import Settings
|
|
from app.dependencies import (
|
|
get_app_settings,
|
|
get_db,
|
|
get_current_auth_session,
|
|
get_ticktick_client,
|
|
)
|
|
from app.integrations.ticktick import TickTickAuthError, TickTickClient, TickTickConfigError, TickTickRequestError
|
|
from app.services.auth import AuthenticatedSession
|
|
from app.services.config_page import save_config_value
|
|
|
|
router = APIRouter(tags=["ticktick"])
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@router.get("/ticktick/auth/start")
|
|
def start_ticktick_auth(
|
|
current_auth: AuthenticatedSession | None = Depends(get_current_auth_session),
|
|
ticktick_client: TickTickClient = Depends(get_ticktick_client),
|
|
) -> Response:
|
|
if current_auth is None:
|
|
return RedirectResponse(url="/login", status_code=status.HTTP_303_SEE_OTHER)
|
|
|
|
try:
|
|
authorization_url = ticktick_client.build_authorization_url()
|
|
except TickTickConfigError as exc:
|
|
logger.warning("Rejected TickTick OAuth start due to incomplete configuration: %s", exc)
|
|
return PlainTextResponse("TickTick integration is not configured", status_code=400)
|
|
|
|
return RedirectResponse(url=authorization_url, status_code=status.HTTP_303_SEE_OTHER)
|
|
|
|
|
|
@router.get("/ticktick/auth/code")
|
|
def handle_ticktick_auth_code(
|
|
request: Request,
|
|
auth_db_session: Session = Depends(get_db),
|
|
settings: Settings = Depends(get_app_settings),
|
|
ticktick_client: TickTickClient = Depends(get_ticktick_client),
|
|
) -> Response:
|
|
code = request.query_params.get("code", "")
|
|
state = request.query_params.get("state", "")
|
|
|
|
if not code or not state:
|
|
return RedirectResponse(
|
|
url="/config?ticktick_oauth=invalid-callback",
|
|
status_code=status.HTTP_303_SEE_OTHER,
|
|
)
|
|
|
|
try:
|
|
token = ticktick_client.exchange_authorization_code(code=code, state=state)
|
|
save_config_value(
|
|
auth_db_session,
|
|
env_name="TICKTICK_TOKEN",
|
|
value=token,
|
|
bootstrap_settings=settings,
|
|
)
|
|
except TickTickAuthError as exc:
|
|
logger.warning("Rejected TickTick OAuth callback due to invalid state: %s", exc)
|
|
return RedirectResponse(
|
|
url="/config?ticktick_oauth=invalid-state",
|
|
status_code=status.HTTP_303_SEE_OTHER,
|
|
)
|
|
except (TickTickConfigError, TickTickRequestError, ValueError) as exc:
|
|
logger.warning("TickTick OAuth callback failed: %s", exc)
|
|
return RedirectResponse(
|
|
url="/config?ticktick_oauth=failed",
|
|
status_code=status.HTTP_303_SEE_OTHER,
|
|
)
|
|
|
|
return RedirectResponse(
|
|
url="/config?ticktick_oauth=success",
|
|
status_code=status.HTTP_303_SEE_OTHER,
|
|
) |