Adding augmented parts
This commit is contained in:
1 parent
2e7002b73b
commit
00fb43c8ed
14 files changed
+590
-2
No files matched your search
@@ -6,6 +6,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 augmented_part as augmented_part_crud
|
||||
from app.crud import imported_part as imported_part_crud
|
||||
from app.crud import imported_part_config as imported_part_config_crud
|
||||
from app.crud import iso_thread as iso_thread_crud
|
||||
@@ -1119,6 +1120,153 @@ async def refresh_imported_parts(
|
||||
)
|
||||
|
||||
|
||||
# --- Augmented Parts ---------------------------------------------------------
|
||||
|
||||
|
||||
async def _augmented_part_form_ctx(db: AsyncSession) -> dict:
|
||||
return {
|
||||
"part_colours": await part_colour_crud.list_all(db),
|
||||
"manufacturers": await manufacturer_crud.list_all(db),
|
||||
"scope_models": await scope_model_crud.list_all(db),
|
||||
"part_types": await part_type_crud.list_all(db),
|
||||
"sub_part_types": await sub_part_type_crud.list_all(db),
|
||||
}
|
||||
|
||||
|
||||
@router.get("/augmented-parts", response_class=HTMLResponse)
|
||||
async def augmented_parts_page(
|
||||
request: Request,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
user: User = Depends(web_require("augmented_parts:read")),
|
||||
):
|
||||
ctx = await _augmented_part_form_ctx(db)
|
||||
ctx["user"] = user
|
||||
ctx["augmented_parts"] = await augmented_part_crud.list_all(db)
|
||||
return templates.TemplateResponse(request, "admin/augmented_parts.html", ctx)
|
||||
|
||||
|
||||
@router.post("/augmented-parts", response_class=HTMLResponse)
|
||||
async def create_augmented_part(
|
||||
request: Request,
|
||||
sku: str = Form(""),
|
||||
description: str = Form(...),
|
||||
colour_id: int = Form(...),
|
||||
manufacturer_id: int = Form(...),
|
||||
scope_model_id: str = Form(""),
|
||||
part_type_id: int = Form(...),
|
||||
sub_part_type_id: str = Form(""),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
actor: User = Depends(web_require("augmented_parts:write")),
|
||||
):
|
||||
created = await augmented_part_crud.create(
|
||||
db,
|
||||
sku=sku.strip() or None,
|
||||
description=description,
|
||||
colour_id=colour_id,
|
||||
manufacturer_id=manufacturer_id,
|
||||
scope_model_id=int(scope_model_id) if scope_model_id else None,
|
||||
part_type_id=part_type_id,
|
||||
sub_part_type_id=int(sub_part_type_id) if sub_part_type_id else None,
|
||||
)
|
||||
await audit_crud.log(
|
||||
db, actor=actor, action="create", resource_type="AugmentedPart",
|
||||
resource_id=created.id,
|
||||
description=audit_crud.describe_create(
|
||||
"AugmentedPart", created.id, created.sku or created.description
|
||||
),
|
||||
)
|
||||
return templates.TemplateResponse(
|
||||
request,
|
||||
"admin/_augmented_parts_table.html",
|
||||
{"augmented_parts": await augmented_part_crud.list_all(db)},
|
||||
)
|
||||
|
||||
|
||||
@router.get("/augmented-parts/{augmented_part_id}/edit", response_class=HTMLResponse)
|
||||
async def edit_augmented_part_row(
|
||||
request: Request,
|
||||
augmented_part_id: int,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
_: User = Depends(web_require("augmented_parts:write")),
|
||||
):
|
||||
ap = await augmented_part_crud.get(db, augmented_part_id)
|
||||
if ap is None:
|
||||
raise HTTPException(status_code=404, detail="Augmented part not found")
|
||||
ctx = await _augmented_part_form_ctx(db)
|
||||
ctx["ap"] = ap
|
||||
return templates.TemplateResponse(request, "admin/_augmented_part_row_edit.html", ctx)
|
||||
|
||||
|
||||
@router.get("/augmented-parts/{augmented_part_id}/row", response_class=HTMLResponse)
|
||||
async def augmented_part_row(
|
||||
request: Request,
|
||||
augmented_part_id: int,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
_: User = Depends(web_require("augmented_parts:read")),
|
||||
):
|
||||
ap = await augmented_part_crud.get(db, augmented_part_id)
|
||||
if ap is None:
|
||||
raise HTTPException(status_code=404, detail="Augmented part not found")
|
||||
return templates.TemplateResponse(
|
||||
request, "admin/_augmented_part_row.html", {"ap": ap}
|
||||
)
|
||||
|
||||
|
||||
@router.post("/augmented-parts/{augmented_part_id}", response_class=HTMLResponse)
|
||||
async def update_augmented_part(
|
||||
request: Request,
|
||||
augmented_part_id: int,
|
||||
sku: str = Form(""),
|
||||
description: str = Form(...),
|
||||
colour_id: int = Form(...),
|
||||
manufacturer_id: int = Form(...),
|
||||
scope_model_id: str = Form(""),
|
||||
part_type_id: int = Form(...),
|
||||
sub_part_type_id: str = Form(""),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
actor: User = Depends(web_require("augmented_parts:write")),
|
||||
):
|
||||
ap = await augmented_part_crud.get(db, augmented_part_id)
|
||||
if ap is None:
|
||||
raise HTTPException(status_code=404, detail="Augmented part not found")
|
||||
changes = {
|
||||
"sku": sku.strip() or None,
|
||||
"description": description,
|
||||
"colour_id": colour_id,
|
||||
"manufacturer_id": manufacturer_id,
|
||||
"scope_model_id": int(scope_model_id) if scope_model_id else None,
|
||||
"part_type_id": part_type_id,
|
||||
"sub_part_type_id": int(sub_part_type_id) if sub_part_type_id else None,
|
||||
}
|
||||
await augmented_part_crud.update(db, ap, changes)
|
||||
await audit_crud.log(
|
||||
db, actor=actor, action="update", resource_type="AugmentedPart",
|
||||
resource_id=augmented_part_id,
|
||||
description=audit_crud.describe_update("AugmentedPart", augmented_part_id, changes),
|
||||
)
|
||||
return templates.TemplateResponse(
|
||||
request, "admin/_augmented_part_row.html", {"ap": ap}
|
||||
)
|
||||
|
||||
|
||||
@router.delete("/augmented-parts/{augmented_part_id}")
|
||||
async def delete_augmented_part(
|
||||
augmented_part_id: int,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
actor: User = Depends(web_require("augmented_parts:delete")),
|
||||
):
|
||||
ap = await augmented_part_crud.get(db, augmented_part_id)
|
||||
if ap is not None:
|
||||
label = ap.sku or ap.description
|
||||
await augmented_part_crud.delete(db, ap)
|
||||
await audit_crud.log(
|
||||
db, actor=actor, action="delete", resource_type="AugmentedPart",
|
||||
resource_id=augmented_part_id,
|
||||
description=audit_crud.describe_delete("AugmentedPart", augmented_part_id, label),
|
||||
)
|
||||
return Response(status_code=status.HTTP_200_OK)
|
||||
|
||||
|
||||
# --- Part Colours ------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user