Adding OData Import
This commit is contained in:
1 parent
c8696e67b5
commit
291416544e
18 files changed
+547
-3
No files matched your search
@@ -6,8 +6,11 @@ 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 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
|
||||
from app.crud import sub_part_type as sub_part_type_crud
|
||||
from app.services import odata as odata_service
|
||||
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
|
||||
@@ -1007,6 +1010,100 @@ async def delete_sub_part_type(
|
||||
return Response(status_code=status.HTTP_200_OK)
|
||||
|
||||
|
||||
# --- Imported Parts ----------------------------------------------------------
|
||||
|
||||
|
||||
@router.get("/imported-parts", response_class=HTMLResponse)
|
||||
async def imported_parts_page(
|
||||
request: Request,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
user: User = Depends(web_require("imported_parts:read")),
|
||||
):
|
||||
config = await imported_part_config_crud.get(db)
|
||||
config_view = {
|
||||
"odata_url": config.odata_url if config else None,
|
||||
"username": config.username if config else None,
|
||||
"has_password": bool(config and config.password),
|
||||
}
|
||||
parts = await imported_part_crud.list_all(db)
|
||||
total = await imported_part_crud.count(db)
|
||||
return templates.TemplateResponse(
|
||||
request,
|
||||
"admin/imported_parts.html",
|
||||
{"user": user, "config": config_view, "parts": parts, "total": total},
|
||||
)
|
||||
|
||||
|
||||
@router.post("/imported-parts/config", response_class=HTMLResponse)
|
||||
async def save_imported_part_config(
|
||||
request: Request,
|
||||
odata_url: str = Form(""),
|
||||
username: str = Form(""),
|
||||
password: str = Form(""),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
actor: User = Depends(web_require("imported_parts:write")),
|
||||
):
|
||||
current = await imported_part_config_crud.get(db)
|
||||
# Keep existing password when field submitted blank
|
||||
kept_password = password.strip() or (current.password if current else None)
|
||||
await imported_part_config_crud.upsert(
|
||||
db,
|
||||
odata_url=odata_url.strip() or None,
|
||||
username=username.strip() or None,
|
||||
password=kept_password,
|
||||
)
|
||||
await audit_crud.log(
|
||||
db, actor=actor, action="update", resource_type="ImportedPartConfig",
|
||||
resource_id=1,
|
||||
description="Updated ImportedPart OData configuration",
|
||||
)
|
||||
return HTMLResponse("Configuration saved.", status_code=200)
|
||||
|
||||
|
||||
@router.post("/imported-parts/refresh", response_class=HTMLResponse)
|
||||
async def refresh_imported_parts(
|
||||
request: Request,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
actor: User = Depends(web_require("imported_parts:write")),
|
||||
):
|
||||
config = await imported_part_config_crud.get(db)
|
||||
if not config or not config.odata_url:
|
||||
parts = await imported_part_crud.list_all(db)
|
||||
total = await imported_part_crud.count(db)
|
||||
return templates.TemplateResponse(
|
||||
request,
|
||||
"admin/_imported_parts_table.html",
|
||||
{"parts": parts, "total": total, "sync_error": "OData URL is not configured."},
|
||||
)
|
||||
try:
|
||||
records = await odata_service.fetch(config.odata_url, config.username, config.password)
|
||||
created, updated = await imported_part_crud.upsert_bulk(db, records)
|
||||
await audit_crud.log(
|
||||
db, actor=actor, action="update", resource_type="ImportedPart",
|
||||
resource_id=None,
|
||||
description=f"OData sync: {created} created, {updated} updated ({len(records)} total records)",
|
||||
)
|
||||
parts = await imported_part_crud.list_all(db)
|
||||
total = await imported_part_crud.count(db)
|
||||
return templates.TemplateResponse(
|
||||
request,
|
||||
"admin/_imported_parts_table.html",
|
||||
{
|
||||
"parts": parts,
|
||||
"total": total,
|
||||
"sync_result": f"Sync complete: {created} created, {updated} updated.",
|
||||
},
|
||||
)
|
||||
except Exception as exc:
|
||||
parts = await imported_part_crud.list_all(db)
|
||||
total = await imported_part_crud.count(db)
|
||||
return templates.TemplateResponse(
|
||||
request,
|
||||
"admin/_imported_parts_table.html",
|
||||
{"parts": parts, "total": total, "sync_error": f"Sync failed: {exc}"},
|
||||
)
|
||||
|
||||
|
||||
# --- Audit Log ---------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user