Some checks failed
build-images / web (push) Has been cancelled
vision2026-ci / Python SDK resource tests (push) Has been cancelled
build-images / api (push) Has been cancelled
build-images / worker-gpu (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Has been cancelled
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Has been cancelled
codeql / Analyze (javascript-typescript) (push) Has been cancelled
codeql / Analyze (python) (push) Has been cancelled
perf / API hot-path micro-smoke (budgeted) (push) Has been cancelled
perf / Desktop engine crates (release build check) (push) Has been cancelled
vision2026-ci / API phase modules (ruff + pytest) (push) Has been cancelled
vision2026-ci / Alembic upgrade + drift check (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Has been cancelled
Paid, hosted product at menivor.com, now retired as a company project. Fresh squashed history of the cleaned working tree (secret-scanned, no .env, no customer data, no outreach logs). Built on MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
97 lines
3.6 KiB
Python
97 lines
3.6 KiB
Python
"""Repurpose persistence: owner-scoped jobs + clips."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime, timezone
|
|
|
|
from sqlalchemy import select
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.models.db.repurpose import RepurposeClip, RepurposeJob
|
|
|
|
|
|
class RepurposeRepository:
|
|
def __init__(self, session: Session) -> None:
|
|
self._session = session
|
|
|
|
# Jobs -------------------------------------------------------------- #
|
|
def create_job(self, job: RepurposeJob) -> RepurposeJob:
|
|
self._session.add(job)
|
|
self._session.flush()
|
|
return job
|
|
|
|
def get_job_owned(self, job_id: str, user_id: str) -> RepurposeJob | None:
|
|
row = self._session.get(RepurposeJob, job_id)
|
|
if row is None or row.user_id != user_id:
|
|
return None
|
|
return row
|
|
|
|
def lock_job(self, job_id: str) -> RepurposeJob | None:
|
|
"""Lock + return the row's CURRENT state, even if already cached.
|
|
|
|
Mirrors ``AdStudioRepository.lock_variant``: ``populate_existing()``
|
|
matters whenever this call shares a session with an earlier read of
|
|
the same row (every caller here does — the job was just read to
|
|
decide whether it looks failed), without it SQLAlchemy's identity map
|
|
would hand back the stale, already-loaded Python object instead of
|
|
refreshing it from this query, making the ``FOR UPDATE`` lock pointless
|
|
for detecting a status change committed by another transaction.
|
|
"""
|
|
return (
|
|
self._session.query(RepurposeJob)
|
|
.filter(RepurposeJob.id == job_id)
|
|
.populate_existing()
|
|
.with_for_update()
|
|
.first()
|
|
)
|
|
|
|
def list_jobs(self, user_id: str, *, limit: int = 50, offset: int = 0) -> list[RepurposeJob]:
|
|
stmt = (
|
|
select(RepurposeJob)
|
|
.where(RepurposeJob.user_id == user_id)
|
|
.order_by(RepurposeJob.created_at.desc(), RepurposeJob.id)
|
|
.limit(max(1, min(200, limit)))
|
|
.offset(max(0, offset))
|
|
)
|
|
return list(self._session.execute(stmt).scalars().all())
|
|
|
|
def set_job_status(
|
|
self, job: RepurposeJob, status: str, *, error: str = "", clip_count: int | None = None
|
|
) -> None:
|
|
job.status = status
|
|
if error:
|
|
job.error = error
|
|
if clip_count is not None:
|
|
job.clip_count = int(clip_count)
|
|
job.updated_at = datetime.now(timezone.utc)
|
|
self._session.flush()
|
|
|
|
# Clips ------------------------------------------------------------- #
|
|
def replace_clips(self, job_id: str, user_id: str, clips: list[RepurposeClip]) -> None:
|
|
"""Delete any prior clips for the job and insert the new set."""
|
|
existing = self._session.execute(
|
|
select(RepurposeClip).where(RepurposeClip.repurpose_job_id == job_id)
|
|
).scalars().all()
|
|
for row in existing:
|
|
self._session.delete(row)
|
|
for clip in clips:
|
|
self._session.add(clip)
|
|
self._session.flush()
|
|
|
|
def list_clips(self, job_id: str, user_id: str) -> list[RepurposeClip]:
|
|
stmt = (
|
|
select(RepurposeClip)
|
|
.where(
|
|
RepurposeClip.repurpose_job_id == job_id,
|
|
RepurposeClip.user_id == user_id,
|
|
)
|
|
.order_by(RepurposeClip.virality_score.desc(), RepurposeClip.start_s)
|
|
)
|
|
return list(self._session.execute(stmt).scalars().all())
|
|
|
|
def get_clip_owned(self, clip_id: str, user_id: str) -> RepurposeClip | None:
|
|
row = self._session.get(RepurposeClip, clip_id)
|
|
if row is None or row.user_id != user_id:
|
|
return None
|
|
return row
|