Skip to content

Commit

Permalink
Merge pull request #59 from rdegges/develop
Browse files Browse the repository at this point in the history
Python 3 support, 0.7 version upgrade
  • Loading branch information
phalt committed Jul 22, 2014
2 parents aa56616 + 8964918 commit d25c604
Show file tree
Hide file tree
Showing 45 changed files with 1,356 additions and 1,052 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
dist

# Editor temp files:
.idea/
*.swp
*~

Expand All @@ -18,3 +19,6 @@ build
# Coverage reports:
.coverage
*htmlcov*

# Tox testing
.tox/
29 changes: 22 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
language: python

python:
- '2.7'
- '2.6'
- '2.6'
- '2.7'
- '3.3'
- '3.4'
- 'pypy'

env:
global:
- secure: TVFg6VDgzyE8VjPQVepE2JTXNbjpCBiiB3tF+rtjwzinnyPaesJ/a1P4XazzdP98Yjx1BPUxvB5kR0uDhseoUOGnZ0F5oeAr44sXZ1itGyANmFsF+cTK/zU2Ov7j+8deDypBxvgOad/LzAE0wihgNFpFopCPs0OdaBn7pfBiv40=
- secure: dAgkutD1GS4VhlRF6tmC0MoKUTakIaFcaQ8vKBQzW9Z5Q+Knjmuwx4iArc73kByg5T1KloERNcKoAGrya9rwuUYFMldLtqeYK/nTMx8ejRZ0PmGqlpmtGvJR8iN+FSdLuiVkfKvZewRHybgQt0mL2886tihf1dbVWDWTcIk/gSc=
- DJANGO="django>=1.4,<1.5"
- DJANGO="django>=1.5,<1.6"
- DJANGO="django>=1.6,<1.7"

install:
- pip install --use-mirrors ${DJANGO}
- pip install --use-mirrors -r requirements.txt
- pip install --use-mirrors -r test_requirements.txt

script:
- python manage.py test

install: pip install -r requirements.txt
script: python run_tests.py
matrix:
exclude:
- python: "3.3"
env: DJANGO="django>=1.4,<1.5"
- python: "3.4"
env: DJANGO="django>=1.4,<1.5"
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
lint:
flake8 django_twilio
flake8 django_twilio --exclude=migrations

test:
python run_tests.py
python manage.py test

coverage:
coverage run --source django_twilio run_tests.py
coverage run --source django_twilio manage.py test
coverage report -m

htmlcov:
coverage run --source django_twilio run_tests.py
coverage run --source django_twilio manage.py test
coverage html
open htmlcov/index.html

Expand Down
9 changes: 7 additions & 2 deletions django_twilio/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
"""A simple library for building twilio-powered Django webapps."""
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, absolute_import

__version__ = '0.6'
"""
A simple library for building twilio-powered Django webapps.
"""

__version__ = '0.7'
10 changes: 7 additions & 3 deletions django_twilio/admin.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
"""This module provides useful django admin hooks that allow you to manage
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, absolute_import

"""
This module provides useful django admin hooks that allow you to manage
various components through the django admin panel (if enabled).
"""

from django.contrib import admin

from django_twilio.models import Caller, Credential
from .models import Caller, Credential


class CallerAdmin(admin.ModelAdmin):
"""This class provides admin panel integration for our
:class:`django_twilio.models.Caller` model.
"""
list_display = ('__unicode__', 'blacklisted')
list_display = ('__str__', 'blacklisted')


admin.site.register(Caller, CallerAdmin)
Expand Down
15 changes: 11 additions & 4 deletions django_twilio/client.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, absolute_import

"""Twilio REST client helpers."""
from django_twilio import settings
"""
Twilio REST client helpers.
"""

from twilio.rest import TwilioRestClient

from .settings import TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN


twilio_client = TwilioRestClient(
settings.TWILIO_ACCOUNT_SID,
settings.TWILIO_AUTH_TOKEN, version='2010-04-01')
TWILIO_ACCOUNT_SID,
TWILIO_AUTH_TOKEN,
version='2010-04-01'
)
58 changes: 37 additions & 21 deletions django_twilio/decorators.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, absolute_import

"""Useful decorators."""

"""
Useful decorators.
"""

import sys
from functools import wraps

from django.conf import settings
Expand All @@ -13,13 +16,20 @@
from twilio.twiml import Verb
from twilio.util import RequestValidator

from django_twilio import settings as django_twilio_settings
from django_twilio.utils import get_blacklisted_response
from .settings import TWILIO_AUTH_TOKEN
from .utils import get_blacklisted_response


# Snippet from the `six` library to help with Python3 compatibility
if sys.version_info[0] == 3:
text_type = str
else:
text_type = unicode


def twilio_view(f):
"""This decorator provides several helpful shortcuts for writing Twilio
views.
"""
This decorator provides several helpful shortcuts for writing Twilio views.
- It ensures that only requests from Twilio are passed through. This
helps protect you from forged requests.
Expand All @@ -32,7 +42,7 @@ def twilio_view(f):
blacklisted, any requests from them will be rejected.
- It allows your view to (optionally) return TwiML to pass back to
Twilio's servers instead of building a ``HttpResponse`` object
Twilio's servers instead of building an ``HttpResponse`` object
manually.
- It allows your view to (optionally) return any ``twilio.Verb`` object
Expand All @@ -44,39 +54,42 @@ def twilio_view(f):
Usage::
from twilio.twiml import Response
from twilio import twiml
@twilio_view
def my_view(request):
r = Response()
r = twiml.Response()
r.message('Thanks for the SMS message!')
return r
"""
@csrf_exempt
@wraps(f)
def decorator(request_or_self, methods=['POST'],
def decorator(request_or_self, methods=('POST',),
blacklist=True, *args, **kwargs):

class_based_view = not(isinstance(request_or_self, HttpRequest))
class_based_view = not isinstance(request_or_self, HttpRequest)
if not class_based_view:
request = request_or_self
else:
assert len(args) >= 1
request = args[0]

# Turn off Twilio authentication when explicitly requested, or in debug mode.
# Otherwise things do not work properly. For more information see the docs.
use_forgery_protection = (
getattr(settings, 'DJANGO_TWILIO_FORGERY_PROTECTION', not settings.DEBUG))
# Turn off Twilio authentication when explicitly requested, or
# in debug mode. Otherwise things do not work properly. For
# more information, see the docs.
use_forgery_protection = getattr(
settings,
'DJANGO_TWILIO_FORGERY_PROTECTION',
not settings.DEBUG,
)
if use_forgery_protection:

if request.method not in methods:
return HttpResponseNotAllowed(request.method)

# Forgery check
try:
validator = RequestValidator(
django_twilio_settings.TWILIO_AUTH_TOKEN)
validator = RequestValidator(TWILIO_AUTH_TOKEN)
url = request.build_absolute_uri()
signature = request.META['HTTP_X_TWILIO_SIGNATURE']
except (AttributeError, KeyError):
Expand All @@ -90,16 +103,19 @@ def decorator(request_or_self, methods=['POST'],
return HttpResponseForbidden()

# Blacklist check
checkBlackList = (
getattr(settings, 'DJANGO_TWILIO_BLACKLIST_CHECK', blacklist))
if checkBlackList:
check_blacklist = getattr(
settings,
'DJANGO_TWILIO_BLACKLIST_CHECK',
blacklist
)
if check_blacklist:
blacklisted_resp = get_blacklisted_response(request)
if blacklisted_resp:
return blacklisted_resp

response = f(request_or_self, *args, **kwargs)

if isinstance(response, str):
if isinstance(response, (text_type, bytes)):
return HttpResponse(response, content_type='application/xml')
elif isinstance(response, Verb):
return HttpResponse(str(response), content_type='application/xml')
Expand Down
18 changes: 0 additions & 18 deletions django_twilio/fixtures/django_twilio.json

This file was deleted.

29 changes: 18 additions & 11 deletions django_twilio/models.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, absolute_import

from django.utils.encoding import python_2_unicode_compatible
from django.db import models
from django.conf import settings
AUTH_USER_MODEL = getattr(settings, 'AUTH_USER_MODEL', 'auth.User')


from phonenumber_field.modelfields import PhoneNumberField


AUTH_USER_MODEL = getattr(settings, 'AUTH_USER_MODEL', 'auth.User')


@python_2_unicode_compatible
class Caller(models.Model):
""" A caller is defined uniquely by their phone number.
"""
A caller is defined uniquely by their phone number.
:param bool blacklisted: Designates whether the caller can use our
services.
Expand All @@ -20,15 +25,17 @@ class Caller(models.Model):
blacklisted = models.BooleanField(default=False)
phone_number = PhoneNumberField(unique=True)

def __unicode__(self):
name = str(self.phone_number)
if self.blacklisted:
name += ' (blacklisted)'
return name
def __str__(self):
return '{phone_number}{blacklist_status}'.format(
phone_number=str(self.phone_number),
blacklist_status=' (blacklisted)' if self.blacklisted else '',
)


@python_2_unicode_compatible
class Credential(models.Model):
""" A Credential model is a set of SID / AUTH tokens for the Twilio.com API
"""
A Credential model is a set of SID / AUTH tokens for the Twilio.com API
The Credential model can be used if a project uses more than one
Twilio account, or provides Users with access to Twilio powered
Expand All @@ -41,8 +48,8 @@ class Credential(models.Model):
"""

def __unicode__(self):
return ' '.join([self.name, '-', self.account_sid])
def __str__(self):
return '{name} - {sid}'.format(name=self.name, sid=self.account_sid)

name = models.CharField(max_length=30)

Expand Down
9 changes: 6 additions & 3 deletions django_twilio/settings.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, absolute_import

"""django_twilio specific settings."""
"""
django_twilio specific settings.
"""

from .utils import discover_twilio_creds
from .utils import discover_twilio_credentials

TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN = discover_twilio_creds()
TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN = discover_twilio_credentials()
4 changes: 1 addition & 3 deletions django_twilio/south_migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# -*- coding: utf-8 -*-
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models
from django_twilio.compat import AUTH_USER_MODEL
from django_twilio.models import AUTH_USER_MODEL


class Migration(SchemaMigration):
Expand Down
34 changes: 0 additions & 34 deletions django_twilio/tests/client.py

This file was deleted.

Loading

0 comments on commit d25c604

Please sign in to comment.