From 5614961980c24d1a072371eb5fb90cb7f52dc3aa Mon Sep 17 00:00:00 2001 From: Ross Date: Mon, 18 May 2026 12:17:44 +0100 Subject: [PATCH] Refactor code structure for improved readability and maintainability --- atlas/models.py | 37 +- atlas/templates/atlas/case_display_block.html | 304 ++++++++-------- atlas/views.py | 21 ++ rad/static/dv3d/index.js | 344 +++++++++--------- 4 files changed, 380 insertions(+), 326 deletions(-) diff --git a/atlas/models.py b/atlas/models.py index def0a463..99cc47ab 100644 --- a/atlas/models.py +++ b/atlas/models.py @@ -617,20 +617,37 @@ class Case(models.Model, AuthorMixin, QuestionMixin): cimar_uuid = models.CharField(max_length=255, null=True, blank=True) def get_ordered_series_details(self): - # If the series are already prefetched, use them directly - if hasattr(self, '_prefetched_objects_cache') and 'series' in self._prefetched_objects_cache: - # Get the through model and sort by sort_order - through_model = self.series.through - # Build a mapping from series pk to through instance - through_objs = list(through_model.objects.filter(case=self).select_related('series')) - through_objs.sort(key=lambda x: x.sort_order) + cached = getattr(self, "_ordered_series_details_cache", None) + if cached is not None: + return cached + + prefetched = getattr(self, "_prefetched_objects_cache", {}) + if "seriesdetail_set" in prefetched: + through_objs = sorted( + list(prefetched["seriesdetail_set"]), + key=lambda item: item.sort_order, + ) + self._ordered_series_details_cache = through_objs return through_objs - # Otherwise, query as before - return self.series.through.objects.filter(case=self).select_related('series').order_by('sort_order') + + through_objs = list( + self.series.through.objects + .filter(case=self) + .select_related("series") + .order_by("sort_order") + ) + self._ordered_series_details_cache = through_objs + return through_objs def get_ordered_series(self): """Returns the series in the case in order of the SeriesDetail sort_order""" - return [sd.series for sd in self.get_ordered_series_details()] + cached = getattr(self, "_ordered_series_cache", None) + if cached is not None: + return cached + + ordered = [sd.series for sd in self.get_ordered_series_details()] + self._ordered_series_cache = ordered + return ordered def get_app_name(self): return "atlas" diff --git a/atlas/templates/atlas/case_display_block.html b/atlas/templates/atlas/case_display_block.html index 523f327c..6a1c8385 100755 --- a/atlas/templates/atlas/case_display_block.html +++ b/atlas/templates/atlas/case_display_block.html @@ -1,7 +1,7 @@ {% load case_widgets %} {% partialdef case-series %} - {% for series in case.get_ordered_series %} + {% for series in ordered_series %}
Series {{ forloop.counter }} - {{ series.images.count }} image{{ series.images.count|pluralize }} + {{ series.visible_image_count }} image{{ series.visible_image_count|pluralize }}
{{ series.description|default:"No description" }}
@@ -506,7 +506,7 @@ Series - {{ case.get_ordered_series|length }} total + {{ ordered_series|length }} total + + + -
- - -
+
+ + + + + + + + Deleting... + +
-
- - - - - - - - Deleting... - - -
+
+ Move selected series to another case + {% if move_series_form %} +
+ {{ move_series_form.destination_case }} +
Click any case row below to select the destination case.
+
No destination case selected.
+ {% case_search_widget collection=None recent_cases=move_series_recent_cases input_id='move-series-case-search-input' target_id='move-series-case-search-results' show_select_button=True %} + +
+ - {% else %} -
Case move options are unavailable in this view.
- {% endif %} -
+ } + + function syncMoveSeriesSubmitState() { + const form = document.getElementById('move-selected-series-form'); + const hiddenInput = form ? form.querySelector("[name='destination_case']") : null; + const submitBtn = document.getElementById('move-selected-series-submit'); + if (!hiddenInput || !submitBtn) return; + submitBtn.disabled = !hiddenInput.value; + } + + document.body.addEventListener('case:selected', function (e) { + try { + const detail = e.detail || {}; + const casePk = detail.casePk; + const caseTitle = detail.caseTitle || ('Case #' + casePk); + applyMoveCaseSelection(casePk, caseTitle); + } catch (err) { + console.error('Move series case:selected handler error', err); + } + }); + + syncMoveSeriesSubmitState(); + })(); + + {% else %} +
Case move options are unavailable in this view.
+ {% endif %} + - + + - +
@@ -1010,7 +1009,7 @@