fix: clear overdue when pruned after the scheduled month

Previously only a log in the exact scheduled month cleared overdue.
Now any log on or after the start of the scheduled month counts,
so pruning late (e.g. in May for a March schedule) correctly shows upcoming.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Stephan Kerkman 2026-05-28 16:43:15 +02:00
parent 36e4570141
commit eb28f7453b
2 changed files with 16 additions and 6 deletions

View file

@ -58,6 +58,15 @@ def test_not_overdue_when_past_month_logged():
assert pruning_status(plant, today) == 'upcoming'
@pytest.mark.django_db
def test_not_overdue_when_logged_after_scheduled_month():
# Pruned in May clears a March schedule — the common real-world case
today = date(2026, 5, 28)
plant = Plant.objects.create(name='Rose', location='Garden', pruning_months=[3])
PruningLog.objects.create(plant=plant, pruned_on=date(2026, 5, 28))
assert pruning_status(plant, today) == 'upcoming'
@pytest.mark.django_db
def test_upcoming_future_month():
today = date(2026, 5, 15)

View file

@ -23,11 +23,10 @@ def pruning_status(plant, today=None):
if not months:
return 'no_schedule'
logs = plant.pruning_logs.all()
logged = {(log.pruned_on.year, log.pruned_on.month) for log in logs}
logs = list(plant.pruning_logs.all())
if today.month in months:
if (today.year, today.month) not in logged:
if not any(l.pruned_on.year == today.year and l.pruned_on.month == today.month for l in logs):
return 'due_this_month'
return 'upcoming'
@ -36,8 +35,10 @@ def pruning_status(plant, today=None):
if past.month in months:
if i >= 6:
return 'upcoming'
if (past.year, past.month) not in logged:
return 'overdue'
return 'upcoming'
# Any log on or after the start of the scheduled month clears overdue
cutoff = date(past.year, past.month, 1)
if any(l.pruned_on >= cutoff for l in logs):
return 'upcoming'
return 'overdue'
return 'upcoming'