Skip to main content

Multi-site companion for django-notifications-community.

Project description

django-notifications-community-sites

PyPI Python versions Django versions

Multi-site companion to django-notifications-community.

Adds a non-nullable site FK on each notification, a site-aware notify handler that stamps Site.objects.get_current() automatically, and a queryset filter that scopes views and the unread badge to the current site.

Why this exists

The base package is sites-blind by design, so installing it imposes no dependency on django.contrib.sites. If you do run more than one site from a single project (different domains, different SITE_ID), install this companion to keep notifications generated on one site from leaking to another.

Installation

Either install the base with the sites extra:

pip install "django-notifications-community[sites]"

or install this package directly (it pulls the base in as a dependency):

pip install django-notifications-community-sites

Setup

In settings.py:

INSTALLED_APPS = [
    ...,
    'django.contrib.sites',
    'notifications',
    'notifications_sites',   # must come AFTER 'notifications'
]

NOTIFICATIONS_NOTIFICATION_MODEL = 'notifications_sites.Notification'

SITE_ID = 1

Then run migrations:

python manage.py migrate

SITE_ID is required; the system check will tell you if it's missing.

How it works

This package registers callbacks against the base's notifications.registry extension hooks:

  • A queryset filter that adds .filter(site=current_site) wherever the base builds a Notification queryset.
  • A cache-key modifier that namespaces the unread-count cache key by site, so concurrent sites don't poison each other.
  • A cache-invalidation registration that drops all per-site cache variants on mutation.

The base's notify_handler is replaced with a site-aware variant that stamps Site.objects.get_current() on each notification (or whatever you pass via site=...).

The base's views, helpers, and template tags are reused unchanged through the hooks. No URL or template changes required.

Bringing existing data over

The companion does not auto-copy rows from notifications_notification into notifications_sites_notification. The reasoning: there's no correct default for which site a legacy row should belong to, and a migration that silently picks SITE_ID would be wrong for anyone running more than one site. Two options:

Start fresh. Install the companion as documented above. Future notifications land in notifications_sites_notification. Existing rows stay in notifications_notification until you drop the table yourself.

Copy legacy rows over with the bundled command. Run:

python manage.py copy_legacy_notifications --default-site=<pk>

<pk> is the Site primary key to assign to rows whose site_id is NULL, or to every row when the source table has no site_id column at all (i.e. you were on plain django-notifications-community, not Guillaume Libersat's feature/sites-framework branch). Existing site_id values are preserved.

Useful flags:

  • --dry-run — report what would be copied without writing.
  • --force — copy even if notifications_sites_notification already has rows (off by default to avoid duplicates).

The copy runs in a single transaction. After verifying everything landed, you can drop notifications_notification yourself.

License

BSD-3-Clause. See LICENSE.txt.

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

django_notifications_community_sites-1.0.0.tar.gz (19.3 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file django_notifications_community_sites-1.0.0.tar.gz.

File metadata

File hashes

Hashes for django_notifications_community_sites-1.0.0.tar.gz
Algorithm Hash digest
SHA256 95d5853841c6f416928e11a4a076f32f11183fa630840a690fd9b9bbdd227ecb
MD5 92ad9960d7e8d7db3e10311176623583
BLAKE2b-256 e79a5f1ac5e387bacb68c0a3b282e581ff07f18926fb5dc13e74bc4ea4b1e6a3

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_notifications_community_sites-1.0.0.tar.gz:

Publisher: release.yml on django-notifications-community/django-notifications-community-sites

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_notifications_community_sites-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_notifications_community_sites-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bb7eff187aa4d34a18e08c8fcd95be3b97dd21fa217587cc8d7eae11c68125ae
MD5 bd207716424d716bff35b100d23d7c55
BLAKE2b-256 51430d22ae46c98442f21dec701f19ecc3a3e82828645d3057b9f4bd8863c1f7

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_notifications_community_sites-1.0.0-py3-none-any.whl:

Publisher: release.yml on django-notifications-community/django-notifications-community-sites

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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