Multi-site companion for django-notifications-community.
Project description
django-notifications-community-sites
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.
NOTIFICATIONS_NOTIFICATION_MODEL must point at the companion model;
the system check will tell you if it doesn't (silence with
SILENCED_SYSTEM_CHECKS = ['notifications_sites.E003'] if you're
subclassing the companion).
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.
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.
Background jobs and other request-less callers
When notify.send() fires from a code path without a request,
Site.objects.get_current() falls back to the row whose primary key matches
SITE_ID. In a multi-tenant deployment that is rarely the right answer for
tenant N. Pass site= explicitly from background jobs:
notify.send(actor, recipient=user, verb='ping', site=tenant_site)
Request host wins over SITE_ID
When a request is in scope, the companion resolves the current site by Host
header first, falling back to SITE_ID only if no Site row matches. This
inverts Django's own Site.objects.get_current(request), which checks
SITE_ID before host. The companion's choice fits multi-tenant Django apps:
each request gets the site it came from; background jobs and other request-less
callers fall back to SITE_ID.
Practical implications:
- Every tenant domain needs a
Siterow. - Hosts that don't match any row (e.g.
testserver,localhostin dev) fall back to theSITE_ID-resolved row, so tests and local development keep working without per-host setup. - If you change
SITE_IDmid-process, callSite.objects.clear_cache()afterwards or the cached lookup will keep returning the previously-resolved row.
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 ifnotifications_sites_notificationalready 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_notifications_community_sites-1.0.2.tar.gz.
File metadata
- Download URL: django_notifications_community_sites-1.0.2.tar.gz
- Upload date:
- Size: 24.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9608c48573b2802919d4d779859c5955b810d2a8d4cb870f6de5c4eb62777e47
|
|
| MD5 |
fee9a0d3f4b85ce27da5194fe23dd37c
|
|
| BLAKE2b-256 |
c9f0c3cacd4853ceb757dc35f429442f30be26578836b94d1f4723983471ae6a
|
Provenance
The following attestation bundles were made for django_notifications_community_sites-1.0.2.tar.gz:
Publisher:
release.yml on django-notifications-community/django-notifications-community-sites
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_notifications_community_sites-1.0.2.tar.gz -
Subject digest:
9608c48573b2802919d4d779859c5955b810d2a8d4cb870f6de5c4eb62777e47 - Sigstore transparency entry: 1411717464
- Sigstore integration time:
-
Permalink:
django-notifications-community/django-notifications-community-sites@804b83a91f3be6858e9612a7ef1fa7d0a22ec7cc -
Branch / Tag:
refs/tags/v1.0.2 - Owner: https://github.com/django-notifications-community
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@804b83a91f3be6858e9612a7ef1fa7d0a22ec7cc -
Trigger Event:
push
-
Statement type:
File details
Details for the file django_notifications_community_sites-1.0.2-py3-none-any.whl.
File metadata
- Download URL: django_notifications_community_sites-1.0.2-py3-none-any.whl
- Upload date:
- Size: 16.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
81721549ea90063ffdcc589425ae40bc17b9172c80104bbe6b35ddb91d79371c
|
|
| MD5 |
844639236f2eb593a7a89aadd9018e7b
|
|
| BLAKE2b-256 |
f9acfa689ddea6abb81f614364d99e9023f2a7015bac956f109668fb10d53a69
|
Provenance
The following attestation bundles were made for django_notifications_community_sites-1.0.2-py3-none-any.whl:
Publisher:
release.yml on django-notifications-community/django-notifications-community-sites
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_notifications_community_sites-1.0.2-py3-none-any.whl -
Subject digest:
81721549ea90063ffdcc589425ae40bc17b9172c80104bbe6b35ddb91d79371c - Sigstore transparency entry: 1411717617
- Sigstore integration time:
-
Permalink:
django-notifications-community/django-notifications-community-sites@804b83a91f3be6858e9612a7ef1fa7d0a22ec7cc -
Branch / Tag:
refs/tags/v1.0.2 - Owner: https://github.com/django-notifications-community
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@804b83a91f3be6858e9612a7ef1fa7d0a22ec7cc -
Trigger Event:
push
-
Statement type: