435 行
14 KiB
Python
435 行
14 KiB
Python
"""HTTP handlers for the isolated advanced-material workflow."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from backend.routes.common import handle_request
|
|
from backend.routes.params import int_or_default, optional_str
|
|
from backend.services.advanced_material_service import (
|
|
AdvancedMaterialService,
|
|
create_advanced_material_service,
|
|
)
|
|
from backend.services.media_service import MediaService
|
|
from core.identity import RequestContext
|
|
from core.security import require_authenticated, require_permission
|
|
|
|
|
|
def list_advanced_material_imports(
|
|
ctx: RequestContext,
|
|
query: dict[str, Any] | None = None,
|
|
service: AdvancedMaterialService | None = None,
|
|
) -> dict:
|
|
resolved = service or create_advanced_material_service()
|
|
params = query or {}
|
|
|
|
def action() -> dict[str, Any]:
|
|
require_permission(ctx, "can_manage_materials")
|
|
payload = resolved.list_imports(
|
|
status=optional_str(params.get("status")),
|
|
page=int_or_default(params.get("page"), 1),
|
|
page_size=int_or_default(params.get("page_size"), 30),
|
|
)
|
|
return _serialize_import_list(payload)
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def upload_advanced_material(
|
|
ctx: RequestContext,
|
|
payload: dict[str, Any] | None = None,
|
|
service: AdvancedMaterialService | None = None,
|
|
) -> dict:
|
|
resolved = service or create_advanced_material_service()
|
|
|
|
def action() -> dict[str, Any]:
|
|
require_permission(ctx, "can_manage_materials")
|
|
return _serialize_payload(resolved.upload(payload or {}, ctx))
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def upload_advanced_style_set(
|
|
ctx: RequestContext,
|
|
payload: dict[str, Any] | None = None,
|
|
service: AdvancedMaterialService | None = None,
|
|
) -> dict:
|
|
"""Create one style-only multi-image import and enqueue every source."""
|
|
|
|
resolved = service or create_advanced_material_service()
|
|
|
|
def action() -> dict[str, Any]:
|
|
require_permission(ctx, "can_manage_materials")
|
|
return _serialize_payload(resolved.upload_style_set(payload or {}, ctx))
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def get_advanced_material_import(
|
|
ctx: RequestContext,
|
|
import_id: str,
|
|
service: AdvancedMaterialService | None = None,
|
|
) -> dict:
|
|
resolved = service or create_advanced_material_service()
|
|
|
|
def action() -> dict[str, Any]:
|
|
require_permission(ctx, "can_manage_materials")
|
|
return _serialize_payload(resolved.get_import(import_id))
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def delete_advanced_material_import(
|
|
ctx: RequestContext,
|
|
import_id: str,
|
|
service: AdvancedMaterialService | None = None,
|
|
) -> dict:
|
|
resolved = service or create_advanced_material_service()
|
|
|
|
def action() -> dict[str, Any]:
|
|
require_permission(ctx, "can_manage_materials")
|
|
return resolved.delete_import(import_id, ctx)
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def retry_advanced_material_import(
|
|
ctx: RequestContext,
|
|
import_id: str,
|
|
payload: dict[str, Any] | None = None,
|
|
service: AdvancedMaterialService | None = None,
|
|
) -> dict:
|
|
del payload
|
|
resolved = service or create_advanced_material_service()
|
|
|
|
def action() -> dict[str, Any]:
|
|
require_permission(ctx, "can_manage_materials")
|
|
return _serialize_payload(resolved.retry_import(import_id, ctx))
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def retry_advanced_style_source(
|
|
ctx: RequestContext,
|
|
import_id: str,
|
|
source_id: str,
|
|
payload: dict[str, Any] | None = None,
|
|
service: AdvancedMaterialService | None = None,
|
|
) -> dict:
|
|
del payload
|
|
resolved = service or create_advanced_material_service()
|
|
|
|
def action() -> dict[str, Any]:
|
|
require_permission(ctx, "can_manage_materials")
|
|
return _serialize_payload(resolved.retry_style_source(import_id, source_id, ctx))
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def exclude_advanced_style_source(
|
|
ctx: RequestContext,
|
|
import_id: str,
|
|
source_id: str,
|
|
payload: dict[str, Any] | None = None,
|
|
service: AdvancedMaterialService | None = None,
|
|
) -> dict:
|
|
resolved = service or create_advanced_material_service()
|
|
|
|
def action() -> dict[str, Any]:
|
|
require_permission(ctx, "can_manage_materials")
|
|
excluded = bool((payload or {}).get("excluded", True))
|
|
return _serialize_payload(resolved.exclude_style_source(import_id, source_id, excluded, ctx))
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def create_advanced_style_consensus(
|
|
ctx: RequestContext,
|
|
import_id: str,
|
|
payload: dict[str, Any] | None = None,
|
|
service: AdvancedMaterialService | None = None,
|
|
) -> dict:
|
|
resolved = service or create_advanced_material_service()
|
|
|
|
def action() -> dict[str, Any]:
|
|
require_permission(ctx, "can_manage_materials")
|
|
return _serialize_payload(resolved.create_style_consensus(import_id, payload or {}, ctx))
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def retry_advanced_style_validation_case(
|
|
ctx: RequestContext,
|
|
import_id: str,
|
|
case_id: str,
|
|
payload: dict[str, Any] | None = None,
|
|
service: AdvancedMaterialService | None = None,
|
|
) -> dict:
|
|
del payload
|
|
resolved = service or create_advanced_material_service()
|
|
|
|
def action() -> dict[str, Any]:
|
|
require_permission(ctx, "can_manage_materials")
|
|
return _serialize_payload(resolved.retry_style_validation_case(import_id, case_id, ctx))
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def cancel_advanced_material_import(
|
|
ctx: RequestContext,
|
|
import_id: str,
|
|
payload: dict[str, Any] | None = None,
|
|
service: AdvancedMaterialService | None = None,
|
|
) -> dict:
|
|
del payload
|
|
resolved = service or create_advanced_material_service()
|
|
|
|
def action() -> dict[str, Any]:
|
|
require_permission(ctx, "can_manage_materials")
|
|
return _serialize_payload(resolved.cancel_import(import_id, ctx))
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def list_advanced_material_attempts(
|
|
ctx: RequestContext,
|
|
import_id: str,
|
|
query: dict[str, Any] | None = None,
|
|
service: AdvancedMaterialService | None = None,
|
|
) -> dict:
|
|
del query
|
|
resolved = service or create_advanced_material_service()
|
|
|
|
def action() -> list[dict[str, Any]]:
|
|
require_permission(ctx, "can_manage_materials")
|
|
return [_serialize_attempt(item) for item in resolved.list_attempts(import_id)]
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def create_advanced_material_attempt(
|
|
ctx: RequestContext,
|
|
import_id: str,
|
|
payload: dict[str, Any] | None = None,
|
|
service: AdvancedMaterialService | None = None,
|
|
) -> dict:
|
|
resolved = service or create_advanced_material_service()
|
|
|
|
def action() -> dict[str, Any]:
|
|
require_permission(ctx, "can_manage_materials")
|
|
return _serialize_payload(resolved.create_validation_attempt(import_id, payload or {}, ctx))
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def reject_advanced_material_attempt(
|
|
ctx: RequestContext,
|
|
import_id: str,
|
|
attempt_id: str,
|
|
payload: dict[str, Any] | None = None,
|
|
service: AdvancedMaterialService | None = None,
|
|
) -> dict:
|
|
resolved = service or create_advanced_material_service()
|
|
|
|
def action() -> dict[str, Any]:
|
|
require_permission(ctx, "can_manage_materials")
|
|
return _serialize_payload(resolved.reject_attempt(import_id, attempt_id, payload or {}, ctx))
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def accept_advanced_material_attempt(
|
|
ctx: RequestContext,
|
|
import_id: str,
|
|
attempt_id: str,
|
|
payload: dict[str, Any] | None = None,
|
|
service: AdvancedMaterialService | None = None,
|
|
) -> dict:
|
|
resolved = service or create_advanced_material_service()
|
|
|
|
def action() -> dict[str, Any]:
|
|
require_permission(ctx, "can_manage_materials")
|
|
return _serialize_payload(resolved.accept_attempt(import_id, attempt_id, payload or {}, ctx))
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def list_advanced_materials(
|
|
ctx: RequestContext,
|
|
query: dict[str, Any] | None = None,
|
|
service: AdvancedMaterialService | None = None,
|
|
) -> dict:
|
|
resolved = service or create_advanced_material_service()
|
|
params = query or {}
|
|
|
|
def action() -> dict[str, Any]:
|
|
require_authenticated(ctx)
|
|
payload = resolved.list_materials(
|
|
page=int_or_default(params.get("page"), 1),
|
|
page_size=int_or_default(params.get("page_size"), 30),
|
|
)
|
|
return _serialize_material_list(payload)
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def get_advanced_material(
|
|
ctx: RequestContext,
|
|
material_id: str,
|
|
service: AdvancedMaterialService | None = None,
|
|
) -> dict:
|
|
resolved = service or create_advanced_material_service()
|
|
|
|
def action() -> dict[str, Any]:
|
|
require_authenticated(ctx)
|
|
return _serialize_material(resolved.get_material(material_id).to_dict())
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def archive_advanced_material(
|
|
ctx: RequestContext,
|
|
material_id: str,
|
|
service: AdvancedMaterialService | None = None,
|
|
) -> dict:
|
|
resolved = service or create_advanced_material_service()
|
|
|
|
def action() -> dict[str, Any]:
|
|
require_permission(ctx, "can_manage_materials")
|
|
return _serialize_material(resolved.archive_material(material_id, ctx).to_dict())
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def list_material_catalog(
|
|
ctx: RequestContext,
|
|
query: dict[str, Any] | None = None,
|
|
service: AdvancedMaterialService | None = None,
|
|
) -> dict:
|
|
resolved = service or create_advanced_material_service()
|
|
params = query or {}
|
|
|
|
def action() -> dict[str, Any]:
|
|
require_authenticated(ctx)
|
|
payload = resolved.list_catalog(
|
|
page=int_or_default(params.get("page"), 1),
|
|
page_size=int_or_default(params.get("page_size"), 100),
|
|
)
|
|
payload["items"] = [
|
|
_serialize_material(item) if item.get("library_kind") == "advanced" else _serialize_standard_material(item)
|
|
for item in payload.get("items", [])
|
|
]
|
|
return payload
|
|
|
|
return handle_request(ctx, action)
|
|
|
|
|
|
def _serialize_import_list(payload: dict[str, Any]) -> dict[str, Any]:
|
|
return {
|
|
**payload,
|
|
"items": [_serialize_import(item) for item in payload.get("items", [])],
|
|
}
|
|
|
|
|
|
def _serialize_material_list(payload: dict[str, Any]) -> dict[str, Any]:
|
|
return {
|
|
**payload,
|
|
"items": [_serialize_material(item) for item in payload.get("items", [])],
|
|
}
|
|
|
|
|
|
def _serialize_payload(payload: Any) -> Any:
|
|
if isinstance(payload, list):
|
|
return [_serialize_payload(item) for item in payload]
|
|
if not isinstance(payload, dict):
|
|
return payload
|
|
result = {key: _serialize_payload(value) for key, value in payload.items()}
|
|
if "source_id" in result and "source_image_uri" in result:
|
|
return _serialize_style_source(result)
|
|
if "case_id" in result and "validation_image_uri" in result:
|
|
return _serialize_style_validation_case(result)
|
|
if "import_id" in result and "source_image_uri" in result:
|
|
return _serialize_import(result)
|
|
if "attempt_id" in result and "validation_image_uri" in result:
|
|
return _serialize_attempt(result)
|
|
if str(result.get("material_id") or "").startswith("advmat_"):
|
|
return _serialize_material(result)
|
|
return result
|
|
|
|
|
|
def _serialize_import(payload: dict[str, Any]) -> dict[str, Any]:
|
|
return {
|
|
**payload,
|
|
"source_image_url": _signed_url(payload.get("source_image_uri")),
|
|
"source_thumbnail_url": _signed_url(payload.get("source_thumbnail_uri")),
|
|
}
|
|
|
|
|
|
def _serialize_attempt(payload: dict[str, Any]) -> dict[str, Any]:
|
|
return {
|
|
**payload,
|
|
**(
|
|
{
|
|
"validation_cases": [
|
|
_serialize_style_validation_case(item)
|
|
for item in payload.get("validation_cases", [])
|
|
if isinstance(item, dict)
|
|
]
|
|
}
|
|
if isinstance(payload.get("validation_cases"), list)
|
|
else {}
|
|
),
|
|
"validation_image_url": _signed_url(payload.get("validation_image_uri")),
|
|
"validation_thumbnail_url": _signed_url(payload.get("validation_thumbnail_uri")),
|
|
}
|
|
|
|
|
|
def _serialize_style_source(payload: dict[str, Any]) -> dict[str, Any]:
|
|
return {
|
|
**payload,
|
|
"source_image_url": _signed_url(payload.get("source_image_uri")),
|
|
"source_thumbnail_url": _signed_url(payload.get("source_thumbnail_uri")),
|
|
}
|
|
|
|
|
|
def _serialize_style_validation_case(payload: dict[str, Any]) -> dict[str, Any]:
|
|
return {
|
|
**payload,
|
|
"validation_image_url": _signed_url(payload.get("validation_image_uri")),
|
|
"validation_thumbnail_url": _signed_url(payload.get("validation_thumbnail_uri")),
|
|
}
|
|
|
|
|
|
def _serialize_material(payload: dict[str, Any]) -> dict[str, Any]:
|
|
return {
|
|
**payload,
|
|
"library_kind": "advanced",
|
|
"source_image_url": _signed_url(payload.get("source_image_uri")),
|
|
"source_thumbnail_url": _signed_url(payload.get("source_thumbnail_uri")),
|
|
"validation_image_url": _signed_url(payload.get("validation_image_uri")),
|
|
"validation_thumbnail_url": _signed_url(payload.get("validation_thumbnail_uri")),
|
|
"image_uri": payload.get("source_image_uri"),
|
|
"thumbnail_uri": payload.get("source_thumbnail_uri"),
|
|
"image_url": _signed_url(payload.get("source_image_uri")),
|
|
"thumbnail_url": _signed_url(payload.get("source_thumbnail_uri")),
|
|
"recognition_status": "confirmed",
|
|
}
|
|
|
|
|
|
def _serialize_standard_material(payload: dict[str, Any]) -> dict[str, Any]:
|
|
return {
|
|
**payload,
|
|
"library_kind": "standard",
|
|
"image_url": _signed_url(payload.get("image_uri")),
|
|
"thumbnail_url": _signed_url(payload.get("thumbnail_uri")),
|
|
}
|
|
|
|
|
|
def _signed_url(uri: Any) -> str | None:
|
|
text = str(uri or "").strip()
|
|
if not text:
|
|
return None
|
|
try:
|
|
return MediaService().sign_media(text)["url"]
|
|
except Exception:
|
|
return None
|