Skip to content

Commit

Permalink
Treat cases merged into a closed case as closed in filtering.
Browse files Browse the repository at this point in the history
  • Loading branch information
neprune committed Sep 19, 2024
1 parent 2808939 commit 80b4142
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
8 changes: 5 additions & 3 deletions cases/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,14 @@ def priority_only_filter(self, queryset, name, value):

def closed_filter(self, queryset, name, value):
if value == "none":
return queryset.filter(closed=False)
cs = [c for c in queryset if not c.is_closed_or_merged_into_closed]
elif value == "include":
return queryset
elif value == "only":
return queryset.filter(closed=True)
return queryset # pragma: no cover - should not be reachable.
cs = [c for c in queryset if c.is_closed_or_merged_into_closed]
else:
return queryset # pragma: no cover - should not be reachable.
return queryset.filter(id__in=[c.id for c in cs])

def complaints_filter(self, queryset, name, value):
qs = Case.objects.annotate_total_complaints(queryset)
Expand Down
6 changes: 6 additions & 0 deletions cases/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,12 @@ def file_storage_remaining_bytes(self):
- self.file_storage_used_bytes
)

@property
def is_closed_or_merged_into_closed(self):
return self.closed or any(
[Case.objects.get(pk=m["id"]).closed for m in self.merged_into_list]
)


class Complaint(AbstractModel):
case = models.ForeignKey(Case, on_delete=models.CASCADE, related_name="complaints")
Expand Down
6 changes: 6 additions & 0 deletions cases/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,21 +196,27 @@ def test_closed_cases(admin_client):
closed_case = Case.objects.create(
closed=True,
)
merged_into_closed_case = Case.objects.create(closed=False)
merged_into_closed_case.merge_into(closed_case)
response = admin_client.get("/cases")
assertContains(response, f"/cases/{open_case.id}")
assertNotContains(response, f"/cases/{closed_case.id}")
assertNotContains(response, f"/cases/{merged_into_closed_case.id}")

response = admin_client.get("/cases?closed=none")
assertContains(response, f"/cases/{open_case.id}")
assertNotContains(response, f"/cases/{closed_case.id}")
assertNotContains(response, f"/cases/{merged_into_closed_case.id}")

response = admin_client.get("/cases?closed=include")
assertContains(response, f"/cases/{open_case.id}")
assertContains(response, f"/cases/{closed_case.id}")
assertContains(response, f"/cases/{merged_into_closed_case.id}")

response = admin_client.get("/cases?closed=only")
assertNotContains(response, f"/cases/{open_case.id}")
assertContains(response, f"/cases/{closed_case.id}")
assertContains(response, f"/cases/{merged_into_closed_case.id}")


def test_priority_only_cases(admin_client, case_1, case_2):
Expand Down

0 comments on commit 80b4142

Please sign in to comment.