Spaces:
Paused
Paused
Download api/v1/workspace.py from 3SCError404/nexus: direct link, hf CLI and curl.
- Browser
- Download file 4.86 kB
-
https://huggingface.co/spaces/3SCError404/nexus/resolve/main/api/v1/workspace.py
- Command line
-
hf download hf://spaces/3SCError404/nexus/api/v1/workspace.py
-
curl -L -o workspace.py https://huggingface.co/spaces/3SCError404/nexus/resolve/main/api/v1/workspace.py
4.86 kB
| from typing import Any, List | |
| from fastapi import APIRouter, Depends, HTTPException | |
| 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.workspace import Workspace | |
| from schemas.workspace import WorkspaceUpdate, WorkspaceResponse, WorkspaceWithRole | |
| router = APIRouter() | |
| async def list_available_workspaces( | |
| current_user: User = Depends(get_current_user), | |
| db: AsyncSession = Depends(get_db) | |
| ) -> Any: | |
| """List all workspaces with user roles (Cached 15m).""" | |
| from core.cache import get_cached_object, set_cached_object | |
| cache_key = f"workspaces:user:{current_user.id}" | |
| # Check cache first | |
| cached_data = await get_cached_object(cache_key) | |
| if cached_data: | |
| return cached_data | |
| from models.user_workspace import UserWorkspace | |
| # 1. Global Super Admin visibility | |
| if current_user.role == UserRole.SC_ADMIN: | |
| stmt = select(Workspace).where( | |
| Workspace.deleted_at.is_(None), | |
| ~Workspace.name.ilike("SOFTDELETE%") | |
| ) | |
| result = await db.execute(stmt) | |
| workspaces = result.scalars().all() | |
| res_payload = [ | |
| { | |
| **WorkspaceResponse.model_validate(w).model_dump(), | |
| "role": UserRole.SC_ADMIN.value, | |
| "is_primary": (str(w.id) == current_user.workspace_id) | |
| } for w in workspaces | |
| ] | |
| await set_cached_object(cache_key, res_payload, ttl=900) | |
| return res_payload | |
| # 2. Association-based discovery for standard users | |
| stmt = ( | |
| select(Workspace, UserWorkspace.role, UserWorkspace.is_primary) | |
| .join(UserWorkspace, Workspace.id == UserWorkspace.workspace_id) | |
| .where( | |
| UserWorkspace.user_id == current_user.id, | |
| Workspace.deleted_at.is_(None) | |
| ) | |
| ) | |
| result = await db.execute(stmt) | |
| rows = result.all() | |
| res_payload = [ | |
| { | |
| **WorkspaceResponse.model_validate(row[0]).model_dump(), | |
| "role": row[1].value if hasattr(row[1], 'value') else row[1], | |
| "is_primary": row[2] | |
| } for row in rows | |
| ] | |
| # Cache the result for 15 minutes | |
| await set_cached_object(cache_key, res_payload, ttl=900) | |
| return res_payload | |
| async def get_my_workspace( | |
| current_user: User = Depends(get_current_user), | |
| db: AsyncSession = Depends(get_db) | |
| ) -> Any: | |
| """Get current workspace details (Cached).""" | |
| from core.cache import get_cached_object, set_cached_object | |
| cache_key = f"ws:meta:{current_user.workspace_id}" | |
| cached = await get_cached_object(cache_key) | |
| if cached: | |
| return cached | |
| stmt = select(Workspace).where( | |
| Workspace.id == current_user.workspace_id, | |
| Workspace.deleted_at.is_(None) | |
| ) | |
| result = await db.execute(stmt) | |
| workspace = result.scalar_one_or_none() | |
| if not workspace: | |
| raise HTTPException(status_code=404, detail="Workspace not found") | |
| # Cache for 1 hour (workspace data is slow-changing) | |
| response_data = WorkspaceResponse.model_validate(workspace) | |
| await set_cached_object(cache_key, response_data.model_dump(), ttl=3600) | |
| return response_data | |
| async def update_workspace( | |
| payload: WorkspaceUpdate, | |
| current_user: User = Depends(get_current_user), | |
| db: AsyncSession = Depends(get_db) | |
| ) -> Any: | |
| """Admin only: Update workspace settings/branding (Invalidates Cache).""" | |
| stmt = select(Workspace).where( | |
| Workspace.id == current_user.workspace_id, | |
| Workspace.deleted_at.is_(None) | |
| ) | |
| result = await db.execute(stmt) | |
| workspace = result.scalar_one_or_none() | |
| workspace.name = payload.name | |
| if payload.branding is not None: | |
| workspace.branding = payload.branding | |
| db.add(workspace) | |
| await db.commit() | |
| await db.refresh(workspace) | |
| # Invalidate Cache | |
| from core.cache import invalidate_cache | |
| await invalidate_cache(f"ws:meta:{current_user.workspace_id}") | |
| return workspace | |
| async def get_workspace_branding( | |
| current_user: User = Depends(get_current_user), | |
| db: AsyncSession = Depends(get_db) | |
| ) -> Any: | |
| """Get standalone branding config (logo, colors).""" | |
| stmt = select(Workspace).where( | |
| Workspace.id == current_user.workspace_id, | |
| Workspace.deleted_at.is_(None) | |
| ) | |
| result = await db.execute(stmt) | |
| workspace = result.scalar_one_or_none() | |
| return workspace.branding or {} | |