From 3045aa89cfe75124e4b581788d93a0a73046d95d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Ram=C3=ADrez=20Norambuena?= Date: Wed, 13 Apr 2016 00:33:07 -0400 Subject: [PATCH] some fixes PEP8 --- app.py | 76 +++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 49 insertions(+), 27 deletions(-) diff --git a/app.py b/app.py index 40b9692..8c32d13 100644 --- a/app.py +++ b/app.py @@ -8,13 +8,15 @@ """ import os -from flask import Flask, render_template, request, url_for, make_response, jsonify +from flask import Flask, render_template, request, url_for, make_response,\ + jsonify import plivo import config from utils import get_redis_connection, get_plivo_connection, tinyid app = Flask(__name__) + @app.route("/") def index(): """ @@ -36,14 +38,15 @@ def conf_music(): """ Renders the XML to be used for hold music in the conference. This XML will be executed by Plivo when there is only 1 - participant in the conference. The URL to this XML is + participant in the conference. The URL to this XML is passed to the Conference XML under the parameter 'waitSound'. """ plivo_response = plivo.XML.Response() plivo_response.addSpeak(config.CONFERENCE_WAIT_ANNOUNCEMENT) plivo_response.addPlay(config.HOLD_MUSIC, loop=1) - response = make_response(render_template('response_template.xml', response=plivo_response)) + response = make_response(render_template('response_template.xml', + response=plivo_response)) response.headers['content-type'] = 'text/xml' return response @@ -53,7 +56,7 @@ def conf(conference_name): """ Renders the XML to start a conference based on the conference name it receives. It checks if the conference exists in our memory (to make - sure it hasn't expired). + sure it hasn't expired). This URL is passed as the Answer URL to Plivo. When the call gets answered, the call will be put into a conference. @@ -65,7 +68,8 @@ def conf(conference_name): if not room_exists: plivo_response.addHangup(reason="invalid conference") - response = make_response(render_template('response_template.xml', response=plivo_response)) + response = make_response(render_template('response_template.xml', + response=plivo_response)) response.headers['content-type'] = 'text/xml' return response @@ -74,11 +78,11 @@ def conf(conference_name): wait_sound_url = url_for('conf_music', _external=True) plivo_response.addConference(conference_name, - enterSound="beep:1", - exitSound="beep:2", - waitSound=wait_sound_url, - ) - response = make_response(render_template('response_template.xml', response=plivo_response)) + enterSound="beep:1", + exitSound="beep:2", + waitSound=wait_sound_url) + response = make_response(render_template('response_template.xml', + response=plivo_response)) response.headers['content-type'] = 'text/xml' return response @@ -86,20 +90,21 @@ def conf(conference_name): @app.route('//', methods=['GET']) def conference(conference_name): """ - Returns the HTML page for a particular conference name. The HTML page + Returns the HTML page for a particular conference name. The HTML page uses the Plivo WebSDK to register to Plivo and make calls. """ - + if conference_exists(conference_name): redis_conn = get_redis_connection() endpoint_username = redis_conn.hget(conference_name, 'username') endpoint_password = redis_conn.hget(conference_name, 'password') inbound_did = redis_conn.hget(conference_name, 'inbound_did') - conference_url = url_for('conference', _external=True, conference_name=conference_name) + conference_url = url_for('conference', _external=True, + conference_name=conference_name) data = { - 'endpoint_username': endpoint_username, + 'endpoint_username': endpoint_username, 'endpoint_password': endpoint_password, 'inbound_did': inbound_did, 'conference_name': conference_name, @@ -128,10 +133,13 @@ def conference_api(): app_id = create_plivo_application(conference_name) endpoint_username = create_plivo_endpoint(conference_name, app_id) inbound_did = attach_inbound_did(app_id) - link_conference(conference_name, endpoint_username, conference_name, inbound_did) + link_conference(conference_name, endpoint_username, conference_name, + inbound_did) - conference_url = url_for('conference', _external=True, conference_name=conference_name) - return jsonify(conference_url = conference_url, conference_name = conference_name) + conference_url = url_for('conference', _external=True, + conference_name=conference_name) + return jsonify(conference_url=conference_url, + conference_name=conference_name) @app.route('/api/v1/conference//', methods=['POST']) @@ -150,9 +158,12 @@ def conference_call_api(conference_name): if conference_exists(conference_name): to_number = request.form.get('to', None) clid = request.form.get('clid', config.PLIVO_CALLER_ID) - answer_url = url_for('conf', _external=True, conference_name=conference_name) + answer_url = url_for('conf', _external=True, + conference_name=conference_name) plivo_conn = get_plivo_connection() - status, _ = plivo_conn.make_call({'to': to_number, 'from': clid, 'answer_url': answer_url, 'answer_method': 'POST'}) + status, _ = plivo_conn.make_call({'to': to_number, 'from': clid, + 'answer_url': answer_url, + 'answer_method': 'POST'}) if status == 201: return jsonify(success=True, message='Call has been queued') return jsonify(success=False, message='Call could not be made') @@ -184,19 +195,25 @@ def create_plivo_endpoint(conference_name, app_id): """ plivo_conn = get_plivo_connection() - _, response = plivo_conn.create_endpoint({'username': conference_name, 'password': conference_name, 'alias': conference_name, 'app_id': app_id}) + _, response = plivo_conn.create_endpoint({'username': conference_name, + 'password': conference_name, + 'alias': conference_name, + 'app_id': app_id}) endpoint_username = response['username'] return endpoint_username -def link_conference(conference_name, endpoint_username, endpoint_password, inbound_did=None): +def link_conference(conference_name, endpoint_username, endpoint_password, + inbound_did=None): """ Store the conference name in memory along with endpoint username and password associated with it. """ redis_conn = get_redis_connection() - redis_conn.hmset(conference_name, {'username': endpoint_username, 'password': endpoint_password, 'inbound_did': inbound_did}) + redis_conn.hmset(conference_name, {'username': endpoint_username, + 'password': endpoint_password, + 'inbound_did': inbound_did}) if config.EXPIRE_CONFERENCE: redis_conn.expire(conference_name, 24*60*60) @@ -210,9 +227,12 @@ def create_plivo_application(conference_name): Returns the id of the application created on Plivo. """ - answer_url = url_for('conf', _external=True, conference_name=conference_name) + answer_url = url_for('conf', _external=True, + conference_name=conference_name) plivo_conn = get_plivo_connection() - _, response = plivo_conn.create_application({'app_name': conference_name, 'answer_url': answer_url, 'answer_method': 'POST'}) + _, response = plivo_conn.create_application({'app_name': conference_name, + 'answer_url': answer_url, + 'answer_method': 'POST'}) app_id = response['app_id'] return app_id @@ -228,7 +248,11 @@ def attach_inbound_did(app_id): status, response = plivo_conn.get_number_group({'country_iso': 'US'}) try: group_id = response['objects'][0]['group_id'] - status, response = plivo_conn.rent_from_number_group({'group_id': group_id, 'quantity': 1, 'app_id': app_id}) + status, response = plivo_conn.rent_from_number_group({'group_id': + group_id, + 'quantity': 1, + 'app_id': + app_id}) number_rented = response['numbers'][0]['number'] return number_rented except Exception as e: @@ -238,5 +262,3 @@ def attach_inbound_did(app_id): if __name__ == '__main__': port = int(os.environ.get('PORT', 5000)) app.run(host='0.0.0.0', port=port) - -