Skip to content

Commit 01d2cfb

Browse files
authored
feat: Change obj field to GenericForeignKey (#73)
* chore: reformat with black * dep: Upgrade django-json-field backport to 1.0.4 * dep: Upgrade `Django` to >= 2.2
1 parent c8c4089 commit 01d2cfb

30 files changed

+330
-144
lines changed

docs/conf.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@
3030
# Add any Sphinx extension module names here, as strings. They can be
3131
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
3232
# ones.
33-
extensions = [
34-
'm2r2',
35-
'sphinx_rtd_theme',
36-
'sphinx.ext.autosectionlabel'
37-
]
33+
extensions = ['m2r2', 'sphinx_rtd_theme', 'sphinx.ext.autosectionlabel']
3834

3935
# Add any paths that contain templates here, relative to this directory.
4036
templates_path = ['_templates']

docs/usage.rst

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ To Create/Send a notification import the notify function and call it with the fo
1818
'recipient': recipent_user, 'category': 'Chat',
1919
'action': 'Sent', 'obj': message.id,
2020
'short_description': 'You a new message', 'url': url,
21-
'channels': ('email', 'websocket', 'slack'), 'silent': True
21+
'channels': ('email', 'websocket', 'slack'), 'silent': True,
22+
'content_object': self.request.user
2223
}
2324
notify(**kwargs)
2425

@@ -32,14 +33,14 @@ Notification Fields
3233

3334
The fields in the `args` dictionary map to the fields in the `Notification` model
3435

35-
- **source: A ForeignKey to Django's User model (Can be null if it's not a User to User Notification).**
36+
- **source: A ForeignKey to Django's User model (optional if it's not a User to User Notification).**
3637
- **source_display_name: A User Friendly name for the source of the notification.**
3738
- **recipient: The Recipient of the notification. It's a ForeignKey to Django's User model.**
3839
- **category: Arbitrary category that can be used to group messages.**
3940
- **action: Verbal action for the notification E.g Sent, Cancelled, Bought e.t.c**
40-
- **obj: The id of the object associated with the notification (Can be null).**
41+
- **obj: An arbitrary object associated with the notification using the `contenttypes` app (optional).**
4142
- **short_description: The body of the notification.**
42-
- **url: The url of the object associated with the notification (Can be null).**
43+
- **url: The url of the object associated with the notification (optional).**
4344
- **silent: If this Value is set, the notification won't be persisted to the database.**
4445
- **extra_data: Arbitrary data as a dictionary.**
4546
- **channels: Delivery channels that should be used to deliver the message (Tuple/List)**
@@ -56,8 +57,6 @@ Simply pass in a dictionary as the extra_data argument.
5657
.. note::
5758
The dictionary is serialized using python's json module so make sure the dictionary contains objects that can be serialized by the json module
5859

59-
Internally, the JSON is stored as plain text with django's standard ``TextField``.
60-
6160

6261
Writing custom delivery channels
6362
--------------------------------

notifications/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
class NotificationError(Exception):
55
"""Custom error type for the app."""
6+
67
pass
78

89

notifications/backends/base.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55

66
class BaseBackend(metaclass=abc.ABCMeta):
7-
87
def __init__(self, notification):
98
self.notification = notification.to_json()
109

notifications/backends/celery.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
@shared_task(
2222
autoretry_for=autoretry_for,
2323
retry_backoff=settings.NOTIFICATIONS_RETRY_INTERVAL,
24-
max_retries=settings.NOTIFICATIONS_MAX_RETRIES
24+
max_retries=settings.NOTIFICATIONS_MAX_RETRIES,
2525
)
2626
def send_notification(notification, channel_alias):
2727
"""Send notification via a channel to celery."""
@@ -34,5 +34,5 @@ def run(self, countdown):
3434
send_notification.apply_async(
3535
args=[self.notification, channel_alias],
3636
queue=settings.NOTIFICATIONS_QUEUE_NAME,
37-
countdown=countdown
37+
countdown=countdown,
3838
)

notifications/backends/django_channels.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111

1212
class ChannelsBackend(BaseBackend):
13-
1413
def run(self, countdown):
1514
channel_layer = channels.layers.get_channel_layer(
1615
settings.NOTIFICATIONS_QUEUE_NAME
@@ -22,6 +21,7 @@ def run(self, countdown):
2221
{
2322
'notification': self.notification,
2423
'countdown': countdown,
25-
'channel_alias': channel_alias, 'type': 'notify'
26-
}
24+
'channel_alias': channel_alias,
25+
'type': 'notify',
26+
},
2727
)

notifications/backends/rq.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,19 @@
1818
if settings.NOTIFICATIONS_RETRY:
1919
retry = Retry(
2020
max=settings.NOTIFICATIONS_MAX_RETRIES,
21-
interval=settings.NOTIFICATIONS_RETRY_INTERVAL
21+
interval=settings.NOTIFICATIONS_RETRY_INTERVAL,
2222
)
2323

2424

2525
class RQBackend(BaseBackend):
26-
2726
def run(self, countdown):
2827
for channel_alias in self.notification['channels']:
2928
queue = django_rq.get_queue(settings.NOTIFICATIONS_QUEUE_NAME)
3029
queue.enqueue_in(
31-
timedelta(seconds=countdown), _send_notification,
32-
self.notification, channel_alias, logger, retry=retry
30+
timedelta(seconds=countdown),
31+
_send_notification,
32+
self.notification,
33+
channel_alias,
34+
logger,
35+
retry=retry,
3336
)

notifications/backends/synchronous.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313

1414
class SynchronousBackend(BaseBackend):
15-
1615
def run(self, countdown):
1716
for channel_alias in self.notification['channels']:
1817
time.sleep(countdown)

notifications/backends/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ def _send_notification(notification, channel_alias, logger):
1616
message = channel.construct_message()
1717
channel.notify(message)
1818
logger.info(
19-
'Sent notification with the %s channel. kwargs: %s\n' %
20-
(channel_alias, notification)
19+
'Sent notification with the %s channel. kwargs: %s\n'
20+
% (channel_alias, notification)
2121
)

notifications/channels/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from .console import ConsoleChannel # noqa
22
from .base import BaseNotificationChannel # noqa
3+
34
try:
45
from .channels_websocket import WebSocketChannel # noqa
56
except ImportError:

0 commit comments

Comments
 (0)