Skip to content

Instantly share code, notes, and snippets.

@webus
Created May 21, 2015 05:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webus/ee927b101d6cc064a672 to your computer and use it in GitHub Desktop.
Save webus/ee927b101d6cc064a672 to your computer and use it in GitHub Desktop.
Interacting with MongoDB through Flask
# -*- coding: utf-8 -*-
from flask import request, session, redirect, url_for, render_template
from flask import Response
from flask import jsonify, make_response
from bson.json_util import dumps
from bson.objectid import ObjectId
from mobile import app, mongo, mongofs
from mobile.controllers.api.common.tools import *
from mobile.utils.decorators import *
API_VERSION = "/api/1.0"
idex = DataExtractor()
@app.route(API_VERSION + '/map/<mid>',methods=['GET'])
@try_except
def api_v1_map_get(mid):
"""Get the coordinates of the location of id"""
user_map = mongo.db.user_object_map.find_one({'_id':ObjectId(mid)})
if user_map == None:
return jsonify({'error':'not found'})
json_docs = idex.extract(user_map) # process the data for proper display id
return Response(dumps(json_docs), mimetype='application/json')
# http://mobile.example.com/api/1.0/map/search/<lat>/<lng>/<limit>
# http://mobile.example.com/api/1.0/map/search/36/138/50
@app.route(API_VERSION + '/map/search/<lat>/<lng>/<limit>/<lang>',methods=['GET'])
@try_except
def api_v1_map_search(lat,lng,limit,lang):
"""Search nearest places
lat - latitude
lng - longitude"""
user_map = mongo.db.user_object_map.find(
{'map': {'$near': {'lat': lat,'lng': lng}}}).limit(int(limit))
user_object_ids = []
for m in user_map:
fuser_object = mongo.db.user_object.find_one({'_id':m['user_object_id']})
user_object_ids.append(str(fuser_object['_id']))
user_object_ids = list(set(user_object_ids))
user_object = []
for uo in user_object_ids:
user_object.append(mongo.db.user_object.find_one({'_id':ObjectId(uo)}))
user_object = [set_add_info(x,lang) for x in user_object] # tidy map,address
idex.relang(user_object,lang) # remove unnecessary languages
json_docs = [idex.extract(uo) for uo in user_object] # process the data for proper display id
return Response(dumps(json_docs), mimetype='application/json')
# http://127.0.0.1:5000/api/1.0/map/search-radius/40.4349504/49.8676232/1000/100
@app.route(API_VERSION + '/map/search-radius/<lat>/<lng>/<radius>/<limit>/<lang>',methods=['GET'])
def api_v1_map_search_radius(lat,lng,radius,limit,lang):
"""Places within a radius"""
# Important If you use longitude and latitude, specify longitude first.
# http://docs.mongodb.org/manual/reference/operator/query/center/
user_map = mongo.db.user_object_map.find(
{'map':{'$geoWithin':{'$center':[[float(lng),float(lat)],float(radius)]}}}).limit(int(limit))
user_object_ids = []
for m in user_map:
fuser_object = mongo.db.user_object.find_one({'_id':m['user_object_id']})
if fuser_object != None:
user_object_ids.append(str(fuser_object['_id']))
user_object_ids = list(set(user_object_ids))
user_object = []
for uo in user_object_ids:
user_object.append(mongo.db.user_object.find_one({'_id':ObjectId(uo)}))
user_object = [set_add_info(x,lang) for x in user_object] # tidy map,address
idex.relang(user_object,lang) # remove unnecessary languages
json_docs = [idex.extract(uo) for uo in user_object] # process the data for proper display id
return Response(dumps(json_docs), mimetype='application/json')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment