Refactor import_dicoms_helper to accept explicit queryset and enhance upload_dicom_case with detailed upload status reporting
This commit is contained in:
+43
-7
@@ -203,7 +203,9 @@ def uncategorised_dicoms(request):
|
|||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
def import_dicoms_helper(request, case_id: int | None = None):
|
def import_dicoms_helper(request, case_id: int | None = None, dicoms=None):
|
||||||
|
# Allow callers to pass an explicit queryset of UncategorisedDicom objects
|
||||||
|
if dicoms is None:
|
||||||
if "selection" in request.POST:
|
if "selection" in request.POST:
|
||||||
dicoms = UncategorisedDicom.objects.filter(
|
dicoms = UncategorisedDicom.objects.filter(
|
||||||
series_instance_uid__in=request.POST.getlist("selection")
|
series_instance_uid__in=request.POST.getlist("selection")
|
||||||
@@ -304,7 +306,7 @@ def import_dicoms_case(request, case_id: int):
|
|||||||
return import_dicoms_helper(request, case_id=case_id)
|
return import_dicoms_helper(request, case_id=case_id)
|
||||||
|
|
||||||
|
|
||||||
@router.post("/upload_dicom_case/{case_id}", auth=BearerAuth(), response=List[Tuple[SeriesSchema, str]])
|
@router.post("/upload_dicom_case/{case_id}", auth=BearerAuth())
|
||||||
def upload_dicom_case(request, case_id: int, files: List[UploadedFile] = File(...)):
|
def upload_dicom_case(request, case_id: int, files: List[UploadedFile] = File(...)):
|
||||||
"""Upload DICOM files and immediately import them into the given case.
|
"""Upload DICOM files and immediately import them into the given case.
|
||||||
|
|
||||||
@@ -312,16 +314,50 @@ def upload_dicom_case(request, case_id: int, files: List[UploadedFile] = File(..
|
|||||||
then calls the existing import helper which will create Series/SeriesImage
|
then calls the existing import helper which will create Series/SeriesImage
|
||||||
objects and attach them to the specified case.
|
objects and attach them to the specified case.
|
||||||
"""
|
"""
|
||||||
|
uploaded = []
|
||||||
|
duplicate = []
|
||||||
|
failed = []
|
||||||
|
duplicate_series = set()
|
||||||
|
|
||||||
|
created_ids = []
|
||||||
|
|
||||||
for file in files:
|
for file in files:
|
||||||
try:
|
try:
|
||||||
ud = UncategorisedDicom(image=file, user=request.user)
|
ud = UncategorisedDicom(image=file, user=request.user)
|
||||||
ud.save()
|
ud.save()
|
||||||
except DuplicateDicom:
|
|
||||||
# skip duplicates
|
|
||||||
continue
|
|
||||||
|
|
||||||
# Now reuse import helper which will consume UncategorisedDicom for this user
|
uploaded.append((file.name, ud.image_blake3_hash))
|
||||||
return import_dicoms_helper(request, case_id=case_id)
|
created_ids.append(ud.pk)
|
||||||
|
except DuplicateDicom as e:
|
||||||
|
duplicate.append((file.name, getattr(ud, "image_blake3_hash", None)))
|
||||||
|
|
||||||
|
dup_obj = getattr(e, "duplicate", None)
|
||||||
|
if isinstance(dup_obj, SeriesImage):
|
||||||
|
try:
|
||||||
|
if dup_obj.series is not None:
|
||||||
|
duplicate_series.add(dup_obj.series.get_absolute_url())
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
continue
|
||||||
|
except InvalidDicomError:
|
||||||
|
failed.append(file.name)
|
||||||
|
|
||||||
|
# Import into the target case (consume only the uploaded UncategorisedDicom objects)
|
||||||
|
if created_ids:
|
||||||
|
try:
|
||||||
|
dicom_qs = UncategorisedDicom.objects.filter(pk__in=created_ids)
|
||||||
|
import_dicoms_helper(request, case_id=case_id, dicoms=dicom_qs)
|
||||||
|
except Exception:
|
||||||
|
# don't let import errors prevent returning upload status
|
||||||
|
logger.error("Error importing dicoms after upload", exc_info=True)
|
||||||
|
|
||||||
|
return {
|
||||||
|
"uploaded": uploaded,
|
||||||
|
"duplicates": duplicate,
|
||||||
|
"failed": failed,
|
||||||
|
"duplicate_series": list(duplicate_series),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@router.get("/orphan_series", auth=BearerAuth(), response=List[SeriesSchema])
|
@router.get("/orphan_series", auth=BearerAuth(), response=List[SeriesSchema])
|
||||||
|
|||||||
Reference in New Issue
Block a user