Files
privatestudio/privatestudio/studio/routers/transcode.py
T
Shaun KrugerandClaude Opus 5.5 b3b51f6cc5 Convert web service from FastAPI to Django 6.1
Replace the FastAPI/SQLAlchemy/Alembic web service with a Django project
laid out like cumorah-build: manage.py at the root, settings in the
privatestudio.settings package (base/dev/production), and a
privatestudio.studio app.

- Studio model owns a unique bucket_name; access is granted through
  membership in the studio's auth Group. Assets, profiles and jobs are
  scoped to a studio; API keys and S3 credentials stay per user.
- JSON API via Django Ninja under /api/ (profiles and transcode jobs now
  live under /api/studios/{bucket}/).
- S3-compatible interface as plain class-based views under /s3/,
  supporting bearer tokens and AWS v4 signatures.
- create_studio and create_key management commands replace ps-serve.
- Fresh Django initial migration; tests ported to Django TestCase.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 21:54:16 -07:00

101 lines
3.4 KiB
Python

"""YAML-based transcode job request endpoints (scoped to a studio)."""
from typing import Optional
import yaml
from django.shortcuts import get_object_or_404
from ninja import Router, Schema
from ninja.errors import HttpError
from privatestudio.studio.models import TranscodeJob, TranscodeProfile, UploadedAsset
from privatestudio.studio.routers.common import get_studio_for_user, parse_yaml_body
router = Router()
class TranscodeJobResponse(Schema):
id: int
asset_id: int
profile_id: Optional[int]
status: str
requested_at: str
def _job_response(job: TranscodeJob) -> dict:
return {
"id": job.id,
"asset_id": job.asset_id,
"profile_id": job.profile_id,
"status": job.status,
"requested_at": job.requested_at.isoformat(),
}
@router.post("/studios/{bucket}/transcode", response=TranscodeJobResponse)
def request_transcode(request, bucket: str):
"""
Request a transcode job with a YAML definition in the request body.
```yaml
asset: "video.mp4" # Asset key, filename or ID
profile: "hd-profile" # Profile name, or an inline profile definition
```
"""
studio = get_studio_for_user(request.auth, bucket)
request_data = parse_yaml_body(request)
asset_ref = request_data.get("asset")
if not asset_ref:
raise HttpError(400, "Request must specify 'asset'")
assets = UploadedAsset.objects.filter(studio=studio)
if isinstance(asset_ref, int):
asset = assets.filter(pk=asset_ref).first()
else:
asset_ref = str(asset_ref)
asset = assets.filter(s3_path=asset_ref).first() or assets.filter(filename=asset_ref).first()
if asset is None:
raise HttpError(404, f"Asset '{asset_ref}' not found")
profile_ref = request_data.get("profile")
if not profile_ref:
raise HttpError(400, "Request must specify 'profile'")
profile = None
if isinstance(profile_ref, str):
profile = TranscodeProfile.objects.filter(studio=studio, name=profile_ref).first()
if profile is None:
raise HttpError(404, f"Profile '{profile_ref}' not found")
profile_definition = profile.profile_definition
elif isinstance(profile_ref, dict):
if "profiles" not in profile_ref:
raise HttpError(400, "Inline profile must contain a 'profiles' section")
profile_definition = yaml.safe_dump(profile_ref)
else:
raise HttpError(400, "Profile must be a string (name) or object (definition)")
job = TranscodeJob.objects.create(
studio=studio,
requested_by=request.auth,
asset=asset,
profile=profile,
profile_definition=profile_definition,
)
return _job_response(job)
@router.get("/studios/{bucket}/transcode/{job_id}", response=TranscodeJobResponse)
def get_transcode_job(request, bucket: str, job_id: int):
"""Get the status of a transcode job."""
studio = get_studio_for_user(request.auth, bucket)
return _job_response(get_object_or_404(TranscodeJob, pk=job_id, studio=studio))
@router.get("/studios/{bucket}/transcode", response=list[TranscodeJobResponse])
def list_transcode_jobs(request, bucket: str, status: Optional[str] = None):
"""List transcode jobs for the studio."""
studio = get_studio_for_user(request.auth, bucket)
jobs = studio.transcode_jobs.order_by("id")
if status:
jobs = jobs.filter(status=status)
return [_job_response(job) for job in jobs]