Skip to content

Commit

Permalink
Don't let a bogus campaign crash the server
Browse files Browse the repository at this point in the history
  • Loading branch information
beaugunderson committed Feb 11, 2014
1 parent ed3868c commit c82fa43
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
31 changes: 30 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pystache
import twilio.twiml

from flask import Flask, request, render_template, url_for
from flask import abort, Flask, request, render_template, url_for
from flask_cache import Cache
from flask_jsonpify import jsonify
from raven.contrib.flask import Sentry
Expand Down Expand Up @@ -64,6 +64,9 @@ def parse_params(r):
# lookup campaign by ID
campaign = data.get_campaign(params['campaignId'])

if not campaign:
return None, None

# add repIds to the parameter set, if spec. by the campaign
if campaign.get('repIds', None):
if isinstance(campaign['repIds'], basestring):
Expand Down Expand Up @@ -131,6 +134,9 @@ def make_calls(params, campaign):
def _make_calls():
params, campaign = parse_params(request)

if not params or not campaign:
abort(404)

return make_calls(params, campaign)


Expand All @@ -148,6 +154,9 @@ def call_user():
# parse the info needed to make the call
params, campaign = parse_params(request)

if not params or not campaign:
abort(404)

# initiate the call
try:
call = app.config['TW_CLIENT'].calls.create(
Expand Down Expand Up @@ -179,6 +188,9 @@ def connection():
"""
params, campaign = parse_params(request)

if not params or not campaign:
abort(404)

if params['repIds']:
resp = twilio.twiml.Response()

Expand Down Expand Up @@ -207,6 +219,9 @@ def incoming_call():
"""
params, campaign = parse_params(request)

if not params or not campaign:
abort(404)

return intro_zip_gather(params, campaign)


Expand All @@ -217,6 +232,10 @@ def zip_parse():
Required Params: campaignId, Digits
"""
params, campaign = parse_params(request)

if not params or not campaign:
abort(404)

zipcode = request.values.get('Digits', '')
rep_ids = data.locate_member_ids(zipcode, campaign)

Expand All @@ -238,6 +257,10 @@ def zip_parse():
@app.route('/make_single_call', methods=call_methods)
def make_single_call():
params, campaign = parse_params(request)

if not params or not campaign:
abort(404)

i = int(request.values.get('call_index', 0))
params['call_index'] = i
member = [l for l in data.legislators
Expand Down Expand Up @@ -268,6 +291,9 @@ def make_single_call():
def call_complete():
params, campaign = parse_params(request)

if not params or not campaign:
abort(404)

log_call(params, campaign, request)

resp = twilio.twiml.Response()
Expand All @@ -293,6 +319,9 @@ def call_complete_status():
# asynch callback from twilio on call complete
params, _ = parse_params(request)

if not params:
abort(404)

return jsonify({
'phoneNumber': request.values.get('To', ''),
'callStatus': request.values.get('CallStatus', 'unknown'),
Expand Down
4 changes: 3 additions & 1 deletion political_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ def __init__(self):
self.districts = districts

def get_campaign(self, campaign_id):
return dict(self.campaigns['default'], **self.campaigns[campaign_id])
if campaign_id in self.campaigns:
return dict(self.campaigns['default'],
**self.campaigns[campaign_id])

def get_senators(self, districts):
states = [d['state'] for d in districts]
Expand Down

0 comments on commit c82fa43

Please sign in to comment.