diff --git a/atlas/models.py b/atlas/models.py
index 968f1fbb..6ee96fd0 100644
--- a/atlas/models.py
+++ b/atlas/models.py
@@ -816,13 +816,17 @@ class CaseCollection(ExamOrCollectionGenericBase):
# return False
+ def get_cases(self):
+ """Returns the cases in the collection in order of the CaseDetail sort_order"""
+ return self.cases.all().order_by("casedetail__sort_order")
+
def get_case_by_index(
self, case_index: int, case_count=False
) -> Case | Tuple[Case, int]:
# Seems to be a bug when case_number is 0 or 1 the same (first) object gets returned
# forces a list fixes (but is likely inefficient)
cases = list(
- self.cases.all().order_by("casedetail__sort_order").prefetch_related()
+ self.get_cases().prefetch_related()
)
try:
@@ -838,7 +842,7 @@ class CaseCollection(ExamOrCollectionGenericBase):
def get_case_take_url(self, case):
cases = list(
- self.cases.all().order_by("casedetail__sort_order").prefetch_related()
+ self.get_cases().prefetch_related()
)
return reverse(
@@ -878,6 +882,13 @@ class CaseCollection(ExamOrCollectionGenericBase):
"studies": studies
}
+ def add_case(self, case):
+ """Adds a case to the collection and makes sure order is maintained"""
+ # We might be better off adding via the case detail model direct
+ #CaseDetail.objects.create(case=case, collection=self)
+ self.cases.add(case)
+ self.order_cases()
+
def order_cases(self):
"""Modifies the casedetail sort_order to sequentially order the cases"""
diff --git a/atlas/templates/atlas/collection_detail.html b/atlas/templates/atlas/collection_detail.html
index 8882deec..485b814c 100644
--- a/atlas/templates/atlas/collection_detail.html
+++ b/atlas/templates/atlas/collection_detail.html
@@ -11,7 +11,7 @@
Cases
{% for casedetail in casesdetails %}
- - Case {{forloop.counter}}
+
- Case {{forloop.counter}}
: {{casedetail.case.title}}
diff --git a/atlas/tests/conftest.py b/atlas/tests/conftest.py
index 751993b5..74149026 100644
--- a/atlas/tests/conftest.py
+++ b/atlas/tests/conftest.py
@@ -107,10 +107,52 @@ def create_case(
case.presentation.add(presentation)
case.pathological_process.add(pathological_process)
case.author.add(user)
- case.editor.add(user)
return case
+@pytest.fixture
+def make_case(create_subspecialty, create_condition, create_presentation, create_pathological_process):
+ def _make_case(
+ title="Sample Case",
+ description="Sample description",
+ history="Sample history",
+ discussion="Sample discussion",
+ report="Sample report",
+ diagnostic_certainty=Case.CertaintyChoices.LIKELY,
+ verified=True,
+ created_date=timezone.now(),
+ published_date=timezone.now(),
+ archive=False,
+ open_access=True,
+ ):
+ subspecialty = create_subspecialty
+ condition = create_condition
+ presentation = create_presentation
+ pathological_process = create_pathological_process
+ user, _ = get_user_model().objects.get_or_create(
+ username="Test User 2", password="Test Password", email="test@emai.com")
+
+ case = Case.objects.create(
+ title=title,
+ description=description,
+ history=history,
+ discussion=discussion,
+ report=report,
+ diagnostic_certainty=diagnostic_certainty,
+ verified=verified,
+ created_date=created_date,
+ published_date=published_date,
+ archive=archive,
+ open_access=open_access,
+ )
+ case.subspecialty.add(subspecialty)
+ case.condition.add(condition)
+ case.presentation.add(presentation)
+ case.pathological_process.add(pathological_process)
+ case.author.add(user)
+
+ return case
+ return _make_case
@pytest.fixture
def create_series(create_modality, create_examination, create_plane, create_contrast, create_user):
@@ -141,12 +183,12 @@ def create_series_image():
@pytest.fixture
def create_series_finding():
- return SeriesFinding.objects.create()
+ return SeriesFinding.objects.create(description="Test Description")
@pytest.fixture
def create_case_collection():
- return CaseCollection.objects.create()
+ return CaseCollection.objects.create(name="Test Collection")
@pytest.fixture
diff --git a/atlas/tests/test_atlas_models.py b/atlas/tests/test_atlas_models.py
index 049a0386..7d86cd23 100644
--- a/atlas/tests/test_atlas_models.py
+++ b/atlas/tests/test_atlas_models.py
@@ -1,7 +1,7 @@
from django.urls import reverse
import pytest
-from atlas.models import Finding, Condition
+from atlas.models import CaseCollection, Finding, Condition
#from atlas.views import GenericExamViews
#@pytest.mark.django_db
@@ -80,4 +80,49 @@ def test_create_case(db, create_case):
def test_create_series(db, create_series):
series = create_series
- assert series
\ No newline at end of file
+ assert series
+
+
+
+def test_collection(db, create_case_collection, create_case, make_case, client):
+ collection: CaseCollection = create_case_collection
+
+ # create some cases
+ case1 = create_case
+ case2 = make_case()
+ case3 = make_case(title="Case 3")
+ case4 = make_case(title="Case 4")
+
+ collection.cases.set([case1, case2, case3, case4])
+
+ case5 = make_case(title="Case 5")
+
+ assert collection.cases.all().count() == 4
+
+ collection.add_case(case5)
+
+ assert collection.cases.all().count() == 5
+
+ assert collection.get_case_by_index(0) == case1
+ assert collection.get_case_by_index(1) == case2
+ assert collection.get_case_by_index(2) == case3
+ assert collection.get_case_by_index(3) == case4
+ assert collection.get_case_by_index(4) == case5
+
+ assert collection.get_absolute_url() == reverse("atlas:collection_detail", kwargs={"pk": collection.pk})
+
+ assert collection.get_take_url() == reverse("atlas:collection_take_start", kwargs={"pk": collection.pk})
+
+ new_order = [
+ case5.pk,
+ case1.pk,
+ case2.pk,
+ case3.pk,
+ case4.pk
+ ]
+ response = client.post(reverse("atlas:exam_json_edit", kwargs={"pk":collection.pk} ), json=new_order, follow=True)
+ # Check that sorting the exam works
+ assert response.status_code == 200
+ assert response.content == b"OK"
+
+
diff --git a/atlas/views.py b/atlas/views.py
index 77eaf8c6..fa1869fa 100755
--- a/atlas/views.py
+++ b/atlas/views.py
@@ -391,8 +391,7 @@ def add_case_to_collection(request, collection_id):
if case in collection.cases.all():
return HttpResponse("Case already in collection")
print(case)
- collection.cases.add(case)
- collection.order_cases()
+ collection.add_case(case)
return HttpResponse("Case added to collection")