def query_handle(json_obj): client = pymongo.MongoClient('localhost',27017) database = client['device_database'] collection = database['devices'] json_obj.pop('type') print json_obj li = [] print json_obj print type(json_obj) mongo_query = sample.get_mongo_query(json_obj['query']) if mongo_query[0] == True: if len(mongo_query[1]) > 1: peeps = collection.find(mongo_query[1][0],mongo_query[1][1]) else: peeps = collection.find(mongo_query[1][0]) #cursor = collection.find(eval(json_obj['query'])) #print cursor.count() for elem in peeps: _id = elem.pop('_id') li.append(ast.literal_eval(json.dumps(elem))) # print li[0] return '{"result":"' + str(li) + '"}' else: print "wrong query"
def query_handle(json_obj,logfile): client = pymongo.MongoClient('localhost',27017) database = client['device_database'] collection = database['devices'] json_obj.pop('type') print json_obj li = [] print json_obj print type(json_obj) mongo_query = sample.get_mongo_query(json_obj['query']) if mongo_query[0] == True: if len(mongo_query[1]) > 1: peeps = collection.find(mongo_query[1][0],mongo_query[1][1]) else: peeps = collection.find(mongo_query[1][0]) #cursor = collection.find(eval(json_obj['query'])) #print cursor.count() for elem in peeps: _id = elem.pop('_id') li.append(ast.literal_eval(json.dumps(elem))) # print li[0] logfile.write("query handling was successful at\t" + str(datetime.now()) + "\n") logfile.close() return '{"result":"' + str(li) + '","valid_query":"True"}' else: logfile.write("query handling was unsuccessful at\t" + str(datetime.now()) + "\n") logfile.close() return '{"valid_query":"False"}'