start removing lots of print
This commit is contained in:
+54
-68
@@ -128,15 +128,10 @@ def uncategorised_dicoms(request):
|
||||
|
||||
|
||||
def import_dicoms_helper(request, case_id: int | None = None):
|
||||
# TODO: move this out of the api (would be better with HTMX)
|
||||
|
||||
|
||||
# dicoms = UncategorisedDicom.objects.filter(user=request.user)
|
||||
if "selection" in request.POST:
|
||||
dicoms = UncategorisedDicom.objects.filter(
|
||||
series_instance_uid__in=request.POST.getlist("selection")
|
||||
)
|
||||
|
||||
else:
|
||||
dicoms = UncategorisedDicom.objects.filter(user=request.user)
|
||||
|
||||
@@ -145,73 +140,66 @@ def import_dicoms_helper(request, case_id: int | None = None):
|
||||
else:
|
||||
order = None
|
||||
|
||||
data = defaultdict(list)
|
||||
for d in dicoms:
|
||||
# Group dicoms by SeriesInstanceUID, but don't keep all tags in memory
|
||||
series_map = {}
|
||||
for d in dicoms.iterator(): # Use iterator() to avoid loading all at once
|
||||
tags = d.basic_dicom_tags
|
||||
series_uid = tags["SeriesInstanceUID"]
|
||||
|
||||
data[tags["SeriesInstanceUID"]].append((d, tags))
|
||||
|
||||
series_list = []
|
||||
for series_uid in data:
|
||||
# Hmm probably not a good idea to blinding create a modality...
|
||||
try:
|
||||
modality = Modality.objects.get(short_code=tags["Modality"])
|
||||
except Modality.DoesNotExist:
|
||||
modality = Modality.objects.create(short_code=tags["Modality"], modality=tags["Modality"])
|
||||
|
||||
|
||||
tags = data[series_uid][0][1]
|
||||
|
||||
try:
|
||||
with transaction.atomic():
|
||||
series, created = Series.objects.get_or_create(
|
||||
series_instance_uid=tags["SeriesInstanceUID"],
|
||||
defaults={
|
||||
"modality": modality,
|
||||
"description": tags["SeriesDescription"],
|
||||
}
|
||||
)
|
||||
if created and tags["StudyDescription"]:
|
||||
examination, created_exam = Examination.objects.get_or_create(
|
||||
examination=tags["StudyDescription"]
|
||||
)
|
||||
if created_exam:
|
||||
examination.modality = modality
|
||||
examination.save()
|
||||
series.examination = examination
|
||||
series.save()
|
||||
except IntegrityError:
|
||||
# If a race condition still occurs, fetch the existing series
|
||||
pass
|
||||
|
||||
|
||||
if 'series' not in locals():
|
||||
series = Series.objects.get(series_instance_uid=tags["SeriesInstanceUID"])
|
||||
|
||||
|
||||
# We might only want to add the author during creation....
|
||||
series.author.add(request.user)
|
||||
|
||||
if case_id is not None:
|
||||
case_object = get_object_or_404(Case, pk=case_id)
|
||||
if series_uid not in series_map:
|
||||
# Get or create Series and related objects as needed
|
||||
try:
|
||||
series.case.add(case_object)
|
||||
modality = Modality.objects.get(short_code=tags["Modality"])
|
||||
except Modality.DoesNotExist:
|
||||
modality = Modality.objects.create(short_code=tags["Modality"], modality=tags["Modality"])
|
||||
|
||||
series_tags = tags # Save the first tag for this series
|
||||
try:
|
||||
with transaction.atomic():
|
||||
series, created = Series.objects.get_or_create(
|
||||
series_instance_uid=series_uid,
|
||||
defaults={
|
||||
"modality": modality,
|
||||
"description": tags.get("SeriesDescription", ""),
|
||||
}
|
||||
)
|
||||
if created and tags.get("StudyDescription"):
|
||||
examination, created_exam = Examination.objects.get_or_create(
|
||||
examination=tags["StudyDescription"]
|
||||
)
|
||||
if created_exam:
|
||||
examination.modality = modality
|
||||
examination.save()
|
||||
series.examination = examination
|
||||
series.save()
|
||||
except IntegrityError:
|
||||
# The relation already exists, safe to ignore
|
||||
pass
|
||||
series = Series.objects.get(series_instance_uid=series_uid)
|
||||
|
||||
for dicom, dicom_tags in data[series_uid]:
|
||||
series_image = SeriesImage(
|
||||
image=dicom.image,
|
||||
series=series,
|
||||
is_dicom=True,
|
||||
image_blake3_hash=dicom.image_blake3_hash,
|
||||
)
|
||||
series_image.save()
|
||||
# dicom.image.delete()
|
||||
# Add author and case if needed
|
||||
series.author.add(request.user)
|
||||
if case_id is not None:
|
||||
case_object = get_object_or_404(Case, pk=case_id)
|
||||
try:
|
||||
series.case.add(case_object)
|
||||
except IntegrityError:
|
||||
pass
|
||||
|
||||
dicom.delete()
|
||||
series_map[series_uid] = series
|
||||
else:
|
||||
series = series_map[series_uid]
|
||||
|
||||
# Create and save SeriesImage immediately
|
||||
series_image = SeriesImage(
|
||||
image=d.image,
|
||||
series=series,
|
||||
is_dicom=True,
|
||||
image_blake3_hash=d.image_blake3_hash,
|
||||
)
|
||||
series_image.save()
|
||||
d.delete() # Remove UncategorisedDicom immediately
|
||||
|
||||
# Optionally, re-order series if needed
|
||||
for series in series_map.values():
|
||||
match order:
|
||||
case "order-series-instance-number":
|
||||
series.order_by_dicom("InstanceNumber")
|
||||
@@ -219,10 +207,8 @@ def import_dicoms_helper(request, case_id: int | None = None):
|
||||
series.order_by_dicom("SliceLocation")
|
||||
case _:
|
||||
pass
|
||||
|
||||
series_list.append((series, series.get_link()))
|
||||
|
||||
return series_list
|
||||
return [(series, series.get_link()) for series in series_map.values()]
|
||||
|
||||
|
||||
@router.post(
|
||||
|
||||
Reference in New Issue
Block a user