Allow the renaming of the main section by temporal product permission.

Allow deleting sections which do not have tables, but have associated section printers.
This commit is contained in:
2021-09-04 13:08:46 +05:30
parent 63dfe05044
commit 5bea488181
2 changed files with 13 additions and 3 deletions

View File

@ -5,12 +5,13 @@ from typing import List
import barker.schemas.section as schemas
from fastapi import APIRouter, Depends, HTTPException, Security, status
from sqlalchemy import select
from sqlalchemy import delete, select
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
@ -44,7 +45,7 @@ def update_route(
try:
with SessionFuture() as db:
item: Section = db.execute(select(Section).where(Section.id == id_)).scalar_one()
if item.is_fixture:
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.",
@ -72,7 +73,12 @@ def delete_route(
status_code=status.HTTP_423_LOCKED,
detail=f"{item.name} is a fixture and cannot be edited or deleted.",
)
db.delete(item)
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()
except SQLAlchemyError as e: