feat(notifications): add scheduled email notification system
- NotificationSchedule + NotificationRecipient models with initial migration - emails.py BUILDERS registry + events_expiring_this_week HTML email builder (IST week bounds) - send_scheduled_notifications management command (croniter due-check + select_for_update(skip_locked)) - 6 admin API endpoints under /api/v1/notifications/ (types, schedules CRUD, recipients CRUD, send-now) - date_from/date_to filters on EventListView for dashboard card - croniter>=2.0.0 added to requirements Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
181
notifications/emails.py
Normal file
181
notifications/emails.py
Normal file
@@ -0,0 +1,181 @@
|
||||
"""HTML email builders for scheduled admin notifications.
|
||||
|
||||
Each builder is registered in ``BUILDERS`` keyed by ``NotificationSchedule.notification_type``
|
||||
and returns ``(subject, html_body)``. Add new types by appending to the registry
|
||||
and extending ``NotificationSchedule.TYPE_CHOICES``.
|
||||
|
||||
Week bounds for ``events_expiring_this_week`` are computed in Asia/Kolkata so the
|
||||
"this week" semantics match the operations team's wall-clock week regardless of
|
||||
``settings.TIME_ZONE`` (currently UTC).
|
||||
"""
|
||||
from datetime import date, datetime, timedelta
|
||||
from html import escape
|
||||
|
||||
try:
|
||||
from zoneinfo import ZoneInfo
|
||||
except ImportError: # pragma: no cover — fallback for py<3.9
|
||||
from backports.zoneinfo import ZoneInfo # type: ignore
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.mail import EmailMessage
|
||||
from django.db.models import Q
|
||||
|
||||
from eventify_logger.services import log
|
||||
|
||||
|
||||
IST = ZoneInfo('Asia/Kolkata')
|
||||
|
||||
|
||||
def _today_in_ist() -> date:
|
||||
return datetime.now(IST).date()
|
||||
|
||||
|
||||
def _upcoming_week_bounds(today: date) -> tuple[date, date]:
|
||||
"""Return (next Monday, next Sunday) inclusive.
|
||||
|
||||
If today is Monday the result is *this week* (today..Sunday).
|
||||
If today is any other weekday the result is *next week* (next Monday..next Sunday).
|
||||
Mon=0 per Python ``weekday()``.
|
||||
"""
|
||||
days_until_monday = (7 - today.weekday()) % 7
|
||||
monday = today + timedelta(days=days_until_monday)
|
||||
sunday = monday + timedelta(days=6)
|
||||
return monday, sunday
|
||||
|
||||
|
||||
def _build_events_expiring_this_week(schedule) -> tuple[str, str]:
|
||||
from events.models import Event
|
||||
|
||||
today = _today_in_ist()
|
||||
monday, sunday = _upcoming_week_bounds(today)
|
||||
|
||||
qs = (
|
||||
Event.objects
|
||||
.select_related('partner', 'event_type')
|
||||
.filter(event_status='published')
|
||||
.filter(
|
||||
Q(end_date__isnull=False, end_date__gte=monday, end_date__lte=sunday)
|
||||
| Q(end_date__isnull=True, start_date__gte=monday, start_date__lte=sunday)
|
||||
)
|
||||
.order_by('end_date', 'start_date', 'name')
|
||||
)
|
||||
|
||||
events = list(qs)
|
||||
rows_html = ''
|
||||
for e in events:
|
||||
end = e.end_date or e.start_date
|
||||
title = e.title or e.name or '(untitled)'
|
||||
partner_name = ''
|
||||
if e.partner_id:
|
||||
try:
|
||||
partner_name = e.partner.name or ''
|
||||
except Exception:
|
||||
partner_name = ''
|
||||
category = ''
|
||||
if e.event_type_id and e.event_type:
|
||||
category = getattr(e.event_type, 'event_type', '') or ''
|
||||
rows_html += (
|
||||
'<tr>'
|
||||
f'<td style="padding:10px 12px;border-bottom:1px solid #eee;">{escape(title)}</td>'
|
||||
f'<td style="padding:10px 12px;border-bottom:1px solid #eee;">{escape(partner_name or "—")}</td>'
|
||||
f'<td style="padding:10px 12px;border-bottom:1px solid #eee;">{escape(category or "—")}</td>'
|
||||
f'<td style="padding:10px 12px;border-bottom:1px solid #eee;">'
|
||||
f'{end.strftime("%a %d %b %Y") if end else "—"}</td>'
|
||||
'</tr>'
|
||||
)
|
||||
|
||||
if not events:
|
||||
rows_html = (
|
||||
'<tr><td colspan="4" style="padding:24px;text-align:center;color:#888;">'
|
||||
'No published events are expiring next week.'
|
||||
'</td></tr>'
|
||||
)
|
||||
|
||||
subject = (
|
||||
f'[Eventify] {len(events)} event(s) expiring '
|
||||
f'{monday.strftime("%d %b")}–{sunday.strftime("%d %b")}'
|
||||
)
|
||||
|
||||
html = f"""<!doctype html>
|
||||
<html><body style="margin:0;padding:0;background:#f5f5f5;font-family:Arial,Helvetica,sans-serif;color:#1a1a1a;">
|
||||
<table role="presentation" width="100%" cellspacing="0" cellpadding="0" style="background:#f5f5f5;">
|
||||
<tr><td align="center" style="padding:24px 12px;">
|
||||
<table role="presentation" width="640" cellspacing="0" cellpadding="0" style="max-width:640px;background:#ffffff;border-radius:10px;overflow:hidden;box-shadow:0 2px 6px rgba(15,69,207,0.08);">
|
||||
<tr><td style="background:#0F45CF;color:#ffffff;padding:24px 28px;">
|
||||
<h2 style="margin:0;font-size:20px;">Events expiring next week</h2>
|
||||
<p style="margin:6px 0 0;color:#d2dcff;font-size:14px;">
|
||||
{monday.strftime("%A %d %b %Y")} → {sunday.strftime("%A %d %b %Y")}
|
||||
· {len(events)} event(s)
|
||||
</p>
|
||||
</td></tr>
|
||||
<tr><td style="padding:20px 24px;">
|
||||
<p style="margin:0 0 12px;font-size:14px;color:#444;">
|
||||
Scheduled notification: <strong>{escape(schedule.name)}</strong>
|
||||
</p>
|
||||
<table role="presentation" width="100%" cellspacing="0" cellpadding="0" style="border-collapse:collapse;font-size:14px;">
|
||||
<thead>
|
||||
<tr style="background:#f0f4ff;color:#0F45CF;">
|
||||
<th align="left" style="padding:10px 12px;">Title</th>
|
||||
<th align="left" style="padding:10px 12px;">Partner</th>
|
||||
<th align="left" style="padding:10px 12px;">Category</th>
|
||||
<th align="left" style="padding:10px 12px;">End date</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>{rows_html}</tbody>
|
||||
</table>
|
||||
</td></tr>
|
||||
<tr><td style="padding:16px 24px 24px;color:#888;font-size:12px;">
|
||||
Sent automatically by Eventify Command Center.
|
||||
To change recipients or the schedule, open
|
||||
<a href="https://admin.eventifyplus.com/settings" style="color:#0F45CF;">admin.eventifyplus.com › Settings › Notifications</a>.
|
||||
</td></tr>
|
||||
</table>
|
||||
</td></tr>
|
||||
</table>
|
||||
</body></html>"""
|
||||
|
||||
return subject, html
|
||||
|
||||
|
||||
BUILDERS: dict = {
|
||||
'events_expiring_this_week': _build_events_expiring_this_week,
|
||||
}
|
||||
|
||||
|
||||
def render_and_send(schedule) -> int:
|
||||
"""Render the email for ``schedule`` and deliver it to active recipients.
|
||||
|
||||
Returns the number of recipients the message was sent to. Raises on SMTP
|
||||
failure so the management command can mark the schedule as errored.
|
||||
"""
|
||||
builder = BUILDERS.get(schedule.notification_type)
|
||||
if builder is None:
|
||||
raise ValueError(f'No builder for notification type: {schedule.notification_type}')
|
||||
|
||||
subject, html = builder(schedule)
|
||||
recipients = list(
|
||||
schedule.recipients.filter(is_active=True).values_list('email', flat=True)
|
||||
)
|
||||
if not recipients:
|
||||
log('warning', 'notification schedule has no active recipients', logger_data={
|
||||
'schedule_id': schedule.id,
|
||||
'schedule_name': schedule.name,
|
||||
})
|
||||
return 0
|
||||
|
||||
msg = EmailMessage(
|
||||
subject=subject,
|
||||
body=html,
|
||||
from_email=settings.DEFAULT_FROM_EMAIL,
|
||||
to=recipients,
|
||||
)
|
||||
msg.content_subtype = 'html'
|
||||
msg.send(fail_silently=False)
|
||||
|
||||
log('info', 'notification email sent', logger_data={
|
||||
'schedule_id': schedule.id,
|
||||
'schedule_name': schedule.name,
|
||||
'type': schedule.notification_type,
|
||||
'recipient_count': len(recipients),
|
||||
})
|
||||
return len(recipients)
|
||||
0
notifications/management/__init__.py
Normal file
0
notifications/management/__init__.py
Normal file
0
notifications/management/commands/__init__.py
Normal file
0
notifications/management/commands/__init__.py
Normal file
@@ -0,0 +1,153 @@
|
||||
"""Dispatch due ``NotificationSchedule`` jobs.
|
||||
|
||||
Host cron invokes this every ~15 minutes via ``docker exec``. The command
|
||||
walks all active schedules, evaluates their cron expression against
|
||||
``last_run_at`` using ``croniter``, and fires any that are due. A row-level
|
||||
``select_for_update(skip_locked=True)`` prevents duplicate sends if two cron
|
||||
ticks race or the container is restarted mid-run.
|
||||
|
||||
Evaluation timezone is **Asia/Kolkata** to match
|
||||
``notifications/emails.py::_upcoming_week_bounds`` — the same wall-clock week
|
||||
used in the outgoing email body.
|
||||
|
||||
Flags:
|
||||
--schedule-id <id> Fire exactly one schedule, ignoring cron check.
|
||||
--dry-run Resolve due schedules + render emails, send nothing.
|
||||
"""
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
try:
|
||||
from zoneinfo import ZoneInfo
|
||||
except ImportError: # pragma: no cover — py<3.9
|
||||
from backports.zoneinfo import ZoneInfo # type: ignore
|
||||
|
||||
from croniter import croniter
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
from django.db import transaction
|
||||
from django.utils import timezone
|
||||
|
||||
from eventify_logger.services import log
|
||||
from notifications.emails import BUILDERS, render_and_send
|
||||
from notifications.models import NotificationSchedule
|
||||
|
||||
|
||||
IST = ZoneInfo('Asia/Kolkata')
|
||||
|
||||
|
||||
def _is_due(schedule: NotificationSchedule, now_ist: datetime) -> bool:
|
||||
"""Return True if ``schedule`` should fire at ``now_ist``.
|
||||
|
||||
``croniter`` is seeded with ``last_run_at`` (or one year ago for a fresh
|
||||
schedule) and asked for the next fire time. If that time has already
|
||||
passed relative to ``now_ist`` the schedule is due.
|
||||
"""
|
||||
if not croniter.is_valid(schedule.cron_expression):
|
||||
return False
|
||||
|
||||
if schedule.last_run_at is not None:
|
||||
seed = schedule.last_run_at.astimezone(IST)
|
||||
else:
|
||||
seed = now_ist - timedelta(days=365)
|
||||
|
||||
itr = croniter(schedule.cron_expression, seed)
|
||||
next_fire = itr.get_next(datetime)
|
||||
return next_fire <= now_ist
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Dispatch due NotificationSchedule email jobs.'
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument(
|
||||
'--schedule-id', type=int, default=None,
|
||||
help='Force-run a single schedule by ID, ignoring cron check.',
|
||||
)
|
||||
parser.add_argument(
|
||||
'--dry-run', action='store_true',
|
||||
help='Render and log but do not send or persist last_run_at.',
|
||||
)
|
||||
|
||||
def handle(self, *args, **opts):
|
||||
schedule_id = opts.get('schedule_id')
|
||||
dry_run = opts.get('dry_run', False)
|
||||
|
||||
now_ist = datetime.now(IST)
|
||||
qs = NotificationSchedule.objects.filter(is_active=True)
|
||||
if schedule_id is not None:
|
||||
qs = qs.filter(id=schedule_id)
|
||||
|
||||
candidate_ids = list(qs.values_list('id', flat=True))
|
||||
if not candidate_ids:
|
||||
self.stdout.write('No active schedules to evaluate.')
|
||||
return
|
||||
|
||||
fired = 0
|
||||
skipped = 0
|
||||
errored = 0
|
||||
|
||||
for sid in candidate_ids:
|
||||
with transaction.atomic():
|
||||
locked_qs = (
|
||||
NotificationSchedule.objects
|
||||
.select_for_update(skip_locked=True)
|
||||
.filter(id=sid, is_active=True)
|
||||
)
|
||||
schedule = locked_qs.first()
|
||||
if schedule is None:
|
||||
skipped += 1
|
||||
continue
|
||||
|
||||
forced = schedule_id is not None
|
||||
if not forced and not _is_due(schedule, now_ist):
|
||||
skipped += 1
|
||||
continue
|
||||
|
||||
if schedule.notification_type not in BUILDERS:
|
||||
schedule.last_status = NotificationSchedule.STATUS_ERROR
|
||||
schedule.last_error = (
|
||||
f'No builder registered for {schedule.notification_type!r}'
|
||||
)
|
||||
schedule.save(update_fields=['last_status', 'last_error', 'updated_at'])
|
||||
errored += 1
|
||||
continue
|
||||
|
||||
if dry_run:
|
||||
self.stdout.write(
|
||||
f'[dry-run] would fire schedule {schedule.id} '
|
||||
f'({schedule.name}) type={schedule.notification_type}'
|
||||
)
|
||||
fired += 1
|
||||
continue
|
||||
|
||||
try:
|
||||
recipient_count = render_and_send(schedule)
|
||||
except Exception as exc: # noqa: BLE001 — wide catch, store msg
|
||||
log('error', 'notification dispatch failed', logger_data={
|
||||
'schedule_id': schedule.id,
|
||||
'schedule_name': schedule.name,
|
||||
'error': str(exc),
|
||||
})
|
||||
schedule.last_status = NotificationSchedule.STATUS_ERROR
|
||||
schedule.last_error = str(exc)[:2000]
|
||||
schedule.save(update_fields=['last_status', 'last_error', 'updated_at'])
|
||||
errored += 1
|
||||
continue
|
||||
|
||||
schedule.last_run_at = timezone.now()
|
||||
schedule.last_status = NotificationSchedule.STATUS_SUCCESS
|
||||
schedule.last_error = ''
|
||||
schedule.save(update_fields=[
|
||||
'last_run_at', 'last_status', 'last_error', 'updated_at',
|
||||
])
|
||||
fired += 1
|
||||
self.stdout.write(
|
||||
f'Fired schedule {schedule.id} ({schedule.name}) '
|
||||
f'→ {recipient_count} recipient(s)'
|
||||
)
|
||||
|
||||
summary = f'Done. fired={fired} skipped={skipped} errored={errored}'
|
||||
self.stdout.write(summary)
|
||||
log('info', 'send_scheduled_notifications complete', logger_data={
|
||||
'fired': fired, 'skipped': skipped, 'errored': errored,
|
||||
'dry_run': dry_run, 'forced_id': schedule_id,
|
||||
})
|
||||
93
notifications/migrations/0001_initial.py
Normal file
93
notifications/migrations/0001_initial.py
Normal file
@@ -0,0 +1,93 @@
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Notification',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('title', models.CharField(max_length=255)),
|
||||
('message', models.TextField()),
|
||||
('notification_type', models.CharField(
|
||||
choices=[
|
||||
('event', 'Event'),
|
||||
('promo', 'Promotion'),
|
||||
('system', 'System'),
|
||||
('booking', 'Booking'),
|
||||
],
|
||||
default='system', max_length=20,
|
||||
)),
|
||||
('is_read', models.BooleanField(default=False)),
|
||||
('action_url', models.URLField(blank=True, null=True)),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('user', models.ForeignKey(
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name='notifications',
|
||||
to=settings.AUTH_USER_MODEL,
|
||||
)),
|
||||
],
|
||||
options={'ordering': ['-created_at']},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='NotificationSchedule',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('name', models.CharField(max_length=200)),
|
||||
('notification_type', models.CharField(
|
||||
choices=[('events_expiring_this_week', 'Events Expiring This Week')],
|
||||
db_index=True, max_length=64,
|
||||
)),
|
||||
('cron_expression', models.CharField(
|
||||
default='0 0 * * 1',
|
||||
help_text=(
|
||||
'Standard 5-field cron (minute hour dom month dow). '
|
||||
'Evaluated in Asia/Kolkata.'
|
||||
),
|
||||
max_length=100,
|
||||
)),
|
||||
('is_active', models.BooleanField(db_index=True, default=True)),
|
||||
('last_run_at', models.DateTimeField(blank=True, null=True)),
|
||||
('last_status', models.CharField(blank=True, default='', max_length=20)),
|
||||
('last_error', models.TextField(blank=True, default='')),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
],
|
||||
options={'ordering': ['-created_at']},
|
||||
),
|
||||
migrations.AddIndex(
|
||||
model_name='notificationschedule',
|
||||
index=models.Index(
|
||||
fields=['is_active', 'notification_type'],
|
||||
name='notificatio_is_acti_26dfb5_idx',
|
||||
),
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='NotificationRecipient',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('email', models.EmailField(max_length=254)),
|
||||
('display_name', models.CharField(blank=True, default='', max_length=200)),
|
||||
('is_active', models.BooleanField(default=True)),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('schedule', models.ForeignKey(
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name='recipients',
|
||||
to='notifications.notificationschedule',
|
||||
)),
|
||||
],
|
||||
options={
|
||||
'ordering': ['display_name', 'email'],
|
||||
'unique_together': {('schedule', 'email')},
|
||||
},
|
||||
),
|
||||
]
|
||||
0
notifications/migrations/__init__.py
Normal file
0
notifications/migrations/__init__.py
Normal file
@@ -1,4 +1,16 @@
|
||||
"""
|
||||
Two distinct concerns live in this app:
|
||||
|
||||
1. ``Notification`` — consumer-facing in-app inbox entries surfaced on the mobile
|
||||
SPA (/api/notifications/list/). One row per user per alert.
|
||||
|
||||
2. ``NotificationSchedule`` + ``NotificationRecipient`` — admin-side recurring
|
||||
email jobs configured from the Command Center Settings tab and dispatched by
|
||||
the ``send_scheduled_notifications`` management command (host cron).
|
||||
Not user-facing; strictly operational.
|
||||
"""
|
||||
from django.db import models
|
||||
|
||||
from accounts.models import User
|
||||
|
||||
|
||||
@@ -23,3 +35,68 @@ class Notification(models.Model):
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.notification_type}: {self.title} → {self.user.email}"
|
||||
|
||||
|
||||
class NotificationSchedule(models.Model):
|
||||
"""One configurable recurring email job.
|
||||
|
||||
New types are added by registering a builder in ``notifications/emails.py``
|
||||
and adding the slug to ``TYPE_CHOICES`` below. Cron expression is evaluated
|
||||
in ``Asia/Kolkata`` by the dispatcher (matches operations team timezone).
|
||||
"""
|
||||
|
||||
TYPE_EVENTS_EXPIRING_THIS_WEEK = 'events_expiring_this_week'
|
||||
|
||||
TYPE_CHOICES = [
|
||||
(TYPE_EVENTS_EXPIRING_THIS_WEEK, 'Events Expiring This Week'),
|
||||
]
|
||||
|
||||
STATUS_SUCCESS = 'success'
|
||||
STATUS_ERROR = 'error'
|
||||
|
||||
name = models.CharField(max_length=200)
|
||||
notification_type = models.CharField(
|
||||
max_length=64, choices=TYPE_CHOICES, db_index=True,
|
||||
)
|
||||
cron_expression = models.CharField(
|
||||
max_length=100, default='0 0 * * 1',
|
||||
help_text='Standard 5-field cron (minute hour dom month dow). '
|
||||
'Evaluated in Asia/Kolkata.',
|
||||
)
|
||||
is_active = models.BooleanField(default=True, db_index=True)
|
||||
last_run_at = models.DateTimeField(null=True, blank=True)
|
||||
last_status = models.CharField(max_length=20, blank=True, default='')
|
||||
last_error = models.TextField(blank=True, default='')
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
class Meta:
|
||||
ordering = ['-created_at']
|
||||
indexes = [models.Index(fields=['is_active', 'notification_type'])]
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.name} ({self.notification_type})'
|
||||
|
||||
|
||||
class NotificationRecipient(models.Model):
|
||||
"""Free-form recipient — not tied to a User row so external stakeholders
|
||||
(vendors, partners, sponsors) can receive notifications without needing
|
||||
platform accounts."""
|
||||
|
||||
schedule = models.ForeignKey(
|
||||
NotificationSchedule,
|
||||
on_delete=models.CASCADE,
|
||||
related_name='recipients',
|
||||
)
|
||||
email = models.EmailField()
|
||||
display_name = models.CharField(max_length=200, blank=True, default='')
|
||||
is_active = models.BooleanField(default=True)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
class Meta:
|
||||
unique_together = [('schedule', 'email')]
|
||||
ordering = ['display_name', 'email']
|
||||
|
||||
def __str__(self):
|
||||
label = self.display_name or self.email
|
||||
return f'{label} ({self.schedule.name})'
|
||||
|
||||
Reference in New Issue
Block a user