Files
brewman/brewman/routers/cost_centre.py
tanshu aae48faf91 Renamed service points to points consistently
Renamed Schemas to shorten them
Added the name validators for Account Base
Added joining_date/leaving_date validators for employees
Employees should be working now
2020-05-10 13:32:08 +05:30

144 lines
4.1 KiB
Python

import traceback
import uuid
from typing import List, Optional
from fastapi import APIRouter, HTTPException, status, Depends, Security
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm import Session
import brewman.schemas.master as schemas
from ..core.security import User, get_current_active_user as get_user
from ..db.session import SessionLocal
from ..models.master import CostCentre
router = APIRouter()
# Dependency
def get_db():
try:
db = SessionLocal()
yield db
finally:
db.close()
@router.post("/", response_model=schemas.CostCentre)
def save(
data: schemas.CostCentreIn,
db: Session = Depends(get_db),
user: User = Security(get_user, scopes=["cost-centres"]),
):
try:
item = CostCentre(data.name)
db.add(item)
db.commit()
return cost_centre_info(item, db)
except Exception:
db.rollback()
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=traceback.format_exc(),
)
@router.put("/{id_}", response_model=schemas.CostCentre)
def update(
id_: uuid.UUID,
data: schemas.CostCentreIn,
db: Session = Depends(get_db),
user: User = Security(get_user, scopes=["cost-centres"]),
):
try:
item = db.query(CostCentre).filter(CostCentre.id == id_).first()
if item.is_fixture:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"{item.name} is a fixture and cannot be edited or deleted.",
)
item.name = data.name
db.commit()
return cost_centre_info(item, db)
except SQLAlchemyError as e:
db.rollback()
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=str(e),
)
except Exception:
db.rollback()
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=traceback.format_exc(),
)
@router.delete("/{id_}")
def delete(
id_: uuid.UUID,
db: Session = Depends(get_db),
user: User = Security(get_user, scopes=["cost-centres"]),
):
try:
item = db.query(CostCentre).filter(CostCentre.id == id_).first()
if item is None:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Cost Centre not found",
)
elif item.is_fixture:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"{item.name} is a fixture and cannot be edited or deleted.",
)
else:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Cost Centre deletion not implemented",
)
except Exception:
db.rollback()
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=traceback.format_exc(),
)
@router.get("/")
def show_blank(
db: Session = Depends(get_db),
user: User = Security(get_user, scopes=["cost-centres"]),
):
return cost_centre_info(None, db)
@router.get("/list", response_model=List[schemas.CostCentre])
async def show_list(db: Session = Depends(get_db), user: User = Depends(get_user)):
return [
{"id": item.id, "name": item.name, "isFixture": item.is_fixture}
for item in db.query(CostCentre).order_by(CostCentre.name).all()
]
@router.get("/{id_}", response_model=schemas.CostCentre)
def show_id(
id_: uuid.UUID,
db: Session = Depends(get_db),
user: User = Security(get_user, scopes=["cost-centres"]),
):
item = db.query(CostCentre).filter(CostCentre.id == id_).first()
return cost_centre_info(item, db)
def cost_centre_info(item: Optional[CostCentre], db: Session):
if item is None:
return {
"name": "",
"isFixture": False,
}
else:
return {
"id": item.id,
"name": item.name,
"isFixture": item.is_fixture,
}