Skip to content

Commit

Permalink
Version 2.0
Browse files Browse the repository at this point in the history
* Code updated for new version of twilio-python plugin
* Messaging Response updated for new version
* Return Protocol from HTTP_X_FORWARDED_PROTO Meta tag
  • Loading branch information
mastizada authored Apr 12, 2017
1 parent b164fa6 commit 3b67a02
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ sudo: false
language: python
python:
- "2.7"
- "3.5"
- "3.6"

install: pip install tox-travis

Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changes
-------

# 2.0

* Switched to new api, dependencies upgraded.
* Solved problem with defining protocol (thanks to chrisgrande).
* Django 1.11 added to tox for tests.

# 1.3.0

* Added url for inbound messages (receive sms messages)
Expand Down
17 changes: 6 additions & 11 deletions dj_twilio_sms/decorators.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
# -*- coding: utf-8 -*-
# Based on https://github.com/rdegges/django-twilio under Public Domain
# Added logging and error reporting
# Updated to support Python 2/3

from __future__ import unicode_literals
from functools import wraps
import logging
Expand All @@ -13,8 +9,8 @@
from django.utils.encoding import force_text
from django.views.decorators.csrf import csrf_exempt

from twilio.twiml import Verb
from twilio.util import RequestValidator
from twilio.twiml import TwiML
from twilio.request_validator import RequestValidator

logger = logging.getLogger("dj-twilio-sms.decorators")

Expand All @@ -34,7 +30,7 @@ def twilio_view(f):
Twilio's servers instead of building a ``HttpResponse`` object
manually.
- It allows your view to (optionally) return any ``twilio.Verb`` object
- It allows your view to (optionally) return any ``twilio.TwiML`` object
instead of building a ``HttpResponse`` object manually.
Usage::
Expand Down Expand Up @@ -67,9 +63,8 @@ def decorator(request, *args, **kwargs):
# Ensure the original requested url is tested for validation
# Prevents breakage when processed behind a proxy server
if "HTTP_X_FORWARDED_SERVER" in request.META:
protocol = "https" if request.META["HTTP_X_TWILIO_SSL"] == "Enabled" else "http"
url = "{0}://{1}{2}".format(
protocol, request.META["HTTP_X_FORWARDED_SERVER"], request.META["REQUEST_URI"]
request.META["HTTP_X_FORWARDED_PROTO"], request.META["HTTP_X_FORWARDED_SERVER"], request.META["REQUEST_URI"]
)
signature = request.META["HTTP_X_TWILIO_SIGNATURE"]
except (AttributeError, KeyError) as e:
Expand All @@ -88,14 +83,14 @@ def decorator(request, *args, **kwargs):
# Run the wrapped view, and capture the data returned.
response = f(request, *args, **kwargs)

# If the view returns a string (or a ``twilio.Verb`` object), we'll
# If the view returns a string (or a ``twilio.TwiML`` object), we'll
# assume it is XML TwilML data and pass it back with the appropriate
# mimetype. We won't check the XML data because that would be too time
# consuming for every request. Instead, we'll let the errors pass
# through to be dealt with by the developer.
if isinstance(response, six.text_type):
return HttpResponse(response, mimetype="application/xml")
elif isinstance(response, Verb):
elif isinstance(response, TwiML):
return HttpResponse(force_text(response), mimetype="application/xml")
else:
return response
Expand Down
6 changes: 3 additions & 3 deletions dj_twilio_sms/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.conf import settings
from django.core.urlresolvers import reverse
from django.utils.encoding import force_text
from twilio.rest import TwilioRestClient
from twilio.rest import Client
from decimal import Decimal
from pytz import timezone

Expand Down Expand Up @@ -44,7 +44,7 @@ def send_sms(request, to_number, body, callback_urlname="sms_status_callback"):
"""
Create :class:`OutgoingSMS` object and send SMS using Twilio.
"""
client = TwilioRestClient(settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN)
client = Client(settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN)
from_number = settings.TWILIO_PHONE_NUMBER

message = OutgoingSMS.objects.create(
Expand All @@ -61,7 +61,7 @@ def send_sms(request, to_number, body, callback_urlname="sms_status_callback"):
to_number, status_callback, body)

if not getattr(settings, "TWILIO_DRY_MODE", False):
sent = client.sms.messages.create(
sent = client.messages.create(
to=to_number,
from_=from_number,
body=body,
Expand Down
9 changes: 5 additions & 4 deletions dj_twilio_sms/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from django.views.decorators.csrf import csrf_exempt
from django.utils.decorators import method_decorator
from rest_framework import status
from twilio import twiml
from twilio.twiml.messaging_response import MessagingResponse, Body

from .decorators import twilio_view
from .models import OutgoingSMS
Expand All @@ -21,7 +21,6 @@ class TwilioView(View):
"""
Base view for Twilio callbacks
"""

response_text = None

@method_decorator(csrf_exempt)
Expand All @@ -42,11 +41,11 @@ def get_response_text(self):
return self.response_text

def get_response(self, message, **kwargs):
response = twiml.Response()
response = MessagingResponse()

response_text = self.get_response_text()
if response_text:
response.sms(response_text)
response.message(Body(response_text))

response = HttpResponse(str(response), content_type='application/xml')
return response
Expand All @@ -58,6 +57,7 @@ class IncomingSMSView(TwilioView):
Override to add custom logic and configure url in the Twilio admin panel.
"""
object = None

def handle_request(self, data):
logger.debug("Received SMS message: %r", data)
Expand Down Expand Up @@ -86,6 +86,7 @@ class SMSStatusCallbackView(SingleObjectMixin, TwilioView):
Configure callback url in the Twilio admin panel.
"""
object = None

model = OutgoingSMS

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Django
djangorestframework
twilio
twilio==6.0.0
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from setuptools import setup, find_packages
import os

version = "1.3.0"
version = "2.0"

with open(os.path.join(os.path.dirname(__file__), 'README.rst')) as f:
README = f.read()
Expand Down
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tox]
envlist =
py27-django{18},
py35-django{18,19,110},
py36-django{18,19,110,111},

[testenv]
passenv =
Expand All @@ -18,3 +18,4 @@ deps =
django18: Django>=1.8,<1.9
django19: Django>=1.9,<1.10
django110: Django>=1.10,<1.11
django111: Django>=1.11,<1.12

0 comments on commit 3b67a02

Please sign in to comment.