barker/barker/barker/routers/section.py

120 lines
3.9 KiB
Python
Raw Normal View History

2019-06-22 20:05:00 +00:00
import uuid
from typing import List
2020-06-15 16:10:12 +00:00
import barker.schemas.section as schemas
from fastapi import APIRouter, Depends, HTTPException, Security, status
from sqlalchemy import delete, select
2020-06-15 16:10:12 +00:00
from sqlalchemy.exc import SQLAlchemyError
from ..core.security import get_current_active_user as get_user
from ..db.session import SessionFuture
from ..models.section import Section
from ..models.section_printer import SectionPrinter
from ..schemas.user_token import UserToken
2020-06-15 16:10:12 +00:00
router = APIRouter()
2020-09-22 02:29:14 +00:00
@router.post("", response_model=schemas.Section)
2020-06-15 16:10:12 +00:00
def save(
data: schemas.SectionIn,
user: UserToken = Security(get_user, scopes=["sections"]),
) -> schemas.Section:
2020-06-15 16:10:12 +00:00
try:
with SessionFuture() as db:
item = Section(name=data.name)
db.add(item)
db.commit()
return section_info(item)
2020-06-15 16:10:12 +00:00
except SQLAlchemyError as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=str(e),
2020-06-15 16:10:12 +00:00
)
@router.put("/{id_}", response_model=schemas.Section)
def update_route(
2020-06-15 16:10:12 +00:00
id_: uuid.UUID,
data: schemas.SectionIn,
user: UserToken = Security(get_user, scopes=["sections"]),
) -> schemas.Section:
2020-06-15 16:10:12 +00:00
try:
with SessionFuture() as db:
item: Section = db.execute(select(Section).where(Section.id == id_)).scalar_one()
if item.is_fixture and "temporal-products" not in user.permissions:
raise HTTPException(
status_code=status.HTTP_423_LOCKED,
detail=f"{item.name} is a fixture and cannot be edited or deleted.",
)
item.name = data.name
db.commit()
return section_info(item)
2020-06-15 16:10:12 +00:00
except SQLAlchemyError as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=str(e),
2020-06-15 16:10:12 +00:00
)
@router.delete("/{id_}", response_model=schemas.SectionBlank)
def delete_route(
id_: uuid.UUID,
user: UserToken = Security(get_user, scopes=["sections"]),
) -> schemas.SectionBlank:
2020-06-15 16:10:12 +00:00
try:
with SessionFuture() as db:
item: Section = db.execute(select(Section).where(Section.id == id_)).scalar_one()
if item.is_fixture:
raise HTTPException(
status_code=status.HTTP_423_LOCKED,
detail=f"{item.name} is a fixture and cannot be edited or deleted.",
)
db.execute(
delete(SectionPrinter)
.where(SectionPrinter.section_id == id_)
.execution_options(synchronize_session=False)
)
db.execute(delete(Section).where(Section.id == id_).execution_options(synchronize_session=False))
db.commit()
return section_blank()
2020-06-15 16:10:12 +00:00
except SQLAlchemyError as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=str(e),
2020-06-15 16:10:12 +00:00
)
@router.get("", response_model=schemas.SectionBlank)
2020-06-15 16:10:12 +00:00
def show_blank(
user: UserToken = Security(get_user, scopes=["sections"]),
) -> schemas.SectionBlank:
return section_blank()
2020-06-15 16:10:12 +00:00
@router.get("/list", response_model=List[schemas.Section])
def show_list(user: UserToken = Depends(get_user)):
with SessionFuture() as db:
return [section_info(item) for item in db.execute(select(Section).order_by(Section.name)).scalars().all()]
2020-06-15 16:10:12 +00:00
@router.get("/{id_}", response_model=schemas.Section)
2020-06-15 16:10:12 +00:00
def show_id(
id_: uuid.UUID,
user: UserToken = Security(get_user, scopes=["sections"]),
) -> schemas.Section:
with SessionFuture() as db:
item: Section = db.execute(select(Section).where(Section.id == id_)).scalar_one()
return section_info(item)
2020-06-15 16:10:12 +00:00
def section_info(item: Section) -> schemas.Section:
return schemas.Section(id=item.id, name=item.name)
def section_blank() -> schemas.SectionBlank:
return schemas.SectionBlank(name="")