Skip to content

Commit 649ca20

Browse files
Change 'all' filtering to only include clinics from the last 7 days
This has been changed as if we collect all clinics, this list will grow very large and will get out of control very quick. This has been changed to the last 7 days and can be more digestable
1 parent 666db5d commit 649ca20

2 files changed

Lines changed: 34 additions & 4 deletions

File tree

manage_breast_screening/clinics/models.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import uuid
2-
from datetime import date
2+
from datetime import date, timedelta
33
from enum import StrEnum
44

55
from django.conf import settings
@@ -59,7 +59,7 @@ def by_filter(self, filter: str):
5959
case ClinicFilter.COMPLETED:
6060
return self.completed()
6161
case ClinicFilter.ALL:
62-
return self
62+
return self.last_seven_days()
6363
case _:
6464
raise ValueError(filter)
6565

@@ -75,6 +75,9 @@ def upcoming(self):
7575
"""
7676
return self.filter(starts_at__date__gt=date.today())
7777

78+
def last_seven_days(self):
79+
return self.filter(starts_at__date__gte=(date.today() - timedelta(days=7)))
80+
7881
def completed(self):
7982
"""
8083
Completed clinics that started in the past
@@ -87,8 +90,12 @@ def completed(self):
8790
.values("state")[:1]
8891
)
8992

93+
completed_last_seven_days = self.filter(
94+
starts_at__date__gte=(date.today() - timedelta(days=7))
95+
)
96+
9097
return (
91-
self.filter(starts_at__date__lt=date.today())
98+
completed_last_seven_days.filter(starts_at__date__lt=date.today())
9299
.annotate(latest_status=Subquery(latest_status))
93100
.filter(latest_status__in=["CLOSED", "CANCELLED"])
94101
.order_by("-ends_at")
@@ -160,7 +167,7 @@ def filter_counts(cls, provider_id):
160167
queryset = cls.objects.filter(setting__provider_id=provider_id)
161168

162169
return {
163-
ClinicFilter.ALL: queryset.count(),
170+
ClinicFilter.ALL: queryset.last_seven_days().count(),
164171
ClinicFilter.TODAY: queryset.today().count(),
165172
ClinicFilter.UPCOMING: queryset.upcoming().count(),
166173
ClinicFilter.COMPLETED: queryset.completed().count(),

manage_breast_screening/clinics/tests/test_models.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,29 @@ def test_completed_ordering_by_ends_at():
8787
assertQuerySetEqual(completed, [clinic1, clinic2, clinic3])
8888

8989

90+
@pytest.mark.django_db
91+
@time_machine.travel(datetime(2025, 1, 1, 10, tzinfo=tz.utc))
92+
def test_last_seven_days_all_filtering():
93+
_clinic1 = ClinicFactory.create(
94+
starts_at=datetime(2024, 12, 1, 9, tzinfo=tz.utc),
95+
ends_at=datetime(2024, 12, 1, 17, tzinfo=tz.utc),
96+
current_status=models.ClinicStatus.CLOSED,
97+
)
98+
_clinic2 = ClinicFactory.create(
99+
starts_at=datetime(2024, 12, 2, 9, tzinfo=tz.utc),
100+
ends_at=datetime(2024, 12, 2, 17, tzinfo=tz.utc),
101+
current_status=models.ClinicStatus.CLOSED,
102+
)
103+
clinic3 = ClinicFactory.create(
104+
starts_at=datetime(2024, 12, 29, 9, tzinfo=tz.utc),
105+
ends_at=datetime(2024, 12, 29, 17, tzinfo=tz.utc),
106+
current_status=models.ClinicStatus.CLOSED,
107+
)
108+
109+
all = models.Clinic.objects.last_seven_days()
110+
assertQuerySetEqual(all, [clinic3])
111+
112+
90113
class TestUserAssignment:
91114
def test_str(self):
92115
user = UserFactory.build(first_name="John", last_name="Doe")

0 commit comments

Comments
 (0)