Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Commit 639488f

Browse files
author
Juan Pablo Santos
committed
Initial middleware implementation
0 parents  commit 639488f

17 files changed

+436
-0
lines changed

.coveragerc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Coverage configuration file
2+
3+
[run]
4+
source = .
5+
6+
omit =
7+
twilio_sample_project/settings/*
8+
twilio_sample_project/wsgi.py
9+
*/migrations/*
10+
*/tests.py

.env_example

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Environment variables for server-notifications-django
2+
3+
# Django settings to use in this environment
4+
export DJANGO_SETTINGS_MODULE=twilio_sample_project.settings.local
5+
6+
# Twilio API credentials
7+
export TWILIO_ACCOUNT_SID=ACXXXXXXXXXXXXXXXXX
8+
export TWILIO_AUTH_TOKEN=YYYYYYYYYYYYYYYYYY
9+
10+
# Twilio sending number
11+
export TWILIO_NUMBER=+1555555505

.gitignore

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Environment variables
2+
.env
3+
4+
# virtualenv
5+
venv
6+
7+
# Staticfiles
8+
twilio_sample_project/staticfiles
9+
10+
# Byte-compiled / optimized / DLL files
11+
__pycache__/
12+
*.py[cod]
13+
14+
# C extensions
15+
*.so
16+
17+
# Distribution / packaging
18+
.Python
19+
env/
20+
build/
21+
develop-eggs/
22+
dist/
23+
downloads/
24+
eggs/
25+
.eggs/
26+
lib/
27+
lib64/
28+
parts/
29+
sdist/
30+
var/
31+
*.egg-info/
32+
.installed.cfg
33+
*.egg
34+
35+
# PyInstaller
36+
# Usually these files are written by a python script from a template
37+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
38+
*.manifest
39+
*.spec
40+
41+
# Installer logs
42+
pip-log.txt
43+
pip-delete-this-directory.txt
44+
45+
# Unit test / coverage reports
46+
htmlcov/
47+
.tox/
48+
.coverage
49+
.coverage.*
50+
.cache
51+
nosetests.xml
52+
coverage.xml
53+
*,cover
54+
55+
# Translations
56+
*.mo
57+
*.pot
58+
59+
# Django stuff:
60+
*.log
61+
62+
# Sphinx documentation
63+
docs/_build/
64+
65+
# PyBuilder
66+
target/

.travis.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
language: python
2+
python:
3+
- '2.7'
4+
- '3.3'
5+
- '3.4'
6+
install:
7+
- pip install -r requirements.txt
8+
- pip install coveralls
9+
script: coverage run manage.py test --settings=twilio_sample_project.settings.test
10+
env:
11+
- TWILIO_ACCOUNT_SID=ACXXXXXXXXXXXXXXXXX TWILIO_AUTH_TOKEN=YYYYYYYYYYYYYYYYYY
12+
after_success:
13+
- coveralls

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Twilio Inc.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+

README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Server Notifications
2+
3+
[![Build Status](https://travis-ci.org/TwilioDevEd/server-notifications-django.svg?branch=master)](https://travis-ci.org/TwilioDevEd/server-notifications-django)
4+
[![Coverage Status](https://coveralls.io/repos/TwilioDevEd/server-notifications-django/badge.svg?branch=master&service=github)](https://coveralls.io/github/TwilioDevEd/server-notifications-django?branch=master)
5+
6+
*Include 2-3 sentences here describing this sample app and what it does.*
7+
8+
## Quickstart
9+
10+
This project is built using the [Django](https://www.djangoproject.com/) web framework. It runs on Python 2.7+ and Python 3.4+.
11+
12+
To run the app locally, first clone this repository and `cd` into its directory. Then:
13+
14+
1. Create a new virtual environment:
15+
- If using vanilla [virtualenv](https://virtualenv.pypa.io/en/latest/):
16+
17+
```
18+
virtualenv venv
19+
source venv/bin/activate
20+
```
21+
22+
- If using [virtualenvwrapper](https://virtualenvwrapper.readthedocs.org/en/latest/):
23+
24+
```
25+
mkvirtualenv server-notifications-django
26+
```
27+
28+
1. Install the requirements:
29+
30+
```
31+
pip install -r requirements.txt
32+
```
33+
34+
1. Start a local PostgreSQL database and create a database called `server_notifications_django`:
35+
- If on a Mac, we recommend [Postgres.app](http://postgresapp.com/). After install, open psql and run `CREATE DATABASE server_notifications_django;`
36+
- If Postgres is already installed locally, you can just run `createdb server_notifications_django` from a terminal
37+
38+
1. Run the migrations with:
39+
40+
```
41+
python manage.py migrate
42+
```
43+
44+
1. Optionally create a superuser so you can access the Django admin:
45+
46+
```
47+
python manage.py createsuperuser
48+
```
49+
50+
1. Copy the `.env_example` file to `.env`, and edit it to include your Twilio API credentials (found at https://www.twilio.com/user/account/voice)
51+
1. Run `source .env` to apply the environment variables (or even better, use [autoenv](https://github.com/kennethreitz/autoenv))
52+
1. Start the development server
53+
54+
```
55+
python manage.py runserver
56+
```
57+
58+
## Run the tests
59+
60+
You can run the tests locally through [coverage](http://coverage.readthedocs.org/):
61+
62+
```
63+
$ coverage run manage.py test --settings=twilio_sample_project.settings.test
64+
```
65+
66+
You can then view the results with `coverage report` or build an HTML report with `coverage html`.

manage.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env python
2+
import os
3+
import sys
4+
5+
if __name__ == "__main__":
6+
os.environ.setdefault(
7+
"DJANGO_SETTINGS_MODULE",
8+
"twilio_sample_project.settings.local")
9+
10+
from django.core.management import execute_from_command_line
11+
12+
execute_from_command_line(sys.argv)

requirements.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Requirements for server-notifications-django
2+
3+
# Django (1.8 is a long-term support release)
4+
Django==1.8
5+
twilio
6+
7+
# Production dependencies
8+
gunicorn
9+
10+
# Test dependencies
11+
coverage
12+
mock
13+
six

twilio_notifications/middleware.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from twilio.rest import TwilioRestClient
2+
from django.core.exceptions import MiddlewareNotUsed
3+
import os
4+
import logging
5+
6+
logger = logging.getLogger(__name__)
7+
8+
MESSAGE = """[This is a test] ALERT!
9+
It appears the server is having issues.
10+
Exception: %s.
11+
Go to: http://newrelic.com for more details."""
12+
13+
NOT_CONFIGURED_MESSAGE = """Cannot initialize Twilio notification
14+
middleware. Required enviroment variables TWILIO_ACCOUNT_SID, or
15+
TWILIO_AUTH_TOKEN or TWILIO_NUMBER missing"""
16+
17+
18+
class TwilioNotificationsMiddleware(object):
19+
20+
def __init__(self):
21+
(twilio_number, twilio_client) = self._twilio_config()
22+
23+
self.twilio_number = twilio_number
24+
self.twilio_client = twilio_client
25+
self.administrators = self._administrators()
26+
27+
def _twilio_config(self):
28+
twilio_account_sid = os.environ.get('TWILIO_ACCOUNT_SID')
29+
twilio_auth_token = os.environ.get('TWILIO_AUTH_TOKEN')
30+
twilio_number = os.environ.get('TWILIO_NUMBER')
31+
32+
if not all([twilio_account_sid,
33+
twilio_auth_token,
34+
self.twilio_number]):
35+
36+
logger.error(NOT_CONFIGURED_MESSAGE)
37+
raise MiddlewareNotUsed
38+
39+
return (twilio_number,
40+
TwilioRestClient(twilio_account_sid, twilio_auth_token))
41+
42+
def _send_message(self, body, to):
43+
self.twilio_client.messages.create(body=body, to=to,
44+
from_=self.twilio_number)
45+
46+
def _administrators(self):
47+
# Read administrators
48+
pass
49+
50+
def process_exception(self, request, exception):
51+
exception_message = str(exception)
52+
message_to_send = MESSAGE % exception_message
53+
54+
for admin in self.administrators:
55+
self._send_message(message_to_send, admin.phone_number)
56+
57+
return None

twilio_sample_project/__init__.py

Whitespace-only changes.

twilio_sample_project/settings/__init__.py

Whitespace-only changes.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
"""
2+
Common Django settings for the project.
3+
4+
See the local, test, and production settings modules for the values used
5+
in each environment.
6+
7+
For more information on this file, see
8+
https://docs.djangoproject.com/en/1.8/topics/settings/
9+
10+
For the full list of settings and their values, see
11+
https://docs.djangoproject.com/en/1.8/ref/settings/
12+
"""
13+
import os
14+
15+
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
16+
17+
# SECURITY WARNING: don't run with debug turned on in production!
18+
DEBUG = False
19+
20+
# SECURITY WARNING: keep the secret key used in production secret!
21+
SECRET_KEY = 'not-so-secret'
22+
23+
ALLOWED_HOSTS = []
24+
25+
# Application definition
26+
27+
DJANGO_APPS = (
28+
'django.contrib.admin',
29+
'django.contrib.auth',
30+
'django.contrib.contenttypes',
31+
'django.contrib.sessions',
32+
'django.contrib.messages',
33+
'django.contrib.staticfiles',
34+
'django.contrib.humanize'
35+
)
36+
37+
THIRD_PARTY_APPS = ()
38+
39+
LOCAL_APPS = ()
40+
41+
INSTALLED_APPS = DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS
42+
43+
MIDDLEWARE_CLASSES = (
44+
'django.contrib.sessions.middleware.SessionMiddleware',
45+
'django.middleware.common.CommonMiddleware',
46+
'django.middleware.csrf.CsrfViewMiddleware',
47+
'django.contrib.auth.middleware.AuthenticationMiddleware',
48+
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
49+
'django.contrib.messages.middleware.MessageMiddleware',
50+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
51+
'django.middleware.security.SecurityMiddleware',
52+
)
53+
54+
ROOT_URLCONF = 'twilio_sample_project.urls'
55+
56+
TEMPLATES = [
57+
{
58+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
59+
'DIRS': ['templates/'],
60+
'APP_DIRS': True,
61+
'OPTIONS': {
62+
'context_processors': [
63+
'django.template.context_processors.debug',
64+
'django.template.context_processors.request',
65+
'django.contrib.auth.context_processors.auth',
66+
'django.contrib.messages.context_processors.messages',
67+
],
68+
},
69+
},
70+
]
71+
72+
WSGI_APPLICATION = 'twilio_sample_project.wsgi.application'
73+
74+
75+
# Database
76+
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
77+
78+
DATABASES = {}
79+
80+
# Internationalization
81+
# https://docs.djangoproject.com/en/1.8/topics/i18n/
82+
83+
LANGUAGE_CODE = 'en-us'
84+
85+
TIME_ZONE = 'UTC'
86+
87+
USE_I18N = True
88+
89+
USE_L10N = True
90+
91+
USE_TZ = True
92+
93+
94+
# Static files (CSS, JavaScript, Images)
95+
# https://docs.djangoproject.com/en/1.8/howto/static-files/
96+
97+
STATIC_ROOT = BASE_DIR + '/staticfiles'
98+
99+
STATIC_URL = '/static/'
100+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'''
2+
Local settings
3+
4+
- Run in Debug mode
5+
'''
6+
7+
from .common import * # noqa
8+
9+
# Use DEBUG for local development
10+
DEBUG = True

0 commit comments

Comments
 (0)