Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Commit

Permalink
add additional checks for missing objects
Browse files Browse the repository at this point in the history
  • Loading branch information
ttavenner committed Oct 14, 2016
1 parent c9f95c7 commit 31fe369
Showing 1 changed file with 32 additions and 34 deletions.
66 changes: 32 additions & 34 deletions API.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,19 @@
from math import radians, cos, sin, atan2, sqrt
from collections import OrderedDict
from datetime import datetime
from flask import Flask, Response, url_for, request, current_app, render_template
from flask import g, Flask, Response, url_for, request, current_app, render_template
from functools import wraps
from livesdataexporter import LivesDataExporter
from piwik import Tracker
from threading import Thread

app = Flask(__name__)
app.debug = False

try:
db = mongolab.connect()
except ValueError:
print "Could not connect to database"

piwik = Tracker()

@app.before_request
def before_request():
if piwik:
piwik.track(request)
def get_db():
if not hasattr(g, 'db'):
g.db = mongolab.connect()


def support_jsonp(f):
Expand Down Expand Up @@ -116,7 +109,7 @@ def api_vendors():
if request.args.get('minDist'):
query['geo']['$nearSphere']['$minDistance'] = int(request.args.get('minDist'))

data = db.va.find(query, output).limit(limit)
data = g.db.va.find(query, output).limit(limit)

if data.count() == 0:
resp = json.dumps({'status': '204', 'message': 'no results returned'})
Expand All @@ -134,14 +127,14 @@ def api_vendors():
vendor_list[item['slug']]['category'] = item['category']
if 'score' in item:
vendor_list[item['slug']]['score'] = item['score']
if 'geo' in item:
if 'geo' in item and 'coordinates' in item['geo']:
vendor_list[item['slug']]['coordinates'] = {'latitude': item['geo']['coordinates'][1],
'longitude': item['geo']['coordinates'][0]}
if request.args.get('lat') is not None:
vendor_list[item['slug']]['dist'] = round(great_circle(float(request.args.get('lng')),
float(request.args.get('lat')),
item['geo']['coordinates'][0],
item['geo']['coordinates'][1]), 2)
if request.args.get('lat') is not None:
vendor_list[item['slug']]['dist'] = round(great_circle(float(request.args.get('lng')),
float(request.args.get('lat')),
item['geo']['coordinates'][0],
item['geo']['coordinates'][1]), 2)

if request.args.get('pretty') == 'true':
resp = json.dumps(vendor_list, indent=4)
Expand All @@ -153,16 +146,16 @@ def api_vendors():
@app.route('/vendor/<vendorid>')
@support_jsonp
def api_vendor(vendorid):
data = db.va.find({'slug': vendorid}, {'name': 1,
'address': 1,
'city': 1,
'locality': 1,
'type': 1,
'category': 1,
'slug': 1,
'score': 1,
'inspections.0': {'$slice': 1},
'geo.coordinates': 1}).sort('inspection.date')
data = g.db.va.find({'slug': vendorid}, {'name': 1,
'address': 1,
'city': 1,
'locality': 1,
'type': 1,
'category': 1,
'slug': 1,
'score': 1,
'inspections.0': {'$slice': 1},
'geo.coordinates': 1}).sort('inspection.date')
if data.count() == 1:
item = data[0]

Expand Down Expand Up @@ -248,7 +241,7 @@ def api_inspections():
score_below = 101 if request.args.get('score_below') is None else int(request.args.get('score_below'))
score_above = 0 if request.args.get('score_above') is None else int(request.args.get('score_above'))

data = db.va.find(query, output).limit(limit)
data = g.db.va.find(query, output).limit(limit)

if data.count() == 0:
resp = json.dumps({'Status': 204, 'Message': 'No results returned'})
Expand Down Expand Up @@ -276,10 +269,15 @@ def api_inspections():

if 'violations' in inspection:
for violation in inspection['violations']:
if request.args.get('violation_text') is not None and request.args.get('violation_text') in violation['observation']:
inspections[index]['violations'].append(violation)
elif request.args.get('violation_code') is not None and request.args.get('violation_code') in violation['code']:
inspections[index]['violations'].append(violation)

if request.args.get('violation_text') is not None:
if 'observation' in violation:
if violation['observation'] is not None and request.args.get('violation_text') in violation['observation']:
inspections[index]['violations'].append(violation)
elif request.args.get('violation_code') is not None:
if 'code' in violation:
if violation['code'] is not None and request.args.get('violation_code') in violation['code']:
inspections[index]['violations'].append(violation)
elif request.args.get('violation_text') is None and request.args.get('violation_code') is None:
inspections[index]['violations'].append(violation)

Expand All @@ -303,7 +301,7 @@ def api_inspections():

if 'score' in item:
vendor_list[item['slug']]['score'] = item['score']
if 'geo' in item:
if 'geo' in item and 'coordinates' in item['geo']:
vendor_list[item['slug']]['coordinates'] = {'latitude': item['geo']['coordinates'][1],
'longitude': item['geo']['coordinates'][0]}
if request.args.get('pretty') == 'true':
Expand Down

0 comments on commit 31fe369

Please sign in to comment.