165 lines
5.3 KiB
Python
165 lines
5.3 KiB
Python
import uuid
|
|
from typing import Optional, List
|
|
|
|
from fastapi import APIRouter, HTTPException, status, Depends, Security
|
|
from sqlalchemy import and_
|
|
from sqlalchemy.exc import SQLAlchemyError
|
|
from sqlalchemy.orm import Session
|
|
from sqlalchemy.dialects.postgresql import insert as pg_insert
|
|
|
|
from ..schemas.auth import UserToken
|
|
import barker.schemas.master as schemas
|
|
from ..core.security import get_current_active_user as get_user
|
|
from ..db.session import SessionLocal
|
|
from ..models.master import SectionPrinter, MenuCategory
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
# Dependency
|
|
def get_db():
|
|
try:
|
|
db = SessionLocal()
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
@router.post("/{id_}", response_model=List[schemas.SectionPrinter])
|
|
def save(
|
|
id_: uuid.UUID,
|
|
data: List[schemas.SectionPrinter],
|
|
db: Session = Depends(get_db),
|
|
user: UserToken = Security(get_user, scopes=["section-printers"]),
|
|
):
|
|
try:
|
|
current = []
|
|
for mcs in data:
|
|
if mcs.menu_category.id_ is None and mcs.printer.id_ is None:
|
|
raise ValueError("Please choose a default printer")
|
|
if mcs.printer.id_ is None:
|
|
continue
|
|
stmt = (
|
|
pg_insert(SectionPrinter.__table__)
|
|
.values(
|
|
id=uuid.uuid4(),
|
|
menu_category_id=mcs.menu_category.id_ if mcs.menu_category is not None else None,
|
|
section_id=id_,
|
|
printer_id=mcs.printer.id_,
|
|
copies=mcs.copies,
|
|
)
|
|
.on_conflict_do_update(
|
|
index_elements=["menu_category_id", "section_id"],
|
|
set_=dict(printer_id=mcs.printer.id_, copies=mcs.copies),
|
|
)
|
|
)
|
|
db.execute(stmt)
|
|
current.append(mcs.menu_category.id_)
|
|
db.execute(
|
|
SectionPrinter.__table__.delete(
|
|
and_(
|
|
SectionPrinter.section_id == id_,
|
|
SectionPrinter.menu_category_id != None,
|
|
~SectionPrinter.menu_category_id.in_([x for x in current if x is not None]),
|
|
)
|
|
)
|
|
)
|
|
db.commit()
|
|
|
|
return report(id_, 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
|
|
|
|
|
|
@router.delete("/{id_}")
|
|
def delete(
|
|
id_: uuid.UUID, db: Session = Depends(get_db), user: UserToken = Security(get_user, scopes=["section-printers"]),
|
|
):
|
|
try:
|
|
SectionPrinter.__table__.delete(SectionPrinter.section_id == id_)
|
|
db.commit()
|
|
return report(id_, 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
|
|
|
|
|
|
@router.get("")
|
|
def show_blank(
|
|
db: Session = Depends(get_db), user: UserToken = Security(get_user, scopes=["section-printers"]),
|
|
):
|
|
return report(None, db)
|
|
|
|
|
|
@router.get("/{id_}")
|
|
def show_id(
|
|
id_: uuid.UUID, db: Session = Depends(get_db), user: UserToken = Security(get_user, scopes=["section-printers"]),
|
|
):
|
|
return report(id_, db)
|
|
|
|
|
|
# @view_config(
|
|
# request_method="GET",
|
|
# route_name="v1_section_printers_id",
|
|
# renderer="json",
|
|
# request_param="m",
|
|
# permission="Authenticated",
|
|
# )
|
|
# def show_details(request):
|
|
# section_id = uuid.UUID(request.matchdict["section_id"])
|
|
# menu_category_id = request.GET.get("m", None)
|
|
# if menu_category_id is not None:
|
|
# menu_category_id = uuid.UUID(menu_category_id)
|
|
# item = (
|
|
# request.dbsession.query(SectionPrinter)
|
|
# .filter(SectionPrinter.section_id == section_id, SectionPrinter.menu_category_id == menu_category_id,)
|
|
# .first()
|
|
# )
|
|
# return section_printer_info(item, request.dbsession)
|
|
|
|
|
|
def report(section_id: uuid.UUID, db: Session):
|
|
menu_categories = db.query(MenuCategory.id, MenuCategory.name).filter(MenuCategory.is_active == True).order_by(MenuCategory.sort_order).all()
|
|
list_ = []
|
|
for item in [(None,)] + menu_categories:
|
|
mc = (
|
|
db.query(SectionPrinter)
|
|
.filter(SectionPrinter.section_id == section_id, SectionPrinter.menu_category_id == item[0])
|
|
.first()
|
|
)
|
|
list_.append(
|
|
{
|
|
"menuCategory": {} if item[0] is None else {"id": item[0], "name": item[1]},
|
|
"printer": {} if mc is None else {"id": mc.printer_id},
|
|
"copies": 0 if mc is None else mc.copies,
|
|
}
|
|
)
|
|
return list_
|
|
|
|
|
|
def section_printer_info(item: Optional[SectionPrinter]):
|
|
if item is None:
|
|
return {"menuCategory": {}, "section": {}, "printer": {}, "copies": 0}
|
|
# if type(item) is uuid.UUID:
|
|
# item = db.query(SectionPrinter).filter(SectionPrinter.id == item).first()
|
|
return {
|
|
"id": item.id,
|
|
"menuCategory": None
|
|
if item.menu_category is None
|
|
else {"id": item.menu_category.id, "name": item.menu_category.name},
|
|
"section": {"id": item.section.id, "name": item.section.name},
|
|
"printer": {"id": item.printer.id, "name": item.printer.name},
|
|
"copies": item.copies,
|
|
}
|