From 569ef760fa42209baadd5d5ea3124e189d8321ae Mon Sep 17 00:00:00 2001 From: Ross Date: Mon, 6 Oct 2025 11:32:44 +0100 Subject: [PATCH] Refactor case detail retrieval into a separate function for improved readability and error handling --- atlas/views.py | 85 ++++++++++++++++++++++++++++---------------------- 1 file changed, 47 insertions(+), 38 deletions(-) diff --git a/atlas/views.py b/atlas/views.py index 0fb7a76d..b00ba018 100755 --- a/atlas/views.py +++ b/atlas/views.py @@ -303,47 +303,54 @@ def case_displaysets_delete(request, pk): displayset.delete() return HttpResponse("
  • Display set deleted successfully.") + +def get_case_for_case_detail(pk: int) -> Case: + try: + case = ( + Case.objects + .select_related() # Add any FK fields if needed + .prefetch_related( + Prefetch( + "seriesdetail_set", + queryset=SeriesDetail.objects.select_related("series").order_by("sort_order").prefetch_related( + Prefetch( + "series", + queryset=Series.objects.select_related("modality", "examination", "plane", "contrast") + .prefetch_related( + Prefetch( + "findings", + queryset=SeriesFinding.objects.prefetch_related( + "findings", "conditions", "structures" + ) + ), + Prefetch("images", queryset=SeriesImage.objects.filter(removed=False).order_by("position")), + ) + ), + ), + ), + Prefetch("subspecialty", queryset=Subspecialty.objects.all()), + Prefetch("condition", queryset=Condition.objects.all()), + Prefetch("presentation", queryset=Presentation.objects.all()), + Prefetch("pathological_process", queryset=PathologicalProcess.objects.all()), + Prefetch("caseresource_set", queryset=CaseResource.objects.all()), + Prefetch("casecollection_set", queryset=CaseCollection.objects.all()), + Prefetch("display_sets", queryset=CaseDisplaySet.objects.prefetch_related( + Prefetch("findings", queryset=Finding.objects.all()), + Prefetch("structures", queryset=Structure.objects.all()), + Prefetch("conditions", queryset=Condition.objects.all()), + )), + ) + .get(pk=pk) + ) + case.ordered_series = [sd.series for sd in case.seriesdetail_set.all()] + return case + except Case.DoesNotExist: + raise Http404("Case not found.") + @login_required @user_has_case_view_access def case_detail(request, pk): - # Prefetch all related objects needed for the template - case = ( - Case.objects - .select_related() # Add any FK fields if needed - .prefetch_related( - Prefetch( - "seriesdetail_set", - queryset=SeriesDetail.objects.select_related("series").order_by("sort_order").prefetch_related( - Prefetch( - "series", - queryset=Series.objects.select_related("modality", "examination", "plane", "contrast") - .prefetch_related( - Prefetch( - "findings", - queryset=SeriesFinding.objects.prefetch_related( - "findings", "conditions", "structures" - ) - ), - Prefetch("images", queryset=SeriesImage.objects.filter(removed=False).order_by("position")), - ) - ), - ), - ), - Prefetch("subspecialty", queryset=Subspecialty.objects.all()), - Prefetch("condition", queryset=Condition.objects.all()), - Prefetch("presentation", queryset=Presentation.objects.all()), - Prefetch("pathological_process", queryset=PathologicalProcess.objects.all()), - Prefetch("caseresource_set", queryset=CaseResource.objects.all()), - Prefetch("casecollection_set", queryset=CaseCollection.objects.all()), - Prefetch("display_sets", queryset=CaseDisplaySet.objects.prefetch_related( - Prefetch("findings", queryset=Finding.objects.all()), - Prefetch("structures", queryset=Structure.objects.all()), - Prefetch("conditions", queryset=Condition.objects.all()), - )), - ) - .get(pk=pk) - ) - case.ordered_series = [sd.series for sd in case.seriesdetail_set.all()] + case = get_case_for_case_detail(pk) can_edit = case.check_user_can_edit(request.user) return render( @@ -2853,6 +2860,8 @@ def collection_case_view(request, pk, case_number): case, case_count = collection.get_case_by_index(case_number, case_count=True) + case = get_case_for_case_detail(case.pk) + casedetail = CaseDetail.objects.get(case=case, collection=collection) series_list = case.series.all().prefetch_related("images", "examination", "plane")