Download app/routers/admin/vod.py from binhugg2/api: direct link, hf CLI and curl.
- Browser
- Download file 6.05 kB
-
https://huggingface.co/spaces/binhugg2/api/resolve/main/app/routers/admin/vod.py
- Command line
-
hf download hf://spaces/binhugg2/api/app/routers/admin/vod.py
-
curl -L -o vod.py https://huggingface.co/spaces/binhugg2/api/resolve/main/app/routers/admin/vod.py
6.05 kB
| from fastapi import APIRouter, Depends, Query | |
| from sqlalchemy import delete as sa_delete, func, select, update | |
| from sqlalchemy.ext.asyncio import AsyncSession | |
| from app.cache import invalidate_vod_cache | |
| from app.database import get_db | |
| from app.dependencies import require_permission | |
| from app.models.admin import Admin | |
| from app.models.vod import Vod | |
| from app.models.vod_source import VodSource | |
| from app.schemas.admin.vod import VodUpdateRequest | |
| from app.schemas.common import ApiResponse, PaginatedData | |
| router = APIRouter() | |
| async def list_vods( | |
| page: int = Query(1, ge=1), | |
| page_size: int = Query(20, ge=1, le=100), | |
| category_id: int | None = Query(None), | |
| keyword: str | None = Query(None), | |
| is_hidden: bool | None = Query(None), | |
| is_recommended: bool | None = Query(None), | |
| admin: Admin = Depends(require_permission("vod:list")), | |
| db: AsyncSession = Depends(get_db), | |
| ): | |
| query = select(Vod) | |
| count_query = select(func.count(Vod.id)) | |
| if category_id: | |
| query = query.where(Vod.category_id == category_id) | |
| count_query = count_query.where(Vod.category_id == category_id) | |
| if keyword: | |
| pattern = f"%{keyword}%" | |
| q = Vod.name.ilike(pattern) | |
| query = query.where(q) | |
| count_query = count_query.where(q) | |
| if is_hidden is not None: | |
| query = query.where(Vod.is_hidden == is_hidden) | |
| count_query = count_query.where(Vod.is_hidden == is_hidden) | |
| if is_recommended is not None: | |
| query = query.where(Vod.is_recommended == is_recommended) | |
| count_query = count_query.where(Vod.is_recommended == is_recommended) | |
| total_result = await db.execute(count_query) | |
| total = total_result.scalar() or 0 | |
| result = await db.execute( | |
| query.order_by(Vod.updated_at.desc()) | |
| .offset((page - 1) * page_size) | |
| .limit(page_size) | |
| ) | |
| vods = result.scalars().all() | |
| if not vods: | |
| return ApiResponse.ok( | |
| PaginatedData(list=[], total=total, page=page, page_size=page_size).model_dump() | |
| ) | |
| vod_ids = [v.id for v in vods] | |
| src_count_result = await db.execute( | |
| select(VodSource.vod_id, func.count(VodSource.id)) | |
| .where(VodSource.vod_id.in_(vod_ids)) | |
| .group_by(VodSource.vod_id) | |
| ) | |
| src_count_map = {row[0]: row[1] for row in src_count_result.all()} | |
| items = [] | |
| for v in vods: | |
| items.append({ | |
| "id": v.id, "name": v.name, "cover": v.cover, | |
| "category_id": v.category_id, "score": v.score, | |
| "year": v.year, "area": v.area, "is_end": v.is_end, | |
| "is_hidden": v.is_hidden, "is_recommended": v.is_recommended, | |
| "remarks": v.remarks, "view_count": v.view_count, | |
| "source_count": src_count_map.get(v.id, 0), | |
| "updated_at": str(v.updated_at) if v.updated_at else None, | |
| }) | |
| return ApiResponse.ok( | |
| PaginatedData(list=items, total=total, page=page, page_size=page_size).model_dump() | |
| ) | |
| async def get_vod( | |
| vod_id: int, | |
| admin: Admin = Depends(require_permission("vod:detail")), | |
| db: AsyncSession = Depends(get_db), | |
| ): | |
| result = await db.execute(select(Vod).where(Vod.id == vod_id)) | |
| vod = result.scalar_one_or_none() | |
| if not vod: | |
| return ApiResponse.fail(40401, "影片不存在") | |
| return ApiResponse.ok({ | |
| "id": vod.id, "name": vod.name, "sub_name": vod.sub_name, | |
| "en_name": vod.en_name, "category_id": vod.category_id, | |
| "cover": vod.cover, "actor": vod.actor, "director": vod.director, | |
| "area": vod.area, "lang": vod.lang, "year": vod.year, | |
| "duration": vod.duration, "score": vod.score, | |
| "douban_id": vod.douban_id, "blurb": vod.blurb, | |
| "content": vod.content, "remarks": vod.remarks, | |
| "is_end": vod.is_end, "is_hidden": vod.is_hidden, | |
| "is_recommended": vod.is_recommended, "view_count": vod.view_count, | |
| }) | |
| async def update_vod( | |
| vod_id: int, | |
| req: VodUpdateRequest, | |
| admin: Admin = Depends(require_permission("vod:update")), | |
| db: AsyncSession = Depends(get_db), | |
| ): | |
| result = await db.execute(select(Vod).where(Vod.id == vod_id)) | |
| vod = result.scalar_one_or_none() | |
| if not vod: | |
| return ApiResponse.fail(40401, "影片不存在") | |
| for field, value in req.model_dump(exclude_unset=True).items(): | |
| setattr(vod, field, value) | |
| await invalidate_vod_cache(vod_id) | |
| return ApiResponse.ok() | |
| async def delete_vod( | |
| vod_id: int, | |
| admin: Admin = Depends(require_permission("vod:btn:delete")), | |
| db: AsyncSession = Depends(get_db), | |
| ): | |
| await db.execute(sa_delete(VodSource).where(VodSource.vod_id == vod_id)) | |
| await db.execute(sa_delete(Vod).where(Vod.id == vod_id)) | |
| await invalidate_vod_cache(vod_id) | |
| return ApiResponse.ok() | |
| async def toggle_hide( | |
| vod_id: int, | |
| admin: Admin = Depends(require_permission("vod:btn:hide")), | |
| db: AsyncSession = Depends(get_db), | |
| ): | |
| result = await db.execute(select(Vod).where(Vod.id == vod_id)) | |
| vod = result.scalar_one_or_none() | |
| if not vod: | |
| return ApiResponse.fail(40401, "影片不存在") | |
| vod.is_hidden = not vod.is_hidden | |
| await invalidate_vod_cache(vod_id) | |
| return ApiResponse.ok({"is_hidden": vod.is_hidden}) | |
| async def toggle_recommend( | |
| vod_id: int, | |
| admin: Admin = Depends(require_permission("vod:btn:recommend")), | |
| db: AsyncSession = Depends(get_db), | |
| ): | |
| result = await db.execute(select(Vod).where(Vod.id == vod_id)) | |
| vod = result.scalar_one_or_none() | |
| if not vod: | |
| return ApiResponse.fail(40401, "影片不存在") | |
| vod.is_recommended = not vod.is_recommended | |
| await invalidate_vod_cache(vod_id) | |
| return ApiResponse.ok({"is_recommended": vod.is_recommended}) |