from typing import Any, List from fastapi import APIRouter, Depends, HTTPException from sqlalchemy.orm import Session from app import crud, models, schemas from app.api import deps router = APIRouter() @router.get("/", response_model=List[schemas.Organization]) def read_organizations( db: Session = Depends(deps.get_db), skip: int = 0, limit: int = 100, current_user: models.User = Depends(deps.get_current_active_user), ) -> Any: """ Retrieve organizations. """ if crud.user.is_superuser(current_user): organizations = crud.organization.get_multi(db, skip=skip, limit=limit) elif current_user.organization_id: organizations = [crud.organization.get(db, id=current_user.organization_id)] else: organizations = [] return organizations @router.post("/", response_model=schemas.Organization) def create_organization( *, db: Session = Depends(deps.get_db), organization_in: schemas.OrganizationCreate, current_user: models.User = Depends(deps.get_current_active_superuser), ) -> Any: """ Create new organization. """ organization = crud.organization.create(db, obj_in=organization_in) return organization @router.get("/{id}", response_model=schemas.Organization) def read_organization( *, db: Session = Depends(deps.get_db), id: int, current_user: models.User = Depends(deps.get_current_active_user), ) -> Any: """ Get organization by ID. """ organization = crud.organization.get(db, id=id) if not organization: raise HTTPException(status_code=404, detail="Organization not found") if not crud.user.is_superuser(current_user) and ( current_user.organization_id != id ): raise HTTPException(status_code=400, detail="Not enough permissions") return organization @router.put("/{id}", response_model=schemas.Organization) def update_organization( *, db: Session = Depends(deps.get_db), id: int, organization_in: schemas.OrganizationUpdate, current_user: models.User = Depends(deps.get_current_active_user), ) -> Any: """ Update an organization. """ organization = crud.organization.get(db, id=id) if not organization: raise HTTPException(status_code=404, detail="Organization not found") if not crud.user.is_superuser(current_user) and ( current_user.organization_id != id ): raise HTTPException(status_code=400, detail="Not enough permissions") organization = crud.organization.update( db, db_obj=organization, obj_in=organization_in ) return organization @router.delete("/{id}", response_model=schemas.Organization) def delete_organization( *, db: Session = Depends(deps.get_db), id: int, current_user: models.User = Depends(deps.get_current_active_superuser), ) -> Any: """ Delete an organization. """ organization = crud.organization.get(db, id=id) if not organization: raise HTTPException(status_code=404, detail="Organization not found") organization = crud.organization.remove(db, id=id) return organization