Permalink
Browse files

Merge pull request #1 from thesteve0/master

I hope this gets to grant to let him know a pull is needed
  • Loading branch information...
2 parents d3c14c3 + 09726db commit 6a969b22c2679ac3cc6f5dec3edbcec548c5e453 @danmcp danmcp committed Sep 21, 2012
Showing with 81 additions and 3 deletions.
  1. +1 −0 README.md
  2. +80 −3 wsgi/myflaskapp.py
View
1 README.md
@@ -15,6 +15,7 @@ Create a python-2.6 application and add a MongoDB cartridge to the app
Add this upstream flask repo
+
cd pythonws
git remote add upstream -m master git://github.com/openshift/openshift-mongo-flask-example.git
git pull -s recursive -X theirs upstream master
View
83 wsgi/myflaskapp.py
@@ -1,9 +1,86 @@
+import os
from flask import Flask
+from flask import request
+import pymongo
+import json
+from bson import json_util
+from bson import objectid
+import re
+
app = Flask(__name__)
+#add this so that flask doesn't swallow error messages
+app.config['PROPAGATE_EXCEPTIONS'] = True
+
+#a base urls that returns all the parks in the collection (of course in the future we would implement paging)
+@app.route("/ws/parks")
+def parks():
+ #setup the connection
+ conn = pymongo.Connection(os.environ['OPENSHIFT_NOSQL_DB_URL'])
+ db = conn.parks
+
+ #query the DB for all the parkpoints
+ result = db.parkpoints.find()
+
+ #Now turn the results into valid JSON
+ return str(json.dumps({'results':list(result)},default=json_util.default))
+
+
+#return a specific park given it's mongo _id
+@app.route("/ws/parks/park/<parkId>")
+def onePark(parkId):
+ #setup the connection
+ conn = pymongo.Connection(os.environ['OPENSHIFT_NOSQL_DB_URL'])
+ db = conn.parks
+
+ #query based on the objectid
+ result = db.parkpoints.find({'_id': objectid.ObjectId(parkId)})
+
+ #turn the results into valid JSON
+ return str(json.dumps({'results' : list(result)},default=json_util.default))
+
+
+#find parks near a lat and long passed in as query parameters (near?lat=45.5&lon=-82)
+@app.route("/ws/parks/near")
+def near():
+ #setup the connection
+ conn = pymongo.Connection(os.environ['OPENSHIFT_NOSQL_DB_URL'])
+ db = conn.parks
+
+ #get the request parameters
+ lat = float(request.args.get('lat'))
+ lon = float(request.args.get('lon'))
+
+ #use the request parameters in the query
+ result = db.parkpoints.find({"pos" : { "$near" : [lon,lat]}})
+
+ #turn the results into valid JSON
+ return str(json.dumps({'results' : list(result)},default=json_util.default))
+
+
+#find parks with a certain name (use regex) near a lat long pair such as above
+@app.route("/ws/parks/name/near/<name>")
+def nameNear(name):
+ #setup the connection
+ conn = pymongo.Connection(os.environ['OPENSHIFT_NOSQL_DB_URL'])
+ db = conn.parks
+
+ #get the request parameters
+ lat = float(request.args.get('lat'))
+ lon = float(request.args.get('lon'))
+
+ #compile the regex we want to search for and make it case insensitive
+ myregex = re.compile(name, re.I)
+
+ #use the request parameters in the query along with the regex
+ result = db.parkpoints.find({"Name" : myregex, "pos" : { "$near" : [lon,lat]}})
+
+ #turn the results into valid JSON
+ return str(json.dumps({'results' : list(result)},default=json_util.default))
+
-@app.route("/")
-def hello():
- return "Hello World!"
+@app.route("/test")
+def test():
+ return "<strong>It actually worked</strong>"
if __name__ == "__main__":
app.run()

0 comments on commit 6a969b2

Please sign in to comment.