Files
privatestudio/privatestudio/studio/routers/common.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

27 lines
956 B
Python

"""Shared helpers for API routers."""
import yaml
from ninja.errors import HttpError
from privatestudio.studio.models import Studio
def get_studio_for_user(user, bucket: str) -> Studio:
"""Resolve a bucket name to a Studio the user may access (404/403 otherwise)."""
studio = Studio.objects.filter(bucket_name=bucket).first()
if studio is None:
raise HttpError(404, f"Studio '{bucket}' not found")
if not studio.user_has_access(user):
raise HttpError(403, f"No access to studio '{bucket}'")
return studio
def parse_yaml_body(request) -> dict:
"""Parse the raw request body as a YAML mapping (400 on failure)."""
try:
data = yaml.safe_load(request.body.decode("utf-8") or "")
except (yaml.YAMLError, UnicodeDecodeError) as e:
raise HttpError(400, f"Invalid YAML: {e}")
if not isinstance(data, dict):
raise HttpError(400, "Request body must be a YAML mapping")
return data