2020-05-08 04:52:25 +00:00
|
|
|
import uuid
|
2020-10-07 15:18:43 +00:00
|
|
|
|
2020-05-08 04:52:25 +00:00
|
|
|
from typing import List, Optional
|
2020-05-10 10:35:39 +00:00
|
|
|
|
2020-10-07 15:18:43 +00:00
|
|
|
import brewman.schemas.master as schemas
|
|
|
|
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Security, status
|
2020-05-08 10:48:50 +00:00
|
|
|
from sqlalchemy.exc import SQLAlchemyError
|
2020-05-08 04:52:25 +00:00
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
|
2020-05-10 15:06:19 +00:00
|
|
|
from ..core.security import get_current_active_user as get_user
|
2020-05-08 04:52:25 +00:00
|
|
|
from ..db.session import SessionLocal
|
|
|
|
from ..models.master import CostCentre
|
2020-10-07 15:18:43 +00:00
|
|
|
from ..schemas.auth import UserToken
|
|
|
|
|
2020-05-21 19:45:25 +00:00
|
|
|
|
2020-05-08 04:52:25 +00:00
|
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
|
|
|
|
# Dependency
|
|
|
|
def get_db():
|
|
|
|
try:
|
|
|
|
db = SessionLocal()
|
|
|
|
yield db
|
|
|
|
finally:
|
|
|
|
db.close()
|
|
|
|
|
|
|
|
|
2020-05-11 20:01:21 +00:00
|
|
|
@router.post("", response_model=schemas.CostCentre)
|
2020-05-08 04:52:25 +00:00
|
|
|
def save(
|
2020-05-10 08:02:08 +00:00
|
|
|
data: schemas.CostCentreIn,
|
2020-05-08 04:52:25 +00:00
|
|
|
db: Session = Depends(get_db),
|
2020-05-10 15:06:19 +00:00
|
|
|
user: UserToken = Security(get_user, scopes=["cost-centres"]),
|
2020-05-08 04:52:25 +00:00
|
|
|
):
|
|
|
|
try:
|
2020-05-10 10:35:39 +00:00
|
|
|
item = CostCentre(name=data.name)
|
2020-05-08 04:52:25 +00:00
|
|
|
db.add(item)
|
|
|
|
db.commit()
|
2020-05-11 20:01:21 +00:00
|
|
|
return cost_centre_info(item)
|
2020-05-10 10:35:39 +00:00
|
|
|
except SQLAlchemyError as e:
|
|
|
|
db.rollback()
|
|
|
|
raise HTTPException(
|
2020-10-07 15:18:43 +00:00
|
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
|
|
detail=str(e),
|
2020-05-10 10:35:39 +00:00
|
|
|
)
|
2020-05-08 04:52:25 +00:00
|
|
|
except Exception:
|
|
|
|
db.rollback()
|
2020-06-01 03:31:31 +00:00
|
|
|
raise
|
2020-05-08 04:52:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
@router.put("/{id_}", response_model=schemas.CostCentre)
|
|
|
|
def update(
|
|
|
|
id_: uuid.UUID,
|
2020-05-10 08:02:08 +00:00
|
|
|
data: schemas.CostCentreIn,
|
2020-05-08 04:52:25 +00:00
|
|
|
db: Session = Depends(get_db),
|
2020-05-10 15:06:19 +00:00
|
|
|
user: UserToken = Security(get_user, scopes=["cost-centres"]),
|
2020-05-08 04:52:25 +00:00
|
|
|
):
|
|
|
|
try:
|
|
|
|
item = db.query(CostCentre).filter(CostCentre.id == id_).first()
|
|
|
|
if item.is_fixture:
|
|
|
|
raise HTTPException(
|
2020-10-07 15:18:43 +00:00
|
|
|
status_code=status.HTTP_423_LOCKED,
|
|
|
|
detail=f"{item.name} is a fixture and cannot be edited or deleted.",
|
2020-05-08 04:52:25 +00:00
|
|
|
)
|
|
|
|
item.name = data.name
|
|
|
|
db.commit()
|
2020-05-11 20:01:21 +00:00
|
|
|
return cost_centre_info(item)
|
2020-05-08 04:52:25 +00:00
|
|
|
except SQLAlchemyError as e:
|
|
|
|
db.rollback()
|
|
|
|
raise HTTPException(
|
2020-10-07 15:18:43 +00:00
|
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
|
|
detail=str(e),
|
2020-05-08 04:52:25 +00:00
|
|
|
)
|
|
|
|
except Exception:
|
|
|
|
db.rollback()
|
2020-06-01 03:31:31 +00:00
|
|
|
raise
|
|
|
|
|
2020-05-08 04:52:25 +00:00
|
|
|
|
|
|
|
@router.delete("/{id_}")
|
|
|
|
def delete(
|
2020-10-07 15:18:43 +00:00
|
|
|
id_: uuid.UUID,
|
|
|
|
db: Session = Depends(get_db),
|
|
|
|
user: UserToken = Security(get_user, scopes=["cost-centres"]),
|
2020-05-08 04:52:25 +00:00
|
|
|
):
|
|
|
|
try:
|
2020-06-30 06:02:09 +00:00
|
|
|
item: CostCentre = db.query(CostCentre).filter(CostCentre.id == id_).first()
|
2020-05-08 04:52:25 +00:00
|
|
|
|
|
|
|
if item is None:
|
|
|
|
raise HTTPException(
|
2020-10-07 15:18:43 +00:00
|
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
|
|
detail="Cost Centre not found",
|
2020-05-08 04:52:25 +00:00
|
|
|
)
|
|
|
|
elif item.is_fixture:
|
|
|
|
raise HTTPException(
|
2020-10-07 15:18:43 +00:00
|
|
|
status_code=status.HTTP_423_LOCKED,
|
|
|
|
detail=f"{item.name} is a fixture and cannot be edited or deleted.",
|
2020-05-08 04:52:25 +00:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
raise HTTPException(
|
2020-10-07 15:18:43 +00:00
|
|
|
status_code=status.HTTP_501_NOT_IMPLEMENTED,
|
|
|
|
detail="Cost Centre deletion not implemented",
|
2020-05-08 04:52:25 +00:00
|
|
|
)
|
|
|
|
except Exception:
|
|
|
|
db.rollback()
|
2020-06-01 03:31:31 +00:00
|
|
|
raise
|
2020-05-08 04:52:25 +00:00
|
|
|
|
|
|
|
|
2020-05-11 20:01:21 +00:00
|
|
|
@router.get("")
|
2020-05-08 04:52:25 +00:00
|
|
|
def show_blank(
|
2020-10-07 15:18:43 +00:00
|
|
|
db: Session = Depends(get_db),
|
|
|
|
user: UserToken = Security(get_user, scopes=["cost-centres"]),
|
2020-05-08 04:52:25 +00:00
|
|
|
):
|
2020-05-11 20:01:21 +00:00
|
|
|
return cost_centre_info(None)
|
2020-05-08 04:52:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
@router.get("/list", response_model=List[schemas.CostCentre])
|
2020-05-10 15:06:19 +00:00
|
|
|
async def show_list(db: Session = Depends(get_db), user: UserToken = Depends(get_user)):
|
2020-05-08 04:52:25 +00:00
|
|
|
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(
|
2020-10-07 15:18:43 +00:00
|
|
|
id_: uuid.UUID,
|
|
|
|
db: Session = Depends(get_db),
|
|
|
|
user: UserToken = Security(get_user, scopes=["cost-centres"]),
|
2020-05-08 04:52:25 +00:00
|
|
|
):
|
|
|
|
item = db.query(CostCentre).filter(CostCentre.id == id_).first()
|
2020-05-11 20:01:21 +00:00
|
|
|
return cost_centre_info(item)
|
2020-05-08 04:52:25 +00:00
|
|
|
|
|
|
|
|
2020-05-11 20:01:21 +00:00
|
|
|
def cost_centre_info(item: Optional[CostCentre]):
|
2020-05-08 04:52:25 +00:00
|
|
|
if item is None:
|
|
|
|
return {
|
|
|
|
"name": "",
|
|
|
|
"isFixture": False,
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
return {
|
|
|
|
"id": item.id,
|
|
|
|
"name": item.name,
|
|
|
|
"isFixture": item.is_fixture,
|
|
|
|
}
|