forked from jtauber-archive/django-notification
-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add some docs on scoping notifications
- Loading branch information
Showing
4 changed files
with
97 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Scoping Notifications | ||
|
||
Sometimes you have a site that has groups or teams. Perhaps you are using | ||
[pinax-teams](https://github.com/pinax/pinax-teams/). If this is the case you | ||
likely want users who might be members of multiple teams to be able to set | ||
their notification preferences on a per team or group basis. | ||
|
||
You will need to: | ||
|
||
* Create a custom model for settings | ||
* Override NoticeSettingsView | ||
|
||
|
||
## Custom Model for Settings | ||
|
||
First thing you need to do is subclass `pinax.notifications.models.NoticeSettingBase` | ||
and add whatever attributes needed to provide the scoping. For example, to | ||
scope by a `pinax-teams` team: | ||
|
||
# models.py | ||
from pinax.notifications.models import NoticeSettingsBase | ||
|
||
class TeamNoticeSetting(NoticeSettingsBase): | ||
team = models.ForeignKey(Team, null=True) | ||
@classmethod | ||
def get_lookup_kwargs(cls, user, notice_type, medium, scoping): | ||
return { | ||
"user": user, | ||
"notice_type": notice_type, | ||
"medium": medium, | ||
"team": scoping | ||
} | ||
|
||
# settings.py | ||
PINAX_NOTIFICATIONS_SETTING_MODEL = "mysite.TeamNoticeSetting" | ||
|
||
|
||
## Override NoticeSettingsView | ||
|
||
Next, let's override the method on `pinax.notifications.views.NoticeSettingsView` | ||
that handles the lookup of the `setting` for each notice type and channel. | ||
|
||
# views.py | ||
from pinax.notifications.views import NoticeSettingsView | ||
|
||
class TeamNoticeSettingsView(NoticeSettingsView): | ||
def setting_for_user(self, notice_type, medium_id): | ||
return self.SettingModel.for_user( | ||
self.request.user, | ||
notice_type, | ||
medium_id, | ||
scoping=self.request.team | ||
) | ||
|
||
# urls.py | ||
from django.conf.urls import patterns, url | ||
|
||
from .views import TeamNoticeSettingsView | ||
|
||
|
||
urlpatterns = patterns( | ||
"", | ||
... | ||
url(r"^notifications/settings/$", TeamNoticeSettingsView.as_view(), name="notification_notice_settings"), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters