forked from TwilioDevEd/server-notifications-django
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upgrade to Django 2+ and remove Whitenoise
- Loading branch information
Showing
11 changed files
with
160 additions
and
88 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ omit = | |
*/migrations/* | ||
*/tests.py | ||
*/tests/*.py | ||
venv/* |
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
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 |
---|---|---|
@@ -1,14 +1,14 @@ | ||
# Requirements for server-notifications-django | ||
|
||
# Django (1.8 is a long-term support release) | ||
Django==1.8 | ||
twilio==6.9.0 | ||
whitenoise | ||
Django==2.2.5 | ||
twilio==6.29.4 | ||
python-dotenv==0.10.3 | ||
|
||
# Production dependencies | ||
gunicorn | ||
|
||
# Test dependencies | ||
black | ||
coverage | ||
flake8 | ||
mock | ||
six |
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 |
---|---|---|
@@ -1,67 +1,89 @@ | ||
from __future__ import unicode_literals | ||
import json | ||
import logging | ||
import os | ||
|
||
from django.conf import settings | ||
from django.core.exceptions import ImproperlyConfigured | ||
from django.http import HttpResponse | ||
from dotenv import load_dotenv | ||
from twilio.rest import Client | ||
from django.core.exceptions import MiddlewareNotUsed | ||
import os | ||
import logging | ||
import json | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
dotenv_path = settings.PROJECT_PATH / '.env' | ||
logger.debug(f'Reading .env file at: {dotenv_path}') | ||
load_dotenv(dotenv_path=dotenv_path) | ||
|
||
|
||
MESSAGE = """[This is a test] ALERT! It appears the server is having issues. | ||
Exception: %s. Go to: http://newrelic.com for more details.""" | ||
Exception: {0}""" | ||
|
||
NOT_CONFIGURED_MESSAGE = """Cannot initialize Twilio notification | ||
middleware. Required enviroment variables TWILIO_ACCOUNT_SID, or | ||
TWILIO_AUTH_TOKEN or TWILIO_NUMBER missing""" | ||
NOT_CONFIGURED_MESSAGE = ( | ||
"Required enviroment variables " | ||
"TWILIO_ACCOUNT_SID or TWILIO_AUTH_TOKEN or TWILIO_NUMBER missing." | ||
) | ||
|
||
|
||
def load_admins_file(): | ||
with open('config/administrators.json') as adminsFile: | ||
admins = json.load(adminsFile) | ||
return admins | ||
admins_json_path = settings.PROJECT_PATH / 'config' / 'administrators.json' | ||
logger.debug(f'Loading administrators info from: {admins_json_path}') | ||
return json.loads(admins_json_path.read_text()) | ||
|
||
|
||
def load_twilio_config(): | ||
twilio_account_sid = os.environ.get('TWILIO_ACCOUNT_SID') | ||
twilio_auth_token = os.environ.get('TWILIO_AUTH_TOKEN') | ||
twilio_number = os.environ.get('TWILIO_NUMBER') | ||
logger.debug('Loading Twilio configuration') | ||
|
||
twilio_account_sid = os.getenv('TWILIO_ACCOUNT_SID') | ||
twilio_auth_token = os.getenv('TWILIO_AUTH_TOKEN') | ||
twilio_number = os.getenv('TWILIO_NUMBER') | ||
|
||
if not all([twilio_account_sid, twilio_auth_token, twilio_number]): | ||
logger.error(NOT_CONFIGURED_MESSAGE) | ||
raise MiddlewareNotUsed | ||
raise ImproperlyConfigured(NOT_CONFIGURED_MESSAGE) | ||
|
||
return (twilio_number, twilio_account_sid, twilio_auth_token) | ||
|
||
|
||
class MessageClient(object): | ||
class MessageClient: | ||
def __init__(self): | ||
(twilio_number, twilio_account_sid, | ||
twilio_auth_token) = load_twilio_config() | ||
logger.debug('Initializing messaging client') | ||
|
||
( | ||
twilio_number, | ||
twilio_account_sid, | ||
twilio_auth_token, | ||
) = load_twilio_config() | ||
|
||
self.twilio_number = twilio_number | ||
self.twilio_client = Client(twilio_account_sid, | ||
twilio_auth_token) | ||
self.twilio_client = Client(twilio_account_sid, twilio_auth_token) | ||
|
||
logger.debug('Twilio client initialized') | ||
|
||
def send_message(self, body, to): | ||
self.twilio_client.messages.create(body=body, to=to, | ||
from_=self.twilio_number, | ||
# media_url=['https://demo.twilio.com/owl.png']) | ||
) | ||
self.twilio_client.messages.create( | ||
body=body, to=to, from_=self.twilio_number | ||
) | ||
|
||
|
||
class TwilioNotificationsMiddleware(object): | ||
def __init__(self): | ||
class TwilioNotificationsMiddleware: | ||
def __init__(self, get_response): | ||
logger.debug('Initializing Twilio notifications middleware') | ||
|
||
self.administrators = load_admins_file() | ||
self.client = MessageClient() | ||
self.get_response = get_response | ||
|
||
logger.debug('Twilio notifications middleware initialized') | ||
|
||
def __call__(self, request): | ||
return self.get_response(request) | ||
|
||
def process_exception(self, request, exception): | ||
exception_message = str(exception) | ||
message_to_send = MESSAGE % exception_message | ||
message_to_send = MESSAGE.format(exception) | ||
|
||
for admin in self.administrators: | ||
self.client.send_message(message_to_send, admin['phone_number']) | ||
|
||
logger.info('Administrators notified') | ||
|
||
return None | ||
logger.info('Administrators notified!') | ||
return HttpResponse( | ||
"An error occured. Don't panic! Administrators are notified." | ||
) |
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
Oops, something went wrong.