Spaces:
Paused
Paused
Download api/v1/projects.py from 3SCError404/miracle: direct link, hf CLI and curl.
- Browser
- Download file 4.43 kB
-
https://huggingface.co/spaces/3SCError404/miracle/resolve/main/api/v1/projects.py
- Command line
-
hf download hf://spaces/3SCError404/miracle/api/v1/projects.py
-
curl -L -o projects.py https://huggingface.co/spaces/3SCError404/miracle/resolve/main/api/v1/projects.py
4.43 kB
| """ | |
| Project endpoints: CRUD for projects within a workspace. | |
| """ | |
| from datetime import datetime | |
| from typing import Any, List, Optional | |
| from uuid import UUID | |
| from fastapi import APIRouter, Depends, HTTPException, status | |
| from sqlalchemy.ext.asyncio import AsyncSession | |
| from sqlalchemy import select | |
| from core.deps import get_db, get_current_user | |
| from core.permissions import require_roles, WORKSPACE_ADMIN_ROLES | |
| from models.user import User, UserRole | |
| from models.project import Project | |
| from schemas.project import ProjectCreate, ProjectResponse, ProjectUpdate | |
| projects_router = APIRouter() | |
| async def create_project( | |
| payload: ProjectCreate, | |
| current_user: User = Depends(get_current_user), | |
| db: AsyncSession = Depends(get_db) | |
| ) -> Any: | |
| """Admin only: create a new project in the workspace.""" | |
| new_project = Project( | |
| name=payload.name, | |
| description=payload.description, | |
| workspace_id=current_user.workspace_id | |
| ) | |
| db.add(new_project) | |
| await db.commit() | |
| await db.refresh(new_project) | |
| return new_project | |
| async def list_projects( | |
| workspace_id: Optional[str] = None, | |
| skip: int = 0, limit: int = 100, | |
| current_user: User = Depends(get_current_user), | |
| db: AsyncSession = Depends(get_db) | |
| ) -> Any: | |
| """List all projects in the workspace (Many-to-Many).""" | |
| # 1. Global Visibility for Super Admin | |
| is_global = (workspace_id == "all" and current_user.role == UserRole.SC_ADMIN) | |
| stmt = select(Project).where(Project.deleted_at.is_(None)) | |
| if not is_global: | |
| target_ws = workspace_id if (workspace_id and current_user.role == UserRole.SC_ADMIN) else current_user.workspace_id | |
| stmt = stmt.where(Project.workspace_id == target_ws) | |
| stmt = stmt.offset(skip).limit(limit) | |
| result = await db.execute(stmt) | |
| return result.scalars().all() | |
| async def get_project( | |
| project_id: str, | |
| current_user: User = Depends(get_current_user), | |
| db: AsyncSession = Depends(get_db) | |
| ) -> Any: | |
| """Get a specific project.""" | |
| stmt = select(Project).where( | |
| Project.id == project_id, | |
| Project.workspace_id == current_user.workspace_id, | |
| Project.deleted_at.is_(None) | |
| ) | |
| result = await db.execute(stmt) | |
| project = result.scalar_one_or_none() | |
| if not project: | |
| raise HTTPException(status_code=404, detail="Project not found") | |
| return project | |
| async def update_project( | |
| project_id: UUID, | |
| payload: ProjectUpdate, | |
| current_user: User = Depends(get_current_user), | |
| db: AsyncSession = Depends(get_db) | |
| ) -> Any: | |
| """Admin only: update project details.""" | |
| stmt = select(Project).where( | |
| Project.id == project_id, | |
| Project.workspace_id == current_user.workspace_id, | |
| Project.deleted_at.is_(None) | |
| ) | |
| result = await db.execute(stmt) | |
| project = result.scalar_one_or_none() | |
| if not project: | |
| raise HTTPException(status_code=404, detail="Project not found") | |
| for field, value in payload.model_dump(exclude_unset=True).items(): | |
| setattr(project, field, value) | |
| db.add(project) | |
| await db.commit() | |
| await db.refresh(project) | |
| return project | |
| async def delete_project( | |
| project_id: UUID, | |
| current_user: User = Depends(get_current_user), | |
| db: AsyncSession = Depends(get_db) | |
| ) -> Any: | |
| """Admin only: Soft Delete a project.""" | |
| stmt = select(Project).where( | |
| Project.id == project_id, | |
| Project.workspace_id == current_user.workspace_id, | |
| Project.deleted_at.is_(None) | |
| ) | |
| result = await db.execute(stmt) | |
| project = result.scalar_one_or_none() | |
| if not project: | |
| raise HTTPException(status_code=404, detail="Project not found") | |
| project.deleted_at = datetime.utcnow() | |
| project.deleted_by_id = current_user.id | |
| db.add(project) | |
| await db.commit() | |
| return {"status": "deleted"} | |