refactor(api): Pydantic request models for small route modules (R6 S1b)
payload:dict → typed request models across the single/double-endpoint modules: - tags: TagCreateIn / TagUpdateIn (color:null clears → reads model_fields_set); _own_tag now delegates to owned_or_404. - config: ConfigSetIn(value:Any) — keeps the "Missing 'value'" 400 via model_fields_set. - me: SwitchAccountIn(user_id:int). - scheduler: JobIntervalIn / MaintenanceSettingsIn — the manual int()+400 becomes a clean 422 on a non-number; the range checks stay 400. - search: ClearSearchIn(video_ids:list[str]). - public: WatchUnlockIn(password:str). Gate: ruff clean, pytest 164 green.
This commit is contained in:
@@ -8,6 +8,7 @@ a short-lived signed grant that the media URL carries (a `<video src>` can't sen
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from fastapi.responses import FileResponse
|
||||
from pydantic import BaseModel
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
@@ -79,8 +80,12 @@ def watch_meta(token: str, db: Session = Depends(get_db)) -> dict:
|
||||
}
|
||||
|
||||
|
||||
class WatchUnlockIn(BaseModel):
|
||||
password: str = ""
|
||||
|
||||
|
||||
@router.post("/watch/{token}/unlock")
|
||||
def watch_unlock(token: str, payload: dict, db: Session = Depends(get_db)) -> dict:
|
||||
def watch_unlock(token: str, body: WatchUnlockIn, db: Session = Depends(get_db)) -> dict:
|
||||
"""Exchange the link password for a signed grant carried by the media URL."""
|
||||
if not _unlock_limit.allow(token):
|
||||
raise HTTPException(status_code=429, detail="Too many attempts. Try again later.")
|
||||
@@ -92,7 +97,7 @@ def watch_unlock(token: str, payload: dict, db: Session = Depends(get_db)) -> di
|
||||
"needs_password": False,
|
||||
**linksmod.public_meta(link, job, asset, _file_url(token), _poster_url(token, asset)),
|
||||
}
|
||||
if not verify_password((payload.get("password") or ""), link.password_hash):
|
||||
if not verify_password(body.password, link.password_hash):
|
||||
raise HTTPException(status_code=403, detail="Wrong password.")
|
||||
job, asset = _ready_target(db, link)
|
||||
link.view_count += 1
|
||||
|
||||
Reference in New Issue
Block a user