Skip to content

Commit 37b9339

Browse files
authored
feat: Add NOTIFICATIONS_MODEL setting (#74)
* feat: Change `obj` field to GenericForeignKey * dep: Upgrade django-json-field backport to 1.0.4 * dep: Upgrade `Django` to >= 2.2 * feat: Add NOTIFICATIONS_MODEL setting * fix: tests
1 parent 01d2cfb commit 37b9339

File tree

16 files changed

+350
-44
lines changed

16 files changed

+350
-44
lines changed

.travis.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,27 @@ matrix:
1818
# Python 3.6
1919
- python: 3.6
2020
if: branch != master
21-
env: TOXENV=py36-django22,py36-django30,py36-django31
21+
env: TOXENV=py36-django22,py36-django30,py36-django31,py36-django32
2222

2323
# Python 3.7
2424
- python: 3.7
2525
if: branch != master
26-
env: TOXENV=py37-django22,py37-django30,py37-django31
26+
env: TOXENV=py37-django22,py37-django30,py37-django31,py37-django32
2727

2828
# Python 3.8
2929
- python: 3.8
3030
if: branch != master
31-
env: TOXENV=py38-django22,py38-django30,py38-django31
31+
env: TOXENV=py38-django22,py38-django30,py38-django31,py38-django32
3232

3333
# Python 3.9
3434
- python: 3.9
3535
if: branch != master
36-
env: TOXENV=py39-django22,py39-django30,py39-django31
36+
env: TOXENV=py39-django22,py39-django30,py39-django31,py39-django32
3737

3838
# Python 3.9 Coverage
3939
- python: 3.9
4040
if: branch = master
41-
env: TOXENV=py39-django31
41+
env: TOXENV=py39-django31,py39-django32
4242
before_script:
4343
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
4444
- chmod +x ./cc-test-reporter

docs/configuration.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ Configuration
22
*************
33

44

5+
``NOTIFICATIONS_MODEL``
6+
--------------------------
7+
8+
``Default='notifications.models.Notification'``
9+
10+
This setting is used override the default database Model for saving notifications. Most users wouldn't need to override this
11+
but it can be useful if you're trying to integrate django-notifs into an existing project that already has it's own Notificaiton model
12+
513

614
``NOTIFICATIONS_CHANNELS``
715
--------------------------

docs/usage.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ channel class::
138138

139139
Delayed notification channel class::
140140

141-
from notifications.models import Notification
142141
from notifications.channels import BaseNotificationChannel
143142

144143

@@ -148,7 +147,7 @@ Delayed notification channel class::
148147
"""Cancel the delivery if the notification has been read"""
149148
# notification_id is only available if the notification isn't silent
150149
if self.notification_id:
151-
notification = Notification.objects.get(id=self.notification_id)
150+
notification = self.NotificationModel.objects.get(id=self.notification_id)
152151

153152
if notification.read is True:
154153
return

notifications/admin.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
from django.contrib import admin
44

5-
from .models import Notification
5+
from .utils import get_notification_model
6+
7+
8+
Notification = get_notification_model()
69

710

811
admin.site.register(Notification)

notifications/backends/utils.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ def _send_notification(notification, channel_alias, logger):
99
# Validate channel alias
1010
channel_path = _validate_channel_alias(channel_alias)
1111

12-
channel = _import_class_string(channel_path)(
13-
**notification, alias=channel_alias
14-
)
12+
channel = _import_class_string(channel_path)(**notification, alias=channel_alias)
1513

1614
message = channel.construct_message()
1715
channel.notify(message)

notifications/channels/base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import abc
22

3+
from notifications.utils import get_notification_model
4+
35

46
class BaseNotificationChannel(metaclass=abc.ABCMeta):
57
"""Base channel for sending notifications."""
@@ -8,6 +10,8 @@ def __init__(self, **kwargs):
810
self.notification_kwargs = kwargs
911
self.notification_id = kwargs['notification_id']
1012

13+
self.NotificationModel = get_notification_model()
14+
1115
@abc.abstractmethod
1216
def construct_message(self):
1317
"""Constructs a message from notification details."""

notifications/channels/channels_websocket.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ def _validate_notification_kwargs(self):
2121
notif_kwargs['extra_data'][self.destination_name]
2222
except KeyError:
2323
raise KeyError(
24-
f'`extra_kwargs` must contain the `{self.destination_name}`'
25-
'key'
24+
f'`extra_kwargs` must contain the `{self.destination_name}`' 'key'
2625
)
2726

2827
def construct_message(self):
@@ -35,9 +34,7 @@ def construct_message(self):
3534

3635
def notify(self, message):
3736
channel_layer = get_channel_layer()
38-
destination = self.notification_kwargs['extra_data'][
39-
self.destination_name
40-
]
37+
destination = self.notification_kwargs['extra_data'][self.destination_name]
4138

4239
async_to_sync(channel_layer.group_send)(
4340
destination,

notifications/consumers.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,13 @@ async def connect(self):
3838
]
3939

4040
# Join room group
41-
await self.channel_layer.group_add(
42-
self.room_group_name, self.channel_name
43-
)
41+
await self.channel_layer.group_add(self.room_group_name, self.channel_name)
4442

4543
await self.accept()
4644

4745
async def disconnect(self, close_code):
4846
"""Leave the group."""
49-
await self.channel_layer.group_discard(
50-
self.room_group_name, self.channel_name
51-
)
47+
await self.channel_layer.group_discard(self.room_group_name, self.channel_name)
5248

5349
async def receive(self, text_data):
5450
"""

notifications/default_settings.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515

1616
NOTIFICATIONS_RETRY = getattr(settings, 'NOTIFICATIONS_RETRY', False)
1717

18-
NOTIFICATIONS_RETRY_INTERVAL = getattr(
19-
settings, 'NOTIFICATIONS_RETRY_INTERVAL', 5
20-
)
18+
NOTIFICATIONS_RETRY_INTERVAL = getattr(settings, 'NOTIFICATIONS_RETRY_INTERVAL', 5)
2119

2220
NOTIFICATIONS_MAX_RETRIES = getattr(settings, 'NOTIFICATIONS_MAX_RETRIES', 5)
2321

notifications/migrations/0010_notification_channels.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ class Migration(migrations.Migration):
1414
migrations.AddField(
1515
model_name='notification',
1616
name='channels',
17-
field=notifications.fields.ListField(
18-
default=('console',), max_length=200
19-
), # noqa
17+
field=notifications.fields.ListField(default=('console',), max_length=200),
2018
preserve_default=False,
2119
),
2220
]

0 commit comments

Comments
 (0)