Skip to content

Commit

Permalink
Merge pull request #4 from inforian/develop
Browse files Browse the repository at this point in the history
Providers configuration ias Function Arguments
  • Loading branch information
inforian committed Jun 7, 2017
2 parents b601676 + b1179db commit 7af5eb0
Show file tree
Hide file tree
Showing 14 changed files with 270 additions and 138 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Expand Up @@ -91,4 +91,7 @@ ENV/

# example project
example/db.sqlite3
example/local_settings.py
example/local_settings.py

# local dev env settings
local.py
60 changes: 58 additions & 2 deletions docs/source/email.rst
Expand Up @@ -7,7 +7,14 @@ Email Providers
notification_type = 'email'
General Settings you need to configure in Django Project when using any email providers are mentioned below :
- General Settings you need to configure in Django Project when using any email providers are mentioned below :

- Some settings can be passed as function arguments as well as in `Django settings`. The main AIM is to provide all
possible flexibility to user to use Any `Provider` with any configuration.

- If we Add settings in `Django settings` then in entire project those settings will be used But if you want every
notification use different provider configuration then that is also possible here.


> General Settings
------------------
Expand Down Expand Up @@ -52,10 +59,15 @@ Default email address to use for various outgoing emails.
provider = 'gmail'
- You can use Gmail as your **SMTP** provider an send Emails from Your own Gmail account.
- You can use Gmail as your **SMTP** provider an send Emails from Your own `Gmail` account.

- For this you need below settings to configure in your Django Project.

- Sample settings for Gmail Provider are as follows:

As Django settings :
++++++++++++++++++++

.. code-block:: python
EMAIL_USE_TLS = True
Expand All @@ -66,6 +78,41 @@ Default email address to use for various outgoing emails.
EMAIL_PORT = 587
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
As Function Arguments:
++++++++++++++++++++++

- `EMAIL_HOST_USER` as `username`.
- `EMAIL_HOST_PASSWORD` as `password`.

**Example Usage** :

.. code-block:: python
from notifyAll.services import notifier
def notify():
"""
"""
context = {
'subject': 'subject'
'body': 'body'
'html_message': '<h1>html message</h1>'
}
data = {
'source': 'admin@example.com',
'destination': 'me@example.com',
'notification_type': 'email',
'provider': 'gmail',
'context': context,
}
notification = notifier.Notifier(**data)
return notification.notify(username='myuser@gmail.com', password='mypassword')
> SendGrid
----------

Expand All @@ -85,6 +132,9 @@ SENDGRID_API_KEY :

- Sample settings for SendGrid Provider are as follows:

As Django settings :
++++++++++++++++++++

.. code-block:: python
EMAIL_USE_TLS = True
Expand All @@ -95,3 +145,9 @@ SENDGRID_API_KEY :
EMAIL_PORT = 587
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
As Function Arguments:
++++++++++++++++++++++

- `SENDGRID_API_KEY` as `sendgrid_api_key`.

- Usage is same as shown in `Gmail` provider example
11 changes: 2 additions & 9 deletions docs/source/installation.rst
Expand Up @@ -74,7 +74,6 @@ context
For email provider only. If you want to send message including **HTML** then you need to send your notification body
in the above key.


**Example Usage** :

.. code-block:: python
Expand All @@ -99,17 +98,11 @@ context
'context': context,
}
notification = notifier.Notifier(
source=data.get('source'),
destination=data.get('destination'),
notification_type=data.get('notification_type'),
provider=data.get('provider'),
context=data.get('context')
)
notification = notifier.Notifier(**data)
return notification.notify()
For more information about usage visit our `Example`_ project.

.. _Provider: https://django-allauth.readthedocs.io/en/latest/Providers.html
.. _Provider: https://django-allauth.readthedocs.io/en/latest/providers.html
.. _Example: https://django-allauth.readthedocs.io/en/latest/example.html
4 changes: 3 additions & 1 deletion docs/source/providers.rst
Expand Up @@ -11,6 +11,8 @@ I'll try to explain what settings you need in order to user Any below providers.
email
sms

**Note :** Providers can be managed in two ways using `django settings` or as `function arguments`.
See respective Providers docs for more info

Add providers
=============
Expand All @@ -32,4 +34,4 @@ If you want to contribute to this repo and wish to add more providers just follo
so that your provider can be enabled for outer world.


.. _Provider: https://django-allauth.readthedocs.io/en/latest/Providers.html
.. _Provider: https://django-allauth.readthedocs.io/en/latest/providers.html
52 changes: 52 additions & 0 deletions docs/source/sms.rst
Expand Up @@ -7,6 +7,15 @@ SMS Providers
notification_type = 'sms'
- General Settings you need to configure in Django Project when using any email providers are mentioned below :

- Some settings can be passed as function arguments as well as in `Django settings`. The main AIM is to provide all
possible flexibility to user to use Any `Provider` with any configuration.

- If we Add settings in `Django settings` then in entire project those settings will be used But if you want every
notification use different provider configuration then that is also possible here.


> Plivo
-------

Expand All @@ -26,13 +35,46 @@ SMS Providers
- When you register at `Plivo` it will give you two keys which you need to configure in your Django Project.

As Django settings :
~~~~~~~~~~~~~~~~~~~~

PLIVO_AUTH_ID
+++++++++++++

PLIVO_AUTH_TOKEN
++++++++++++++++

As Function Arguments:
~~~~~~~~~~~~~~~~~~~~~~

- `PLIVO_AUTH_ID` as `auth_id`
- `PLIVO_AUTH_TOKEN` as `auth_token`


**Example Usage** :

.. code-block:: python
from notifyAll.services import notifier
def notify():
"""
"""
data = {
'source': '<source>',
'destination': '<destination>',
'notification_type': 'sms',
'provider': 'plivo',
'context': {
'body': 'test message'
},
}
notification = notifier.Notifier(**data)
return notification.notify(auth_id='<plivo_auth_id>', auth_token='<plivo_auth_token>')
> Twilio
--------
Expand All @@ -53,13 +95,23 @@ PLIVO_AUTH_TOKEN
- When you register at `Twilio` it will give you two keys which you need to configure in your Django Project.

As Django settings :
~~~~~~~~~~~~~~~~~~~~

TWILIO_ACCOUNT_SID
++++++++++++++++++

TWILIO_AUTH_TOKEN
+++++++++++++++++

As Function Arguments:
~~~~~~~~~~~~~~~~~~~~~~

- `TWILIO_ACCOUNT_SID` as `account_sid`
- `TWILIO_AUTH_TOKEN` as `auth_token`


- Usage is same as shown in `Plivo` provider example

.. _plivo: https://github.com/plivo/plivo-python
.. _twilio: https://github.com/twilio/twilio-python
7 changes: 4 additions & 3 deletions notifyAll/config/email_provider_settings.py
Expand Up @@ -13,6 +13,7 @@
EMAIL_HOST = 'smtp.gmail.com'

# ------------ SendGrid -----------
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
SENDGRID_API_KEY = None
# EMAIL_HOST = 'smtp.sendgrid.net'
# EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
# SENDGRID_API_KEY = None

3 changes: 2 additions & 1 deletion notifyAll/config/settings.py
Expand Up @@ -149,11 +149,12 @@ def get_env_setting(key):
# Notification related settings
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True

EMAIL_PORT = 587

# import local settings
try:
from notifyAll.config.email_provider_settings import *
from notifyAll.config.sms_provider_settings import *
from notifyAll.config.local import *
except:
pass
82 changes: 58 additions & 24 deletions notifyAll/providers/base.py
Expand Up @@ -22,14 +22,10 @@
# Django
from django.conf import settings
from django.core.mail import EmailMultiAlternatives
from django.core.exceptions import ImproperlyConfigured

# local
from notifyAll.services import service_config

# own app
from notifyAll.providers import provider_config


class EmailProvider(object):
"""Base class for All email providers
Expand All @@ -39,16 +35,33 @@ class EmailProvider(object):
name = None
notify_type = service_config.EMAIL

def __init__(self):
def __init__(self, source, destination, notification_type, context,
fail_silently=False):
"""
we will provide to ways to configure clients :
- One, you can configure email settings from Django-settings file if not,
- Then Second, you can send settings as function arguments too,
- Priority wil be given to function arguments
:param source: who wants to send SMS
:param destination: to whom SMS will be sent.
:param notification_type: notification type
:param context: data you want to send in SMS
:param fail_silently: catch exception
"""
# validate necessary settings are configured by user for Email Notifications
email_backend = getattr(settings, 'EMAIL_BACKEND', None)
email_host = getattr(settings, 'EMAIL_HOST', None)

if email_backend is None or email_host is None:
raise ImproperlyConfigured('to send emails you need to configure email Backend and Host.')
# email related stuff
self.source = settings.DEFAULT_FROM_EMAIL if source is None else source
self.destination = destination
self.notification_type = notification_type
self.subject = context.get('subject', '')
self.body = context.get('body', '')
self.cc = context.get('cc')
self.bcc = context.get('bcc')
self.attachment = context.get('attachment')
self.html_message = context.get('html_message')

self.fail_silently = fail_silently

def _validate_notification_type_with_provider(self, notification_type):
"""validate notification_type w.r.t notify_type of Provider, means for which you have called this provider
Expand All @@ -63,31 +76,41 @@ def _convert_var_type_to_list(self, value):
"""
return value if type(value) is list else [value]

def _prepare_email_message(self, from_, to, context):
def _make_connection(self):
"""make connection with backend
:return: connection with email provider
"""
return None

def _prepare_email_message(self):
"""Prepare email message with necessary information.
"""

message = {
'subject': context.get('subject', ''),
'body': context.get('body', ''),
'from_email': from_ if from_ else provider_config.DEFAULT_FROM_EMAIL,
'to': self._convert_var_type_to_list(to),
'cc': self._convert_var_type_to_list(context.get('cc')),
'bcc': self._convert_var_type_to_list(context.get('bcc')),
'subject': self.subject,
'body': self.body,
'from_email': self.source,
'to': self._convert_var_type_to_list(self.destination),
'cc': self._convert_var_type_to_list(self.cc),
'bcc': self._convert_var_type_to_list(self.bcc),
'connection': self._make_connection()
}

self.email_message = EmailMultiAlternatives(**message)

if context.get('attachment'):
self.email_message.attach_file(context.get('attachment'))
if self.attachment:
self.email_message.attach_file(self.attachment)

if context.get('html_message'):
if self.html_message:
self.email_message.attach_alternative(
context.get('html_message'), 'text/html')
self.html_message, 'text/html')

def notify(self):
"""
"""
pass
self._prepare_email_message()
return self.email_message.send()


class SMSProvider(object):
Expand All @@ -98,10 +121,21 @@ class SMSProvider(object):
name = None
notify_type = service_config.SMS

def __init__(self):
def __init__(self, source, destination, notification_type, context):
"""
:param source: who wants to send SMS
:param destination: to whom SMS will be sent.
:param notification_type: notification type
:param context: data you want to send in SMS
"""
# validate notification_type w.r.t Provider notify_type
self._validate_notification_type_with_provider(notification_type)

self.source = source
self.destination = destination
self.notification_type = notification_type
self.context = context

def _validate_notification_type_with_provider(self, notification_type):
"""validate notification_type w.r.t notify_type of Provider, means for which you have called this provider
Expand Down

0 comments on commit 7af5eb0

Please sign in to comment.