Download app/api/v1/dashboards.py from isold/meloon: direct link, hf CLI and curl.
- Browser
- Download file 2.08 kB
-
https://huggingface.co/spaces/isold/meloon/resolve/main/app/api/v1/dashboards.py
- Command line
-
hf download hf://spaces/isold/meloon/app/api/v1/dashboards.py
-
curl -L -o dashboards.py https://huggingface.co/spaces/isold/meloon/resolve/main/app/api/v1/dashboards.py
2.08 kB
| from fastapi import APIRouter, Depends, HTTPException, status | |
| from sqlalchemy.orm import Session | |
| from typing import List | |
| from app.api import deps | |
| from app.models.models import Dashboard, DashboardItem, User, DataSource | |
| from app.schemas import schemas | |
| router = APIRouter() | |
| def read_dashboards( | |
| db: Session = Depends(deps.get_db), | |
| current_user: User = Depends(deps.get_current_user), | |
| skip: int = 0, | |
| limit: int = 100, | |
| ): | |
| dashboards = db.query(Dashboard).filter(Dashboard.user_id == current_user.id).offset(skip).limit(limit).all() | |
| return dashboards | |
| def create_dashboard( | |
| dashboard_in: schemas.DashboardCreate, | |
| db: Session = Depends(deps.get_db), | |
| current_user: User = Depends(deps.get_current_user), | |
| ): | |
| dashboard = Dashboard( | |
| **dashboard_in.model_dump(), | |
| user_id=current_user.id | |
| ) | |
| db.add(dashboard) | |
| db.commit() | |
| db.refresh(dashboard) | |
| return dashboard | |
| def add_dashboard_item( | |
| dashboard_id: int, | |
| item_in: schemas.DashboardItemCreate, | |
| db: Session = Depends(deps.get_db), | |
| current_user: User = Depends(deps.get_current_user), | |
| ): | |
| # Verify Dashboard ownership | |
| dashboard = db.query(Dashboard).filter(Dashboard.id == dashboard_id, Dashboard.user_id == current_user.id).first() | |
| if not dashboard: | |
| raise HTTPException(status_code=404, detail="Dashboard not found") | |
| # Verify DataSource ownership/access | |
| ds = db.query(DataSource).filter(DataSource.id == item_in.data_source_id, DataSource.user_id == current_user.id).first() | |
| if not ds: | |
| raise HTTPException(status_code=404, detail="Data source not found") | |
| item = DashboardItem( | |
| **item_in.model_dump(), | |
| dashboard_id=dashboard_id | |
| ) | |
| db.add(item) | |
| db.commit() | |
| db.refresh(item) | |
| return item | |