Skip to content

Commit f5aeca5

Browse files
authored
Add django sms backend (#80)
* Add django sms backend
1 parent 902b7d3 commit f5aeca5

File tree

9 files changed

+122
-23
lines changed

9 files changed

+122
-23
lines changed

docs/backends.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,10 @@ The ``sqs-lambda-worker`` folder includes four files that are of interest:
131131

132132
You can use this file (after renaming it to ``.env``) to configure the environment variables for the autogenerated Lambda function.
133133
You can replace this step by:
134-
- Configuring the environment variables in your CI/CD environment **(Recommended)**
135-
- Exporting them in the current shell.
134+
135+
* Configuring the environment variables in your CI/CD environment **(Recommended)**
136+
* Exporting them in the current shell.
137+
136138
This is useful if you want to test the serverless deployment locally before moving it to your CI/CD
137139

138140
``requirements.txt``

docs/conf.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,31 @@
1717

1818
# -- Project information -----------------------------------------------------
1919

20+
import os
21+
import sys
22+
23+
import django
24+
2025
project = 'django-notifs'
2126
copyright = '2021, Daniel Osaetin'
2227
author = 'Daniel Osaetin'
2328

24-
# The full version, including alpha/beta/rc tags
25-
release = '2.6.4'
29+
sys.path.append(os.path.abspath('../../'))
30+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'notifs.settings')
31+
django.setup()
2632

2733

2834
# -- General configuration ---------------------------------------------------
2935

3036
# Add any Sphinx extension module names here, as strings. They can be
3137
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
3238
# ones.
33-
extensions = ['m2r2', 'sphinx_rtd_theme', 'sphinx.ext.autosectionlabel']
39+
extensions = [
40+
'm2r2',
41+
'sphinx_rtd_theme',
42+
'sphinx.ext.autosectionlabel',
43+
'sphinx.ext.autodoc',
44+
]
3445

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

docs/providers.rst

Lines changed: 65 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
Providers
22
**************
33

4-
.. _documentation: https://channels.readthedocs.io/en/stable/index.html
5-
.. _channels deployment documentation: https://channels.readthedocs.io/en/stable/deploying.html
4+
.. module:: notifications.providers
5+
6+
.. _django-sms: https://django-sms.readthedocs.io/en/stable/
7+
.. _django-sms documentation: https://django-sms.readthedocs.io/en/stable/
68

79
Django notifs comes with a set of inbuilt providers. These providers are typically classes that accept a payload
8-
and contain the logic for delivery that payload to an external service.
10+
and contain the logic for delivering the payload to an external service.
911

1012
Below are the list of supported providers:
1113

1214

1315
Email
1416
=====
1517

16-
The email provider uses the standard ``django.core.mail`` module.
17-
This opens up support for multiple ESP's (Mailjet, Mailchimp, sendgrid etc)
18+
.. autoclass:: EmailNotificationProvider
1819

1920
name: ``'email'``
2021

22+
The email provider uses the standard ``django.core.mail`` module.
23+
This opens up support for multiple ESP's (Mailjet, Mailchimp, sendgrid etc)
24+
2125

2226
Installation
2327
------------
@@ -30,7 +34,7 @@ Optional dependency for django-anymail::
3034
Settings
3135
--------
3236

33-
If you use ``django-anymail`` or a custom Email backend, all you have to do configure the settings as you'd
37+
If you use ``django-anymail`` or a custom Email backend, all you have to do configure the settings and dependencies as you'd
3438
normally do and the email provider should pick it up.
3539

3640

@@ -55,14 +59,58 @@ Single::
5559

5660
``extra_esp`` is any extra data that you want to pass to your custom Email backend.
5761

58-
59-
|
6062
|
6163
64+
SMS (with django-sms)
65+
=====================
66+
67+
.. autoclass:: DjangoSMSNotificationProvider
68+
69+
name: ``'django_sms'``
70+
71+
The SMS provider uses a third-party app called `django-sms`_ this also opens up support for multiple SMS providers.
72+
73+
Supported providers are:
74+
75+
* Twilio
76+
* Message bird
77+
78+
79+
Installation
80+
------------
81+
82+
::
83+
84+
pip install django-notifs[sms]
85+
86+
Extra dependencies can be installed by::
87+
88+
pip install django-sms[twilio,messagebird]
89+
90+
Settings
91+
--------
92+
93+
See the `django-sms documentation`_ for more information on how to configure your preferred backend. Once it is configured,
94+
``django-notifs`` should pick it up
95+
96+
Payload
97+
-------
98+
99+
Single::
100+
101+
{
102+
'body': 'Sample message',
103+
'originator': '+10000000000',
104+
'recipients': ['+20000000000', '+30000000000'] # list of recipients
105+
}
106+
107+
|
62108
63109
Slack
64110
=====
65111

112+
.. autoclass:: SlackNotificationProvider
113+
66114
name: ``'slack'``
67115

68116

@@ -90,13 +138,13 @@ Single::
90138
'text': 'message',
91139
}
92140

93-
94-
|
95141
|
96142
97143
Pusher Channels
98144
===============
99145

146+
.. autoclass:: PusherChannelsNotificationProvider
147+
100148
name: ``'pusher_channels'``
101149

102150
Installation
@@ -125,11 +173,12 @@ Single::
125173
}
126174

127175
|
128-
|
129176
130177
FCM (Firebase Web push)
131178
=======================
132179

180+
.. autoclass:: FCMWebNotificationProvider
181+
133182
name: ``'fcm_web'``
134183

135184
Settings
@@ -153,11 +202,12 @@ Single::
153202
}
154203

155204
|
156-
|
157205
158206
django-channels
159207
===============
160208

209+
.. autoclass:: DjangoChannelsNotificationProvider
210+
161211
name: ``'django_channels'``
162212

163213
Installation
@@ -206,6 +256,9 @@ Single::
206256
'message': {},
207257
}
208258

259+
|
260+
|
261+
209262
Writing custom Providers
210263
========================
211264

docs/usage.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,19 +91,21 @@ or::
9191

9292

9393
Notification Model
94-
-----------------
94+
------------------
9595

9696
Django notifs includes an inbuilt notification model with the following fields:
9797

9898
* **source: A ForeignKey to Django's User model (optional if it's not a User to User Notification).**
9999
* **source_display_name: A User Friendly name for the source of the notification.**
100+
100101
- **recipient: The Recipient of the notification. It's a ForeignKey to Django's User model.**
101102
- **category: Arbitrary category that can be used to group messages.**
102103
- **action: Verbal action for the notification E.g Sent, Cancelled, Bought e.t.c**
103104
- **obj: An arbitrary object associated with the notification using the `contenttypes` app (optional).**
104105
- **short_description: The body of the notification.**
105106
- **url: The url of the object associated with the notification (optional).**
106107
- **silent: If this Value is set, the notification won't be persisted to the database.**
108+
107109
* **extra_data: Arbitrary data as in a JSONField.**
108110
* **channels: Notification channels related to the notification (Tuple/List in a JSONField)**
109111

notifications/providers/__init__.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,21 @@
88
pass
99

1010
try:
11-
from .fcm import FCMWebNotificationProvider # noqa
11+
from .fcm_web import FCMWebNotificationProvider # noqa
12+
except ImportError as e:
13+
raise Exception(e)
14+
15+
try:
16+
from .django_channels import DjangoChannelsNotificationProvider # noqa
1217
except ImportError:
1318
pass
1419

1520
try:
16-
from .django_channels import DjangoChannelsProvider # noqa
21+
from .slack import SlackNotificationProvider # noqa
1722
except ImportError:
1823
pass
1924

2025
try:
21-
from .slack import SlackNotificationProvider # noqa
26+
from .django_sms import DjangoSMSNotificationProvider # noqa
2227
except ImportError:
2328
pass

notifications/providers/django_channels.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from .base import BaseNotificationProvider
77

88

9-
class DjangoChannelsProvider(BaseNotificationProvider):
9+
class DjangoChannelsNotificationProvider(BaseNotificationProvider):
1010
"""django-channels websocket provider"""
1111

1212
name = 'django_channels'

notifications/providers/django_sms.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from sms import Message, get_connection
2+
3+
from . import BaseNotificationProvider
4+
5+
6+
class DjangoSMSNotificationProvider(BaseNotificationProvider):
7+
name = 'django_sms'
8+
9+
@staticmethod
10+
def _get_sms_message(payload):
11+
message = Message()
12+
for key, value in payload.items():
13+
setattr(message, key, value)
14+
15+
return message
16+
17+
def send(self, payload):
18+
sms_message = self._get_sms_message(payload)
19+
sms_message.send()
20+
21+
def send_bulk(self, payloads):
22+
messages = (self._get_sms_message(payload) for payload in payloads)
23+
connection = get_connection()
24+
connection.send_messages(messages)

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ celery==4.1.0
22
channels==3.0.3
33
channels-redis==3.2.0
44
django-anymail==8.4
5+
django-sms[twilio,messagebird]==0.5.0
56
docutils<=0.16
67
django-jsonfield-backport==1.0.4
78
django-rq==2.4.0
89
m2r2==0.2.7
9-
pusher==3.0.0
1010
pre-commit==2.15.0
11+
pusher==3.0.0
1112
pytz==2017.2
1213
requests==2.25.1
1314
slack_sdk==3.11.2

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
'pusher_channels': ['pusher>=3.0.0'],
3131
'slack': ['slack_sdk>=3.11.2'],
3232
'anymail': ['django-anymail>=8.4'],
33+
'sms': ['django-sms>=0.5.0'],
3334
}
3435
EXCLUDE = ['notifs', 'tests', '*.tests', '*.tests.*', 'tests.*']
3536

0 commit comments

Comments
 (0)