Add per-task reminder notifications: before due, due now, and overdue

Only proactive notification was the once-a-day 6-7AM digest. Wires up
the previously-unused User.default_reminder_minutes profile setting
and ScheduledReminder model (Gitea #8) to send timely per-task alerts:

- Task.reschedule_reminders(), hooked into Task.save() via a dirty-check
  against due_date/due_time/reminder_at/status/is_deleted, (re)creates up
  to 3 ScheduledReminder rows per active due task: a "before due" reminder
  (from reminder_at if set, else default_reminder_minutes before due), a
  "due now" notification, and an "overdue" notification (mirroring
  is_overdue's day-after rule for date-only due tasks). Hooking into
  save() means every call site - web views, the API, and sync - picks
  this up automatically.
- New Celery task send_scheduled_reminders (beat schedule: every 5 min)
  sends due reminders via whichever of email/push the user has enabled,
  reusing the existing per-user channel toggles, and logs them to the
  Notification table.
- 14 new tests covering the scheduling math, due-date-change/completion/
  deletion cleanup, and the sending task's channel and timing behavior.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Keith Smith
2026-09-05 09:46:12 -06:00
co-authored by Claude Sonnet 5
parent d67a7e4d8f
commit 5359bf243a
8 changed files with 398 additions and 7 deletions
+75
View File
@@ -16,6 +16,45 @@ from zoneinfo import ZoneInfo
logger = logging.getLogger(__name__)
def notify_user(user, title, body, notification_type, task=None, url='/'):
"""
Send a notification to a user via whichever of email/push they have
enabled, and record it in the Notification log. Returns True if it went
out on at least one channel.
"""
from .models import Notification
notified = False
if user.email_notifications and user.email_verified:
try:
send_mail(
subject=title,
message=body,
from_email=settings.DEFAULT_FROM_EMAIL,
recipient_list=[user.email],
fail_silently=False,
)
notified = True
except Exception as e:
logger.error(f"Failed to email {user.email}: {e}")
if user.push_notifications:
send_web_push_to_user(user, title=title, body=body, url=url)
notified = True
if notified:
Notification.objects.create(
user=user,
notification_type=notification_type,
title=title,
message=body,
task=task,
)
return notified
def send_web_push_to_user(user, title, body, url='/'):
"""Send a Web Push notification to all of a user's registered web devices."""
if not settings.VAPID_PRIVATE_KEY:
@@ -183,6 +222,42 @@ def send_daily_task_email():
return notifications_sent
REMINDER_MESSAGES = {
'reminder': lambda task: (f'Upcoming: {task.title}', f'"{task.title}" is due soon.'),
'due_soon': lambda task: (f'Due now: {task.title}', f'"{task.title}" is due now.'),
'overdue': lambda task: (f'Overdue: {task.title}', f'"{task.title}" is overdue.'),
}
@shared_task
def send_scheduled_reminders():
"""
Send "before due", "due now", and "overdue" notifications from
ScheduledReminder rows created by Task.reschedule_reminders(). Runs
frequently (every few minutes) since these are time-sensitive, unlike
the once-a-day digest above.
"""
from .models import ScheduledReminder
due_reminders = ScheduledReminder.objects.filter(
is_sent=False, remind_at__lte=timezone.now()
).select_related('task', 'task__user')
sent_count = 0
for reminder in due_reminders:
task = reminder.task
if not task.is_deleted and task.status not in ('completed', 'cancelled'):
title, body = REMINDER_MESSAGES[reminder.reminder_type](task)
if notify_user(task.user, title, body, reminder.reminder_type, task=task, url=f'/tasks/{task.id}/'):
sent_count += 1
reminder.is_sent = True
reminder.sent_at = timezone.now()
reminder.save(update_fields=['is_sent', 'sent_at'])
return sent_count
@shared_task
def process_recurring_tasks():
"""