def generate_results_page(study_id,final_rankings): print "generate_results_page" studyObj = Database.getStudy(study_id) results = { "study_id": study_id, "question": studyObj['study_question'], "ranking": [] } # TODO: Optimize this so that it doesn't run one query per # location just to fetch the place_id. study_places = set(map(str,studyObj['places_id'])) places = defaultdict(list) for rankedLocationID in final_rankings: locationObj = Database.getLocation(rankedLocationID) # Get the place_ids shared (set intersection) between the study and this location. # I'm assuming there'll only be one in common, since a location should appear # in a study only once. # place_id = list(study_places&set(locationObj['place_id']))[0] place_id = list(study_places&set(locationObj['place_id']))[0] score = final_rankings[rankedLocationID] places[place_id].append({ "location_id": rankedLocationID, "score": score, "loc": locationObj['loc'], "heading": locationObj['heading'], "pitch": locationObj['pitch'], "study_rank": len(places[place_id])+1 }) for place_id in places.keys(): places[place_id].sort(key=lambda x:x['score']) placeObj = Database.getPlace(place_id) placeRanking = { "name": placeObj.get('name'), "name_slug": slugify(placeObj.get('name')), "place_id": place_id, "rankings": places[place_id] } results['ranking'].append(placeRanking) Database.results.update({ "study_id": study_id, }, { "study_id": study_id, "results": results },True)
def generate_results_page(study_id, final_rankings): print "generate_results_page" studyObj = Database.getStudy(study_id) results = { "study_id": study_id, "question": studyObj['study_question'], "ranking": [] } # TODO: Optimize this so that it doesn't run one query per # location just to fetch the place_id. study_places = set(map(str, studyObj['places_id'])) places = defaultdict(list) for rankedLocationID in final_rankings: locationObj = Database.getLocation(rankedLocationID) # Get the place_ids shared (set intersection) between the study and this location. # I'm assuming there'll only be one in common, since a location should appear # in a study only once. # place_id = list(study_places&set(locationObj['place_id']))[0] place_id = list(study_places & set(locationObj['place_id']))[0] score = final_rankings[rankedLocationID] places[place_id].append({ "location_id": rankedLocationID, "score": score, "loc": locationObj['loc'], "heading": locationObj['heading'], "pitch": locationObj['pitch'], "study_rank": len(places[place_id]) + 1 }) for place_id in places.keys(): places[place_id].sort(key=lambda x: x['score']) placeObj = Database.getPlace(place_id) placeRanking = { "name": placeObj.get('name'), "name_slug": slugify(placeObj.get('name')), "place_id": place_id, "rankings": places[place_id] } results['ranking'].append(placeRanking) Database.results.update({ "study_id": study_id, }, { "study_id": study_id, "results": results }, True)