diff --git a/atlas/views.py b/atlas/views.py index 4ff06c84..2c3cb102 100755 --- a/atlas/views.py +++ b/atlas/views.py @@ -2280,14 +2280,22 @@ def user_uploads( tags, date = data[series][0] series_list.append((series, len(data[series]), tags, date)) - series_list.sort(key=lambda l: l[-1]) + series_list.sort(key=lambda l: l[-1] or "") # Group series by StudyInstanceUID for better visual grouping in the template grouped = {} for series_uid, count, tags, date in series_list: study_uid = tags.get("StudyInstanceUID") or tags.get("StudyID") or "unknown" study_desc = tags.get("StudyDescription", "") - study_date = tags.get("StudyDate", date) + # Normalise to a sortable string; DICOM StudyDate is "YYYYMMDD", fall + # back to the upload's created_date converted to the same format. + raw_study_date = tags.get("StudyDate") + if raw_study_date: + study_date = str(raw_study_date) + elif hasattr(date, "strftime"): + study_date = date.strftime("%Y%m%d") + else: + study_date = str(date) if study_uid not in grouped: grouped[study_uid] = { "description": study_desc,