diff --git a/atlas/models.py b/atlas/models.py index 98922574..4d51e49d 100644 --- a/atlas/models.py +++ b/atlas/models.py @@ -396,6 +396,22 @@ 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) + return through_objs + # Otherwise, query as before + return self.series.through.objects.filter(case=self).select_related('series').order_by('sort_order') + + 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()] + 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 a49e29b9..1f4e8cf9 100755 --- a/atlas/templates/atlas/case_display_block.html +++ b/atlas/templates/atlas/case_display_block.html @@ -1,7 +1,7 @@ {% load partials %} {% partialdef case-series %} - {% for series in case.series.all %} + {% for series in case.get_ordered_series %} Series {{ forloop.counter }}:
@@ -9,7 +9,12 @@ {{series.get_block}}
- + + Popup @@ -144,13 +149,13 @@ {% partial case-series %} - Add new series
Import new uploads
- + + + Manage Series + Create and add new series
+ +
- +
@@ -204,7 +228,7 @@ {% endif %} {% if case.series.all %} - {% for series in case.series.all %} + {% for detail in case.get_ordered_series %} {% for finding in series.findings.all %}