Skip to content

Commit

Permalink
Adding Country, State, Place/City -> Zipcode support
Browse files Browse the repository at this point in the history
Removing all references to Pymongo's json_util
  • Loading branch information
Trea Hauet committed Sep 13, 2012
1 parent f5f40db commit f1c7d2b
Showing 1 changed file with 48 additions and 5 deletions.
53 changes: 48 additions & 5 deletions wsgi.py
@@ -1,5 +1,5 @@
from bottle import route, default_app, static_file, view, response, run
from pymongo import Connection, json_util
from pymongo import Connection
from pymongo.database import Database
from bson.son import SON
import json
Expand Down Expand Up @@ -48,6 +48,14 @@ def find_nearby(country,post):

return result;

@route('/:country/:state/:place', method='GET')
def find_postcode(country, state, place):
(isFound, results) = place_query(country, state, place)
configure(response)
if (isFound == False) :
response.status=404

return results;

def configure(response):
'''
Expand Down Expand Up @@ -79,7 +87,7 @@ def nearby_zip(country,code):
response = { 'near latitude': lat, # Record the query lat
'near longitude': lon, # Record the query lat
'nearby' : nearby[1:] }
content = json.dumps( response , default=json_util.default )
content = json.dumps( response )
return (True, content)

content = json.dumps({})
Expand All @@ -98,7 +106,7 @@ def nearby_coordinates( lat, lon):
results = { 'near latitude': lat, # Record the query lat.
'near longitude': lon, # Record the query lon.
'nearby': nearby[:10] }
content = json.dumps( results, default=json_util.default )
content = json.dumps( results )
return ( True, content)

else :
Expand Down Expand Up @@ -162,11 +170,46 @@ def standard_query(country,code):
content =json.dumps({ 'country':country_name, # Return unique fields
'country abbreviation':country_abbv,
'post code':post_code,
'places':result}, # Return a list of possible places
default=json_util.default) # Using pymongo json settings
'places':result}) # Using pymongo json settings

return (isFound,content) # Return True and JSON results

def place_query(country,state,place):
'''
Place_query returns JSON data if there are matching postcodes for a given
country abbreviation, state abbreviation, and place/city
'''
result = list(db['global'].find({'country abbreviation':country.upper(),
'state abbreviation':state.upper(),
'place name': {'$regex': place, '$options': '-i'}
}));
if len(result) < 1 :
content = json.dumps({}); #Empty JSON string
isFound = False #We didn't find anything
return (isFound, content)
else:
country_name = result[0]['country']
country_abbv = result[0]['country abbreviation']
state = result[0]['state']
state_abbv = result[0]['state abbreviation']
place = result[0]['place name']
isFound = True
for places in result: #Remove from each result
del places['_id'] #Mongo ID
del places['state'] #State
del places['state abbreviation'] #State abbreviation
del places['country'] #Country
del places['country abbreviation'] #Country abbreviation

content = json.dumps({
'country': country_name,
'country abbreviation': country_abbv,
'state': state,
'state abbreviation': state_abbv,
'place name': place,
'places': result})

return (isFound, content) #Return True and JSON results

# PRESENT GLOBAL
ZIP = 'zip'
Expand Down

0 comments on commit f1c7d2b

Please sign in to comment.