Adding scope import
This commit is contained in:
1 parent
cda15df2ed
commit
69cbfa2d3f
18 files changed
+1165
-3
No files matched your search
@@ -14,6 +14,7 @@ from app.api import (
|
||||
part_colours,
|
||||
part_types,
|
||||
roles,
|
||||
scope_import,
|
||||
scope_models,
|
||||
static_connections,
|
||||
sub_part_types,
|
||||
@@ -66,3 +67,6 @@ api_router.include_router(
|
||||
)
|
||||
api_router.include_router(audit_log.router, prefix="/audit-log", tags=["audit-log"])
|
||||
api_router.include_router(fits.router, prefix="/fits", tags=["fits"])
|
||||
api_router.include_router(
|
||||
scope_import.router, prefix="/scope-import", tags=["scope-import"]
|
||||
)
|
||||
@@ -0,0 +1,53 @@
|
||||
from fastapi import APIRouter, Depends, File, HTTPException, UploadFile, status
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.core.database import get_db
|
||||
from app.crud import audit as audit_crud
|
||||
from app.deps import require_permission
|
||||
from app.models.user import User
|
||||
from app.schemas.scope_import import ScopeImportResult
|
||||
from app.services import scope_import as scope_import_service
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
_XLSX_TYPES = {
|
||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||
"application/octet-stream",
|
||||
"",
|
||||
}
|
||||
|
||||
|
||||
@router.post("", response_model=ScopeImportResult)
|
||||
async def import_scopes(
|
||||
file: UploadFile = File(...),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
actor: User = Depends(require_permission("augmented_parts:write")),
|
||||
):
|
||||
filename = file.filename or ""
|
||||
if not filename.lower().endswith(".xlsx"):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
detail="Expected an .xlsx spreadsheet.",
|
||||
)
|
||||
data = await file.read()
|
||||
try:
|
||||
result = await scope_import_service.import_from_bytes(db, data)
|
||||
except Exception as exc: # openpyxl / malformed workbook
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
detail=f"Could not read spreadsheet: {exc}",
|
||||
) from exc
|
||||
|
||||
await audit_crud.log(
|
||||
db,
|
||||
actor=actor,
|
||||
action="update",
|
||||
resource_type="AugmentedPart",
|
||||
resource_id=None,
|
||||
description=(
|
||||
f"Scope import ({filename}): {result.created} created, "
|
||||
f"{result.updated} updated, {result.skipped} skipped, "
|
||||
f"{result.errors} errors"
|
||||
),
|
||||
)
|
||||
return result
|
||||
Reference in new issue
Block a user