def show_neighborhood(neighborhood_id): """Show SF neighborhood details""" neighborhood = crud.get_neighborhood_by_id(neighborhood_id) name = neighborhood.name long_desc = neighborhood.long_desc median_home_price = neighborhood.median_home_price sq_ft_price = neighborhood.sq_ft_price median_rental = neighborhood.median_rent walk_score = neighborhood.walk_score transit_score = neighborhood.transit_score #convert prices (integers) to strings with commas in the appropriate places to improve user readability median_home_price_str = '{:,}'.format(median_home_price) sq_ft_price_str = '{:,}'.format(sq_ft_price) median_rental_str = '{:,}'.format(median_rental) neighborhood_obj = { 'neighborhood_id': neighborhood_id, 'name': name, 'long_desc': long_desc, 'median_home_price': median_home_price_str, 'sq_ft_price': sq_ft_price_str, 'median_rental': median_rental_str, 'walk_score': walk_score, 'transit_score': transit_score, } return jsonify(neighborhood_obj)
def show_restaurant_details(neighborhood_id): """Show a list of restaurants in a specific neighborhood""" neighborhood = crud.get_neighborhood_by_id(neighborhood_id) neighborhood_name = neighborhood.name payload = { "query": f"restaurants in {neighborhood_name} in San Francisco", "key": GOOG_API_KEY } res = requests.get( 'https://maps.googleapis.com/maps/api/place/textsearch/json', params=payload) search_results = res.json() data = search_results["results"] #I'm creating an empty list to limit the API search results to 5. #This limitation allows us to do a separate API call and add the website to our search results #If you don't limit it, you will get a 'key error' for the 'website' field and the page won't load limited_data = [] for i in range(5): limited_data.append(data[i]) for i in range(5): place_id = search_results["results"][i].get("place_id") website = get_restaurant_website(place_id) limited_data[i]["website"] = website return limited_data
def show_neighborhood(neighborhood_id): """Show SF neighborhood details""" neighborhood = crud.get_neighborhood_by_id(neighborhood_id) images = crud.get_image_for_neighborhood(neighborhood_id) name = neighborhood.name long_desc = neighborhood.long_desc median_home_price = neighborhood.median_home_price sq_ft_price = neighborhood.sq_ft_price median_rental = neighborhood.median_rent walk_score = neighborhood.walk_score transit_score = neighborhood.transit_score neighborhood_images = crud.create_list_of_neighborhood_images( neighborhood_id) restaurant_data = show_restaurant_details(neighborhood_id) return render_template("neighborhood.html", name=name, description=long_desc, median_home=median_home_price, sq_ft_price=sq_ft_price, median_rental=median_rental, walk_score=walk_score, transit_score=transit_score, restaurant_data=restaurant_data, images=neighborhood_images, neighborhood_id=neighborhood_id)
def post_housing(neighborhood_id): "Display form for logged in user to post available housing." neighborhood = crud.get_neighborhood_by_id(neighborhood_id) name = neighborhood.name session['neighborhood_id'] = neighborhood.neighborhood_id return render_template('post_housing.html', name=name)
def show_housing(neighborhood_id): """Show housing posted for a neighborhood.""" postings = crud.get_postings(neighborhood_id) neighborhood = crud.get_neighborhood_by_id(neighborhood_id) name = neighborhood.name return render_template('housing.html', postings=postings, name=name, neighborhood_id=neighborhood_id)
def show_restaurant_details(neighborhood_id): """Show a list of restaurants for a given neighborhood_id""" neighborhood = crud.get_neighborhood_by_id(neighborhood_id) neighborhood_name = neighborhood.name payload = { "query": f"restaurants in {neighborhood_name} in San Francisco", "key": GOOG_API_KEY } res = requests.get( 'https://maps.googleapis.com/maps/api/place/textsearch/json', params=payload) search_results = res.json() data = search_results["results"] restaurant_list = [] for i, restaurant in enumerate(data): if i < 4: rest_dict = {} name = data[i]['name'] formatted_address = data[i]['formatted_address'] rating = data[i]['rating'] place_id = data[i]['place_id'] website = get_restaurant_website(place_id) photo = get_restaurant_photo(place_id) street_address = formatted_address.rsplit(",")[0] address = street_address + "," + " SF, CA" rest_dict = { 'name': name, 'address': address, 'rating': rating, 'website': website, 'place_id': place_id, 'photo': photo } restaurant_list.append(rest_dict) #Below we are sorting the list of restaurant dictionaries in descending order by their rating sorted_restaurant_list = sorted(restaurant_list, key=lambda i: i['rating'], reverse=True) return jsonify(sorted_restaurant_list)