Adding sub-parts
This commit is contained in:
1 parent
45bc5bf8f6
commit
c8696e67b5
14 files changed
+436
-2
No files matched your search
@@ -7,6 +7,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from app.core.database import get_db
|
||||
from app.crud import audit as audit_crud
|
||||
from app.crud import iso_thread as iso_thread_crud
|
||||
from app.crud import sub_part_type as sub_part_type_crud
|
||||
from app.crud import manufacturer as manufacturer_crud
|
||||
from app.crud import part_type as part_type_crud
|
||||
from app.crud import role as role_crud
|
||||
@@ -890,6 +891,122 @@ async def delete_part_type(
|
||||
return Response(status_code=status.HTTP_200_OK)
|
||||
|
||||
|
||||
# --- Sub Part Types ----------------------------------------------------------
|
||||
|
||||
|
||||
@router.get("/sub-part-types", response_class=HTMLResponse)
|
||||
async def sub_part_types_page(
|
||||
request: Request,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
user: User = Depends(web_require("sub_part_types:read")),
|
||||
):
|
||||
return templates.TemplateResponse(
|
||||
request,
|
||||
"admin/sub_part_types.html",
|
||||
{
|
||||
"user": user,
|
||||
"sub_part_types": await sub_part_type_crud.list_all(db),
|
||||
"part_types": await part_type_crud.list_all(db),
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@router.post("/sub-part-types", response_class=HTMLResponse)
|
||||
async def create_sub_part_type(
|
||||
request: Request,
|
||||
name: str = Form(...),
|
||||
part_type_id: int = Form(...),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
actor: User = Depends(web_require("sub_part_types:write")),
|
||||
):
|
||||
if not await sub_part_type_crud.get_by_name_and_part_type(db, name, part_type_id):
|
||||
created = await sub_part_type_crud.create(db, name=name, part_type_id=part_type_id)
|
||||
await audit_crud.log(
|
||||
db, actor=actor, action="create", resource_type="SubPartType",
|
||||
resource_id=created.id,
|
||||
description=audit_crud.describe_create("SubPartType", created.id, created.name),
|
||||
)
|
||||
return templates.TemplateResponse(
|
||||
request,
|
||||
"admin/_sub_part_types_table.html",
|
||||
{"sub_part_types": await sub_part_type_crud.list_all(db)},
|
||||
)
|
||||
|
||||
|
||||
@router.get("/sub-part-types/{sub_part_type_id}/edit", response_class=HTMLResponse)
|
||||
async def edit_sub_part_type_row(
|
||||
request: Request,
|
||||
sub_part_type_id: int,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
_: User = Depends(web_require("sub_part_types:write")),
|
||||
):
|
||||
spt = await sub_part_type_crud.get(db, sub_part_type_id)
|
||||
if spt is None:
|
||||
raise HTTPException(status_code=404, detail="Sub part type not found")
|
||||
return templates.TemplateResponse(
|
||||
request,
|
||||
"admin/_sub_part_type_row_edit.html",
|
||||
{"spt": spt, "part_types": await part_type_crud.list_all(db)},
|
||||
)
|
||||
|
||||
|
||||
@router.get("/sub-part-types/{sub_part_type_id}/row", response_class=HTMLResponse)
|
||||
async def sub_part_type_row(
|
||||
request: Request,
|
||||
sub_part_type_id: int,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
_: User = Depends(web_require("sub_part_types:read")),
|
||||
):
|
||||
spt = await sub_part_type_crud.get(db, sub_part_type_id)
|
||||
if spt is None:
|
||||
raise HTTPException(status_code=404, detail="Sub part type not found")
|
||||
return templates.TemplateResponse(
|
||||
request, "admin/_sub_part_type_row.html", {"spt": spt}
|
||||
)
|
||||
|
||||
|
||||
@router.post("/sub-part-types/{sub_part_type_id}", response_class=HTMLResponse)
|
||||
async def update_sub_part_type(
|
||||
request: Request,
|
||||
sub_part_type_id: int,
|
||||
name: str = Form(...),
|
||||
part_type_id: int = Form(...),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
actor: User = Depends(web_require("sub_part_types:write")),
|
||||
):
|
||||
spt = await sub_part_type_crud.get(db, sub_part_type_id)
|
||||
if spt is None:
|
||||
raise HTTPException(status_code=404, detail="Sub part type not found")
|
||||
changes = {"name": name, "part_type_id": part_type_id}
|
||||
await sub_part_type_crud.update(db, spt, changes)
|
||||
await audit_crud.log(
|
||||
db, actor=actor, action="update", resource_type="SubPartType",
|
||||
resource_id=sub_part_type_id,
|
||||
description=audit_crud.describe_update("SubPartType", sub_part_type_id, changes),
|
||||
)
|
||||
return templates.TemplateResponse(
|
||||
request, "admin/_sub_part_type_row.html", {"spt": spt}
|
||||
)
|
||||
|
||||
|
||||
@router.delete("/sub-part-types/{sub_part_type_id}")
|
||||
async def delete_sub_part_type(
|
||||
sub_part_type_id: int,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
actor: User = Depends(web_require("sub_part_types:delete")),
|
||||
):
|
||||
spt = await sub_part_type_crud.get(db, sub_part_type_id)
|
||||
if spt is not None:
|
||||
name = spt.name
|
||||
await sub_part_type_crud.delete(db, spt)
|
||||
await audit_crud.log(
|
||||
db, actor=actor, action="delete", resource_type="SubPartType",
|
||||
resource_id=sub_part_type_id,
|
||||
description=audit_crud.describe_delete("SubPartType", sub_part_type_id, name),
|
||||
)
|
||||
return Response(status_code=status.HTTP_200_OK)
|
||||
|
||||
|
||||
# --- Audit Log ---------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user