43 lines
1.2 KiB
Python
43 lines
1.2 KiB
Python
from datetime import date
|
|
|
|
|
|
def months_before(d, n):
|
|
"""Return a date on the 1st of the month n months before d."""
|
|
m = (d.month - 1 - n) % 12 + 1
|
|
y = d.year + (d.month - 1 - n) // 12
|
|
return date(y, m, 1)
|
|
|
|
|
|
def pruning_status(plant, today=None):
|
|
"""
|
|
Returns one of: 'overdue', 'due_this_month', 'upcoming', 'no_schedule'.
|
|
|
|
A scheduled month is 'overdue' only if its most recent occurrence is within
|
|
the past 6 months and was not logged. Beyond 6 months, the next occurrence
|
|
is sooner than the missed one, so it is 'upcoming'.
|
|
"""
|
|
if today is None:
|
|
today = date.today()
|
|
|
|
months = plant.pruning_months or []
|
|
if not months:
|
|
return 'no_schedule'
|
|
|
|
logs = plant.pruning_logs.all()
|
|
logged = {(log.pruned_on.year, log.pruned_on.month) for log in logs}
|
|
|
|
if today.month in months:
|
|
if (today.year, today.month) not in logged:
|
|
return 'due_this_month'
|
|
return 'upcoming'
|
|
|
|
for i in range(1, 13):
|
|
past = months_before(today, i)
|
|
if past.month in months:
|
|
if i >= 6:
|
|
return 'upcoming'
|
|
if (past.year, past.month) not in logged:
|
|
return 'overdue'
|
|
return 'upcoming'
|
|
|
|
return 'upcoming'
|