def get_all_gardens_for_map(): page = request.args.get('page', 1, type=int) per_page = min(request.args.get('per_page', 10000, type=int), 10000) data = Garden.to_collection_dict( Garden.query.filter(Garden.lat != None, Garden.lon != None), page, per_page, 'api.get_all_gardens_for_map') return jsonify(data)
def get_locations(): page = request.args.get('page', 1, type=int) per_page = min(request.args.get('per_page', 10000, type=int), 10000) data = Garden.to_collection_dict( Garden.query.filter(Garden.lat != None, Garden.lon != None).order_by(Garden.address), page, per_page, 'api.get_locations') all_gardens = Garden.query.all() coords = [] locations = [] for garden in all_gardens: garden_plants_objs_arr = [] if garden.lat and garden.lon: address = garden.address lat = garden.lat lon = garden.lon coord = (lat, lon) garden_plants = garden.plants.all() for plant in garden_plants: plant_obj = Plant.to_dict(plant) garden_plants_objs_arr.append(plant_obj) garden_obj = Garden.to_dict(garden) #handle the case where two markers would be placed in hte exact #same spot, where only one would be visible and usable in the #google maps once rendered if coord in coords: #find the index of the first garden for a given coord index = coords.index(coord) #use index of given coord to select the corresponding #location_for_markers in its list and extend the plants list locations[index]['gardens'].append(garden_obj) locations[index]['plants'].extend(garden_plants_objs_arr) elif coord not in coords: coords.append(coord) location = { 'address': address, 'latitude': lat, 'longitude': lon, 'gardens': [garden_obj], 'plants': garden_plants_objs_arr } locations.append(location) return jsonify(locations)