Skip to content

Commit

Permalink
Merge pull request #18 from TwilioDevEd/masked-numbers-next-gen
Browse files Browse the repository at this point in the history
[Restored] [Masked Numbers] Bump Twilio Library to Version 6.x
  • Loading branch information
joliveros authored Apr 3, 2017
2 parents 628fd5a + 73da1c6 commit 7a5b565
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ docs/_build/
target/

.env
.env.test
.env.test
/venv
57 changes: 30 additions & 27 deletions airtng_flask/models/reservation.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
from airtng_flask.models import app_db, auth_token, account_sid, phone_number, application_sid
from flask import render_template
from twilio.rest import TwilioRestClient
from twilio.rest import Client

db = app_db()
DB = app_db()


class Reservation(db.Model):
class Reservation(DB.Model):
__tablename__ = "reservations"

id = db.Column(db.Integer, primary_key=True)
message = db.Column(db.String, nullable=False)
status = db.Column(db.Enum('pending', 'confirmed', 'rejected', name='reservation_status_enum'), default='pending')
anonymous_phone_number = db.Column(db.String, nullable=True)
guest_id = db.Column(db.Integer, db.ForeignKey('users.id'))
vacation_property_id = db.Column(db.Integer, db.ForeignKey('vacation_properties.id'))
guest = db.relationship("User", back_populates="reservations")
vacation_property = db.relationship("VacationProperty", back_populates="reservations")
id = DB.Column(DB.Integer, primary_key=True)
message = DB.Column(DB.String, nullable=False)
status = DB.Column(DB.Enum('pending', 'confirmed', 'rejected', name='reservation_status_enum'),
default='pending')
anonymous_phone_number = DB.Column(DB.String, nullable=True)
guest_id = DB.Column(DB.Integer, DB.ForeignKey('users.id'))
vacation_property_id = DB.Column(DB.Integer, DB.ForeignKey('vacation_properties.id'))
guest = DB.relationship("User", back_populates="reservations")
vacation_property = DB.relationship("VacationProperty", back_populates="reservations")

def __init__(self, message, vacation_property, guest):
self.message = message
Expand Down Expand Up @@ -46,21 +47,20 @@ def notify_guest(self):
status=self.status))

def buy_number(self, area_code):
numbers = self._get_twilio_client().phone_numbers.search(country="US",
type="local",
area_code=area_code,
sms_enabled=True,
voice_enabled=True)
numbers = self._get_twilio_client().available_phone_numbers("US") \
.local \
.list(area_code=area_code,
sms_enabled=True,
voice_enabled=True)

if numbers:
number = self._purchase_number(numbers[0])
self.anonymous_phone_number = number
return number
else:
numbers = self._get_twilio_client().phone_numbers.search(country="US",
type="local",
sms_enabled=True,
voice_enabled=True)
numbers = self._get_twilio_client().available_phone_numbers("US") \
.local \
.list(sms_enabled=True, voice_enabled=True)

if numbers:
number = self._purchase_number(numbers[0])
Expand All @@ -70,14 +70,17 @@ def buy_number(self, area_code):
return None

def _purchase_number(self, number):
return number.purchase(sms_application_sid=application_sid(),
voice_application_sid=application_sid()).phone_number
return self._get_twilio_client().incoming_phone_numbers \
.create(sms_application_sid=application_sid(),
voice_application_sid=application_sid(),
phone_number=number) \
.phone_number

def _get_twilio_client(self):
return TwilioRestClient(account_sid(), auth_token())
return Client(account_sid(), auth_token())

def _send_message(self, to, message):
self._get_twilio_client().messages.create(
to=to,
from_=phone_number(),
body=message)
self._get_twilio_client().messages \
.create(to,
from_=phone_number(),
body=message)
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pytz==2015.7
six==1.10.0
SQLAlchemy==1.0.11
traceback2==1.4.0
twilio==5.2.0
twilio==6.0.0rc12
unittest2==1.1.0
Werkzeug==0.11.3
wheel==0.24.0
Expand Down

0 comments on commit 7a5b565

Please sign in to comment.