Skip to main content

Event-driven transactional notifications for Django booking and SaaS workflows.

Project description

mroudai-django-notifications

Event-driven transactional notifications for Django (app label: notifications). Email-first, tenant-aware optional, works without Celery.

What it does

  • Store notification templates per event/channel (optionally scoped to a tenant) with fallback to global templates.
  • Enqueue immediate or scheduled notification jobs with idempotency keys.
  • Inline sending (optional) plus a worker-friendly dispatcher with retries, backoff, and attempt logs.
  • Email delivery via Django email backends; SMS/WhatsApp hooks are stubbed for future backends.
  • Admin screens for templates, jobs, and attempts with requeue/cancel actions.

What it avoids

  • No booking/payment logic, slot generation, or user-facing UI.
  • No external queue required; works entirely on Django + DB.

Quick install

  1. Install the package: pip install mroudai-django-notifications
  2. Add to INSTALLED_APPS: "notifications".
  3. Configure settings as needed (defaults shown below).
  4. Run migrations: python manage.py migrate.

Key settings (defaults)

NOTIFY_TENANT_MODEL = None  # e.g. "tenants.Tenant"
NOTIFY_USER_MODEL = None    # defaults to AUTH_USER_MODEL
NOTIFY_EMAIL_FROM_DEFAULT = "no-reply@example.com"
NOTIFY_EMAIL_PROVIDER = "django"
NOTIFY_SEND_IMMEDIATELY = True
NOTIFY_MAX_ATTEMPTS = 5
NOTIFY_RETRY_BACKOFF_SECONDS = [60, 300, 1800, 7200]
NOTIFY_LOCK_TIMEOUT_SECONDS = 300
NOTIFY_ALLOW_GLOBAL_TEMPLATE_FALLBACK = True

Creating templates

  • Via Django admin: add a Notification Template for each event_key/channel, optionally tied to a tenant.
  • Or via shell:
from notifications.models import NotificationTemplate, NotificationChannel

NotificationTemplate.objects.create(
    event_key="booking.confirmed",
    channel=NotificationChannel.EMAIL,
    name="Booking confirmation",
    subject="Your booking is confirmed, {{ name }}",
    body_text="Hi {{ name }}, your booking is confirmed.",
    body_html="<p>Hi {{ name }}, your booking is confirmed.</p>",
)

Enqueue a notification

from notifications.services import enqueue_notification
from notifications.models import NotificationChannel

job = enqueue_notification(
    event_key="booking.confirmed",
    channel=NotificationChannel.EMAIL,
    to={"email": "person@example.com", "name": "Alex"},
    context={"name": "Alex"},
    # tenant=<Tenant instance>  # if NOTIFY_TENANT_MODEL is set
)
  • Jobs scheduled in the past are treated as immediate.
  • Idempotency keys are required; they are auto-generated unless you provide one.

Run the worker (cron-friendly)

Use the bundled management command—no Celery required:

python manage.py send_notifications --limit 50 --worker-id "worker-1"
  • It locks due jobs, sends them, and reschedules failures with backoff until NOTIFY_MAX_ATTEMPTS is reached.

Tenancy

  • Set NOTIFY_TENANT_MODEL (e.g. "tenants.Tenant") to enable tenant scoping.
  • If NOTIFY_ALLOW_GLOBAL_TEMPLATE_FALLBACK is True, tenant-specific templates are preferred and global templates (tenant null) are used as a fallback.

Channels

  • Email is implemented via Django's email backends (respects NOTIFY_EMAIL_FROM_DEFAULT).
  • SMS and WhatsApp are intentionally stubbed; extend notifications.channels with real backends when needed.

Tests

Run the included test suite with SQLite + locmem email:

python test notifications

Publish to PyPI

Build and upload using the helper script (auto-installs twine and build if missing; supply credentials via environment variables):

python upload project
  • Add --repository-url https://test.pypi.org/legacy/ to publish to TestPyPI.
  • Use --skip-build if you already have fresh artifacts in dist/.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mroudai_django_notifications-0.1.0.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mroudai_django_notifications-0.1.0-py3-none-any.whl (19.9 kB view details)

Uploaded Python 3

File details

Details for the file mroudai_django_notifications-0.1.0.tar.gz.

File metadata

File hashes

Hashes for mroudai_django_notifications-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3baf0e067e79767d8252ec581d7e69b5c62a89d1d874c3b482796dca7a1bc090
MD5 d2deb669af4655a4387468ae99051cc8
BLAKE2b-256 5f4505cb426c10084085f718c50db377222c61d36c45abde3d28d0e216022f60

See more details on using hashes here.

File details

Details for the file mroudai_django_notifications-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for mroudai_django_notifications-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fc9c9e032bc7b61af0970ea87f8ab1ecf37af5f5c2aa3f07474c82793a9c2a81
MD5 d2409bc3c0891aecd1d1d451cec17ff9
BLAKE2b-256 7a13f42e12d43a4fc380d45afbd8587ea9f95270c481d7d9bf3cfd3a36f9203b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page