File: //home/arjun/projects/buyercall/buyercall/blueprints/api/doc/endpoints/phonenumbers.py
import logging
from flask import jsonify, request
from flask_restx import Resource
from sqlalchemy import and_
from buyercall.lib.util_rest import (rest_partnership_account, rest_partnership, requires_auth,
rest_is_partnership_account, rest_is_partnership)
from buyercall.blueprints.api.doc import serializers
from buyercall.blueprints.api.restx import api
from buyercall.blueprints.phonenumbers.models import Phone
from buyercall.extensions import db
log = logging.getLogger(__name__)
ns = api.namespace('phonenumbers', description='Operations related to phone numbers.')
@ns.route('/detailed/')
@api.doc(responses={200: 'OK',
400: 'Error performing operation.',
401: 'Unauthorized request.'})
class ApiPhoneNumbersCollection(Resource):
@requires_auth
def get(self):
"""
Retrieves the phonenumbers of a partnership account.
Use this method to view the phonenumbers of a partnership account.
"""
if rest_is_partnership_account and rest_partnership_account:
phonenumbers_query = db.session.query(Phone).filter(
and_(Phone.partnership_account_id == rest_partnership_account.id,
Phone.is_deactivated == False)
).all()
numbers = list()
if phonenumbers_query:
for phone in phonenumbers_query:
phonenumber = {
'id': phone.id,
'phonenumber': phone.phonenumber,
'local': phone.local,
'tollfree': phone.tollfree,
'type': phone.type,
'active': phone.active,
'friendly_name': phone.friendly_name,
'source': phone.source,
'channel': phone.channel,
'twilio_number_sid': phone.twilio_number_sid,
'routing_config': phone.routing_config,
'notifications': phone.notifications
}
numbers.append(phonenumber)
return jsonify(
phonenumbers=numbers
)
else:
return jsonify(
phonenumbers=numbers
)
elif rest_is_partnership and rest_partnership:
partnership_accounts = rest_partnership.get_partnership_accounts()
pa_list = list()
if partnership_accounts:
for pa in partnership_accounts:
phonenumbers_query = (db.session.query(Phone)
.filter(Phone.partnership_account_id == pa.id)).all()
numbers = list()
if phonenumbers_query:
for phone in phonenumbers_query:
phonenumber = {
'id': phone.id,
'phonenumber': phone.phonenumber,
'local': phone.local,
'tollfree': phone.tollfree,
'type': phone.type,
'active': phone.active,
'friendly_name': phone.friendly_name,
'source': phone.source,
'channel': phone.channel,
'twilio_number_sid': phone.twilio_number_sid,
'routing_config': phone.routing_config,
'notifications': phone.notifications
}
numbers.append(phonenumber)
add_pa = {
'name': pa.name,
'phonenumbers': numbers
}
pa_list.append(add_pa)
return jsonify(
accounts=pa_list
)
else:
return jsonify(
accounts=pa_list
)
else:
api.abort(401)
@api.response(204, 'Phone number successfully added.')
@api.expect(serializers.phonenumber_row, validate=True)
@requires_auth
def post(self):
"""
Adds a partnership account phone number.
Use this method to add a phone number to a partnership account.
"""
if rest_is_partnership_account and rest_partnership_account:
result = request.json
if result:
phone_number = db.session.query(Phone)\
.filter(and_(Phone.phonenumber == result['phonenumber'],
Phone.partnership_account_id == rest_partnership_account.id))\
.first()
if phone_number:
api.abort(code=400, message="Phone number already exists.")
else:
is_local = False
is_tollfree = False
local = result['is_local']
tollfree = result['is_tollfree']
if local and (local is 'True' or local is 'true'):
is_local = True
if tollfree and (tollfree is 'True' or tollfree is 'true'):
is_tollfree = True
params = {
'phonenumber': result['phonenumber'],
'partnership_account_id': rest_partnership_account.id,
'local': is_local,
'type': 'priority',
'tollfree': is_tollfree,
'friendly_name': result['friendly_name'],
'source': result['source'],
'twilio_number_sid': result['twilio_number_sid'],
'channel': result['channel'],
'provider': ''
}
final_result = Phone.create(params)
if final_result:
return final_result, 204
else:
api.abort(code=400, message="Error creating phone number.")
else:
api.abort(400)
else:
api.abort(401)
@ns.route('/<int:pnid>')
@api.doc(responses={200: 'OK',
400: 'Error performing operation.',
401: 'Unauthorized request.'},
params={'pnid': 'The phonenumber ID.'})
class ApiPhoneNumber(Resource):
@api.response(204, 'Phone number successfully deactivated.')
@requires_auth
def delete(self, pnid):
"""
Deactivate a partnership account phone number.
Use this method to deactivate a partnership account phone number.
"""
if rest_is_partnership_account and rest_partnership_account:
final_result = Phone.deactivate(pnid, rest_partnership_account.id)
if final_result:
return final_result, 204
else:
api.abort(code=400)
else:
api.abort(401)
@api.response(204, 'Phone number successfully updated.')
@api.expect(serializers.phonenumber_row, validate=True)
@requires_auth
def put(self, pnid):
"""
Update a partnership account phone number.
Use this method to update a partnership account's phone numbers.
"""
if rest_is_partnership_account and rest_partnership_account:
result = request.json
if result:
is_local = False
is_tollfree = False
local = result['is_local']
tollfree = result['is_tollfree']
if local and (local is 'True' or local is 'true'):
is_local = True
if tollfree and (tollfree is 'True' or tollfree is 'true'):
is_tollfree = True
final_result = Phone.update(
pnid,
rest_partnership_account.id,
result['phonenumber'],
result['friendly_name'],
result['twilio_number_sid'],
result['source'],
result['channel'],
is_local,
is_tollfree
)
if final_result:
return final_result, 204
else:
api.abort(400)
else:
api.abort(400)
else:
api.abort(401)
@ns.route('/search/<string:pn>')
@api.doc(responses={200: 'OK',
400: 'Error performing operation.',
401: 'Unauthorized request.'},
params={'pn': 'The phonenumber that will be searched for.'})
class PhoneNumbersSearch(Resource):
@requires_auth
def get(self, pn):
"""
Performs a search and retrieves the phonenumbers of a partnership account based on the input.
Use this method to search for phonenumbers of a partnership account and to return their details.
* Specify the phone number in the request URL path.
"""
if rest_is_partnership_account and rest_partnership_account:
phonenumbers_query = (db.session.query(Phone).filter(
and_(Phone.phonenumber.like('%{}%'.format(pn))),
Phone.partnership_account_id == rest_partnership_account.id,
Phone.active == True)).all()
numbers = list()
if phonenumbers_query is not None:
for phone in phonenumbers_query:
phonenumber = {
'id': phone.id,
'phonenumber': phone.phonenumber,
'local': phone.local,
'tollfree': phone.tollfree,
'type': phone.type,
'active': phone.active,
'friendly_name': phone.friendly_name,
'source': phone.source,
'channel': phone.channel,
'twilio_number_sid': phone.twilio_number_sid,
'routing_config': phone.routing_config,
'notifications': phone.notifications
}
numbers.append(phonenumber)
return jsonify(
phonenumbers=numbers
)
else:
return jsonify(
phonenumbers=numbers
)
elif rest_is_partnership and rest_partnership is not None:
partnership_accounts = rest_partnership.get_partnership_accounts()
pa_list = list()
if partnership_accounts is not None:
for pa in partnership_accounts:
phonenumbers_query = (db.session.query(Phone)
.filter(and_(Phone.phonenumber.ilike(pn)),
Phone.partnership_account_id == pa.id)).all()
if phonenumbers_query is not None:
numbers = list()
for phone in phonenumbers_query:
phonenumber = {
'id': phone.id,
'phonenumber': phone.phonenumber,
'local': phone.local,
'tollfree': phone.tollfree,
'type': phone.type,
'active': phone.active,
'friendly_name': phone.friendly_name,
'source': phone.source,
'channel': phone.channel,
'twilio_number_sid': phone.twilio_number_sid,
'routing_config': phone.routing_config,
'notifications': phone.notifications
}
numbers.append(phonenumber)
add_pa = {
'name': pa.name,
'phonenumbers': numbers
}
pa_list.append(add_pa)
return jsonify(
accounts=pa_list
)
else:
return jsonify(
accounts=pa_list
)
else:
api.abort(401)