Restructure backend and frontend modules; add story, library, and editor schema migrations. Support editable automatic production, manual confirmation modes, account and user lifecycle management, compact container scrolling, consistent forms, and per-task result previews. Update inference and TTS scheduling, deployment guidance, and regression coverage; remove obsolete standalone pages and temporary acceptance artifacts.
164 lines
5.5 KiB
Python
164 lines
5.5 KiB
Python
from sqlalchemy import select, delete
|
|
from app.models import (
|
|
AssetReference,
|
|
ShotCandidate,
|
|
ShotSelection,
|
|
Job,
|
|
Execution,
|
|
WorkflowDraft,
|
|
Batch,
|
|
Submission,
|
|
utcnow,
|
|
)
|
|
from app.core.errors import fail
|
|
from app.core.maintenance import require_writable
|
|
from app.domain.tasks import TERMINAL
|
|
from app.services.stories.common import story
|
|
from app.services.events import emit_asset
|
|
|
|
|
|
async def ensure_terminal(s, c):
|
|
if c.job_id:
|
|
j = await s.get(Job, c.job_id)
|
|
if j and j.deleted_at is None and j.state not in TERMINAL:
|
|
fail(409, "deletion_requires_terminal", "请先取消候选任务并等待执行结束")
|
|
execution = await s.scalar(
|
|
select(Execution.id)
|
|
.where(
|
|
Execution.job_id == c.job_id, Execution.state.not_in({"exited", "results_ready"})
|
|
)
|
|
.limit(1)
|
|
)
|
|
if execution:
|
|
fail(409, "cleanup_pending", "候选执行尚未确认清理")
|
|
|
|
|
|
async def archive_candidate(s, c):
|
|
c.deleted_at = utcnow()
|
|
c.version += 1
|
|
await s.execute(
|
|
delete(AssetReference).where(
|
|
AssetReference.ref_kind == "candidate", AssetReference.ref_id == c.id
|
|
)
|
|
)
|
|
emit_asset(
|
|
s,
|
|
c.owner_id,
|
|
"candidate.deleted",
|
|
{"id": c.id, "draft_id": c.draft_id, "version": c.version},
|
|
)
|
|
from app.services.deletions import references_any
|
|
|
|
submissions = (
|
|
await s.scalars(
|
|
select(Submission).where(
|
|
Submission.owner_id == c.owner_id,
|
|
Submission.operation.like("story.candidate.%"),
|
|
Submission.status != "purged",
|
|
)
|
|
)
|
|
).all()
|
|
for sub in submissions:
|
|
if references_any(sub.resource_json, {c.id}):
|
|
sub.status = "purged"
|
|
|
|
|
|
async def delete_candidate(runtime, actor, id, candidate_id, if_match):
|
|
require_writable(runtime.settings)
|
|
async with runtime.db.session() as s:
|
|
await story(s, id, actor.id)
|
|
c = await s.scalar(
|
|
select(ShotCandidate)
|
|
.where(
|
|
ShotCandidate.id == candidate_id,
|
|
ShotCandidate.draft_id == id,
|
|
ShotCandidate.owner_id == actor.id,
|
|
)
|
|
.with_for_update()
|
|
)
|
|
if not c:
|
|
fail(404, "candidate_not_found", "候选不存在")
|
|
if c.deleted_at:
|
|
return {"deleted": {"candidate_ids": [c.id]}}
|
|
if if_match is None or if_match.strip('"') != str(c.version):
|
|
fail(409, "version_conflict", "候选版本冲突")
|
|
await ensure_terminal(s, c)
|
|
await archive_candidate(s, c)
|
|
await s.commit()
|
|
return {"deleted": {"candidate_ids": [c.id]}}
|
|
|
|
|
|
async def delete_story(runtime, actor, id, if_match):
|
|
from app.services.deletions import archive_records
|
|
|
|
require_writable(runtime.settings)
|
|
async with runtime.db.session() as s:
|
|
d = await story(s, id, actor.id)
|
|
if if_match is None or if_match.strip('"') != str(d.version):
|
|
fail(409, "draft_version_conflict", "故事版本冲突")
|
|
rows = (
|
|
await s.scalars(
|
|
select(ShotCandidate)
|
|
.where(
|
|
ShotCandidate.draft_id == id,
|
|
ShotCandidate.owner_id == actor.id,
|
|
ShotCandidate.deleted_at.is_(None),
|
|
)
|
|
.with_for_update()
|
|
)
|
|
).all()
|
|
for c in rows:
|
|
await ensure_terminal(s, c)
|
|
result = await archive_records(s, WorkflowDraft, id, actor.id, if_match)
|
|
for c in rows:
|
|
await archive_candidate(s, c)
|
|
for batch_id in {c.batch_id for c in rows if c.batch_id}:
|
|
b = await s.get(Batch, batch_id)
|
|
if b and b.owner_id == actor.id and b.deleted_at is None:
|
|
b.deleted_at = utcnow()
|
|
b.version += 1
|
|
b.state = (
|
|
"cancelled"
|
|
if b.state not in {"succeeded", "completed_with_errors"}
|
|
else b.state
|
|
)
|
|
emit_asset(s, actor.id, "batch.deleted", {"id": b.id, "version": b.version})
|
|
selection_ids = select(ShotSelection.id).where(
|
|
ShotSelection.draft_id == id, ShotSelection.owner_id == actor.id
|
|
)
|
|
await s.execute(
|
|
delete(AssetReference).where(
|
|
AssetReference.ref_kind == "selection", AssetReference.ref_id.in_(selection_ids)
|
|
)
|
|
)
|
|
emit_asset(s, actor.id, "story.deleted", {"id": id, "version": d.version})
|
|
result["deleted"]["candidate_ids"] = [c.id for c in rows]
|
|
await s.commit()
|
|
return result
|
|
|
|
|
|
async def delete_candidate_batch(runtime, actor, id, if_match):
|
|
from app.services.deletions import archive_records
|
|
|
|
require_writable(runtime.settings)
|
|
async with runtime.db.session() as s:
|
|
rows = (
|
|
await s.scalars(
|
|
select(ShotCandidate)
|
|
.where(
|
|
ShotCandidate.batch_id == id,
|
|
ShotCandidate.owner_id == actor.id,
|
|
ShotCandidate.deleted_at.is_(None),
|
|
)
|
|
.with_for_update()
|
|
)
|
|
).all()
|
|
for c in rows:
|
|
await ensure_terminal(s, c)
|
|
result = await archive_records(s, Batch, id, actor.id, if_match)
|
|
for c in rows:
|
|
await archive_candidate(s, c)
|
|
result["deleted"]["candidate_ids"] = [c.id for c in rows]
|
|
await s.commit()
|
|
return result
|