Files
ss-tools/backend/src/plugins/translate/preview_session_ops.py
busya f872e610a9 refactor: decompose oversized contracts to satisfy INV_7 fractal limit
Break monolithic modules >400 lines into focused sub-modules while
preserving backward-compatible imports and all test coverage:

Backend (Python):
- TranslationExecutor: 1974→241 lines, split into 9 sub-modules
- Translate plugin: orchestrator (1137→148), preview (1303→244),
  service (1052→275), dictionary (1007→68)
- ProfileService: 857→172 with 4 extracted sub-modules
- TaskManager: 708→322 with graph/event_bus/lifecycle extracted
- Test dictionary: 1199→split into 6 focused test files

Frontend (Svelte):
- SettingsPage: 1451→291 with 6 extracted tab components
- GitManager: 1220→228 with 5 extracted panels
- DatasetReviewWorkspace: 1202→314
- translate.js API: 664→28 barrel with 6 domain modules

Protocol:
- Remove single-contract 150-line limit from INV_7 (keep CC≤10)
- Fix unclosed #endregion tags across 11 files
- Fix 19 test regressions from stale mock paths
- All 294 tests passing
2026-05-17 19:18:32 +03:00

95 lines
3.8 KiB
Python

# #region preview_session_ops [C:3] [TYPE Module] [SEMANTICS preview, session, accept, get]
# @BRIEF Preview session lifecycle operations: accept and query preview sessions.
# @RELATION DEPENDS_ON -> [TranslationPreviewSession]
# @RELATION DEPENDS_ON -> [TranslationPreviewRecord]
# @RELATION DEPENDS_ON -> [preview_session_serializer]
from typing import Any
from sqlalchemy.orm import Session
from ...models.translate import (
TranslationJob,
TranslationPreviewRecord,
TranslationPreviewSession,
)
from .preview_session_serializer import (
apply_language_action, # noqa: F401 — re-export for preview_review.py
apply_record_action, # noqa: F401 — re-export for preview_review.py
serialize_preview_record as _serialize_preview_record,
)
# #region accept_preview_session [C:2] [TYPE Function] [SEMANTICS preview, session, accept]
# @BRIEF Mark a preview session as accepted, which gates full execution.
# @SIDE_EFFECT: DB writes on session status.
def accept_preview_session(db: Session, job_id: str, _current_user: str | None = None) -> dict[str, Any]:
"""Mark a preview session as accepted and return the session data with records."""
session = (
db.query(TranslationPreviewSession)
.filter(TranslationPreviewSession.job_id == job_id, TranslationPreviewSession.status == "ACTIVE")
.order_by(TranslationPreviewSession.created_at.desc())
.first()
)
if not session:
raise ValueError(f"No active preview session for job '{job_id}'")
session.status = "APPLIED"
db.commit()
db.refresh(session)
records = (
db.query(TranslationPreviewRecord)
.filter(TranslationPreviewRecord.session_id == session.id)
.all()
)
job = db.query(TranslationJob).filter(TranslationJob.id == job_id).first()
target_languages = job.target_languages or [job.target_dialect or "en"] if job else ["en"]
if not isinstance(target_languages, list):
target_languages = [str(target_languages)]
return {
"id": session.id,
"job_id": job_id,
"status": "APPLIED",
"created_by": session.created_by,
"created_at": session.created_at.isoformat(),
"expires_at": session.expires_at.isoformat() if session.expires_at else None,
"target_languages": target_languages,
"records": [_serialize_preview_record(r) for r in records],
}
# #endregion accept_preview_session
# #region get_preview_session [C:2] [TYPE Function] [SEMANTICS preview, session, query]
# @BRIEF Get the latest preview session for a job with its records.
def get_preview_session(db: Session, job_id: str) -> dict[str, Any]:
"""Get the latest preview session for a job with its records."""
session = (
db.query(TranslationPreviewSession)
.filter(TranslationPreviewSession.job_id == job_id)
.order_by(TranslationPreviewSession.created_at.desc())
.first()
)
if not session:
raise ValueError(f"No preview session found for job '{job_id}'")
records = (
db.query(TranslationPreviewRecord)
.filter(TranslationPreviewRecord.session_id == session.id)
.all()
)
job = db.query(TranslationJob).filter(TranslationJob.id == job_id).first()
target_languages = job.target_languages or [job.target_dialect or "en"] if job else ["en"]
if not isinstance(target_languages, list):
target_languages = [str(target_languages)]
return {
"id": session.id,
"job_id": job_id,
"status": session.status,
"created_by": session.created_by,
"created_at": session.created_at.isoformat(),
"expires_at": session.expires_at.isoformat() if session.expires_at else None,
"target_languages": target_languages,
"records": [_serialize_preview_record(r) for r in records],
}
# #endregion get_preview_session
# #endregion preview_session_ops