def serve_data(): item_type = request.args.get('item_type', None) rargs = request.args.copy() kwargs = {} # Copy request args into a copy for k in rargs.keys(): if k != 'item_type': kwargs[k] = rargs[k] if '_id' in k: kwargs[k] = ObjectId(rargs[k]) if item_type == 'cart': objs = carts.find(**kwargs) else: objs = reviews.find(**kwargs) results = [o._obj for o in objs] # Remove incompatible types for r in results: serialize(r) # Return results as an array data = {'results': results} return json.dumps(data)
def search(item_type, offset, number, keywords, location): objs = [] # Change objs depending on item type if item_type == 'cart': # Get results for each keyword for word in keywords: if word == '': continue # Check each text field for tfield in carts.text_fields(): kwd = re.compile(r'(?: |^)' + word + '(?: |$)', re.IGNORECASE) search_object = {tfield: kwd} if len(objs) == 0: if location != '': objs += get_carts_near(location, offset, number, search_object) else: objs += carts.find(offset, number, **search_object) search_object = {'tags': {'$in': [word]}} objs += carts.find(offset, number, **search_object) # If there are no keywords, use location instead if len(objs) == 0 and keywords[0] == '' and len(keywords) == 1: objs += get_carts_near(location, offset, number) elif item_type == 'review': for word in keywords: for tfield in reviews.text_fields(): kwd = re.compile(r'(?: |^)' + word + '(?: |$)', re.IGNORECASE) search_object = {tfield: kwd} objs += reviews.find(**search_object) return unique_objs(objs)