"""Transcode profile management endpoints (scoped to a studio).""" from django.shortcuts import get_object_or_404 from ninja import Router, Schema from ninja.errors import HttpError from privatestudio.studio.models import TranscodeProfile from privatestudio.studio.routers.common import get_studio_for_user, parse_yaml_body router = Router() class ProfileResponse(Schema): id: int name: str profile_definition: str created_at: str def _profile_response(profile: TranscodeProfile) -> dict: return { "id": profile.id, "name": profile.name, "profile_definition": profile.profile_definition, "created_at": profile.created_at.isoformat(), } @router.post("/studios/{bucket}/profiles", response=ProfileResponse) def create_profile(request, bucket: str, name: str): """ Create a new transcode profile. The request body must contain the YAML profile definition. """ studio = get_studio_for_user(request.auth, bucket) profile_data = parse_yaml_body(request) if "profiles" not in profile_data: raise HttpError(400, "Profile definition must contain a 'profiles' section") if TranscodeProfile.objects.filter(studio=studio, name=name).exists(): raise HttpError(409, f"Profile '{name}' already exists") profile = TranscodeProfile.objects.create( studio=studio, created_by=request.auth, name=name, profile_definition=request.body.decode("utf-8"), ) return _profile_response(profile) @router.get("/studios/{bucket}/profiles", response=list[ProfileResponse]) def list_profiles(request, bucket: str): """List all transcode profiles for the studio.""" studio = get_studio_for_user(request.auth, bucket) return [_profile_response(p) for p in studio.transcode_profiles.order_by("id")] @router.get("/studios/{bucket}/profiles/{profile_id}", response=ProfileResponse) def get_profile(request, bucket: str, profile_id: int): """Get a specific transcode profile.""" studio = get_studio_for_user(request.auth, bucket) return _profile_response(get_object_or_404(TranscodeProfile, pk=profile_id, studio=studio))