acupedia-api/app/bootstrap/adapters.py

175 lines
5.2 KiB
Python

from __future__ import annotations
from pathlib import Path
from typing import Any
from uuid import UUID
from sqlalchemy.ext.asyncio import AsyncSession
from .seed_auth import AuthBootstrapSeeder
from .seed_commerce import CommerceBootstrapSeeder
from .seed_content.orchestrator import SeedOrchestrator
from .seed_media.service import ImageSeedService
from .seed_media.validators import load_manifest
class AuthBootstrapAdapter:
async def run(
self,
session: AsyncSession,
*,
bootstrap_admin: Any | None = None,
) -> dict[str, object]:
result = await AuthBootstrapSeeder(session).seed(
bootstrap_admin=bootstrap_admin
)
return {
"roles": result.roles,
"permissions": result.permissions,
"role_permissions": result.role_permissions,
"bootstrap_admin_created": result.bootstrap_admin_created,
}
class ContentBootstrapAdapter:
"""
Exact adapter over the existing content SeedOrchestrator contract.
Important:
- no outer transaction
- SeedOrchestrator retains per-template commit/SAVEPOINT behavior
- all legacy fine-grained selectors are preserved
"""
async def run(
self,
session: AsyncSession,
*,
config_path: Path,
modules_root: Path,
module_codes: set[str] | None = None,
template_codes: set[str] | None = None,
content_codes: set[str] | None = None,
search_facets_only: bool = False,
skip_search_facets: bool = False,
publish_content: bool = False,
cleanup: bool = False,
continue_on_error: bool = False,
created_by: UUID | None = None,
) -> dict[str, object]:
summaries = await SeedOrchestrator(session).process_config(
config_path=config_path.resolve(),
modules_root=modules_root.resolve(),
module_codes=module_codes,
template_codes=template_codes,
content_codes=content_codes,
search_facets_only=search_facets_only,
skip_search_facets=skip_search_facets,
publish_content=publish_content,
cleanup=cleanup,
continue_on_error=continue_on_error,
created_by=created_by,
)
serialized: list[dict[str, object]] = []
for summary in summaries:
serialized.append(
{
"module_code": summary.module_code,
"definition_sets_processed": summary.definition_sets_processed,
"content_rows_succeeded": summary.content_rows_succeeded,
"content_rows_failed": summary.content_rows_failed,
"associations_written": summary.associations_written,
"stale_associations_removed": summary.stale_associations_removed,
"errors": list(summary.errors),
}
)
return {
"status": "completed",
"modules": serialized,
}
class MediaBootstrapAdapter:
"""
Exact adapter over the existing image/media seeder behavior.
Storage configuration is supplied by bootstrap CLI from the same
environment variables used by the legacy script.
"""
async def run(
self,
session: AsyncSession,
*,
manifest_path: Path,
media_root: Path,
storage: Any,
bucket: str,
validate_only: bool = False,
dry_run: bool = False,
force_reupload: bool = False,
) -> dict[str, object]:
manifest = load_manifest(manifest_path)
service = ImageSeedService(
session=session,
storage=storage,
bucket=bucket,
)
report = await service.seed(
manifest=manifest,
media_root=media_root,
dry_run=dry_run,
validate_only=validate_only,
force_reupload=force_reupload,
)
return {
"ok": report.ok,
"total": report.total,
"validated": report.validated,
"uploaded": report.uploaded,
"reused_objects": report.reused_objects,
"db_upserts": report.db_upserts,
"associations_upserted": report.associations_upserted,
"errors": [
{
"index": issue.index,
"file": issue.file,
"message": issue.message,
}
for issue in report.errors
],
}
class CommerceBootstrapAdapter:
async def run(
self,
session: AsyncSession,
*,
plans_path: Path | None = None,
plans: Any = None,
coupons: Any = None,
) -> dict[str, object]:
seeder = CommerceBootstrapSeeder(session)
kwargs: dict[str, Any] = {}
if plans_path is not None:
kwargs["plans_path"] = plans_path
if plans is not None:
kwargs["plans"] = plans
if coupons is not None:
kwargs["coupons"] = coupons
result = await seeder.seed(**kwargs)
return {
"plans": result.plans,
"plan_module_mappings": result.plan_module_mappings,
"coupons": result.coupons,
}