Files
KeepItGoingServer/notifications/models.py
T
Keith SmithandClaude Sonnet 5 5359bf243a 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>
2026-09-05 09:46:12 -06:00

81 lines
2.4 KiB
Python

"""
Notification models for KeepItGoing.
"""
import uuid
from django.conf import settings
from django.db import models
class Notification(models.Model):
"""
Stores notifications for users.
"""
NOTIFICATION_TYPES = [
('reminder', 'Task Reminder'),
('due_soon', 'Due Soon'),
('overdue', 'Overdue'),
('shared', 'Task Shared'),
('comment', 'Comment'),
('daily_email', 'Daily Email'),
]
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
related_name='notifications'
)
notification_type = models.CharField(max_length=20, choices=NOTIFICATION_TYPES)
title = models.CharField(max_length=255)
message = models.TextField()
task = models.ForeignKey(
'tasks.Task',
on_delete=models.CASCADE,
null=True,
blank=True,
related_name='notifications'
)
is_read = models.BooleanField(default=False)
read_at = models.DateTimeField(null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
class Meta:
db_table = 'notifications'
ordering = ['-created_at']
def __str__(self):
return f"{self.notification_type}: {self.title}"
class ScheduledReminder(models.Model):
"""
Tracks scheduled reminders for tasks. Rows are (re)created by
Task.reschedule_reminders() whenever a task's due info changes, and
consumed by notifications.tasks.send_scheduled_reminders().
"""
REMINDER_TYPES = [
('reminder', 'Before Due'),
('due_soon', 'Due Now'),
('overdue', 'Overdue'),
]
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
task = models.ForeignKey(
'tasks.Task',
on_delete=models.CASCADE,
related_name='scheduled_reminders'
)
reminder_type = models.CharField(max_length=20, choices=REMINDER_TYPES, default='reminder')
remind_at = models.DateTimeField()
is_sent = models.BooleanField(default=False)
sent_at = models.DateTimeField(null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
class Meta:
db_table = 'scheduled_reminders'
ordering = ['remind_at']
def __str__(self):
return f"{self.get_reminder_type_display()} for {self.task.title} at {self.remind_at}"