def display_apartments(): """ Query database for posts within the user-specified distance. Returns JSON with nested apartment objects. """ search_results = Posting.get_apartments(session['price'], session['bedrooms'], session['origin_latitude'], session['origin_longitude'], session['max_distance']) if not search_results: return 'Your search returned no results!' session['num_results'] = len(search_results) avg_rent = Posting.calculate_avg_rent(search_results) session['avg_rent'] = avg_rent apartments = {'origin_info': {"origin_lat": session['origin_latitude'], "origin_lon": session['origin_longitude']}, 'listings': {}, 'avg_rent': avg_rent, } for apt in search_results: apartments['listings'][apt.post_id] = { "title": apt.title, "date_posted": apt.date_posted, "url": apt.url, "img_url": apt.img_url, "price": apt.price, "bedrooms": apt.bedrooms, "latitude": apt.latitude, "longitude": apt.longitude } return jsonify(apartments)
def show_charts(): """ Displays graphs related to the users' search as well as general prices from Craigslist. """ # Show number of posts more expensive than search. more_expensive = Posting.get_more_expensive(session['price'], session['bedrooms'], session['origin_latitude'], session['origin_longitude'], session['max_distance']) num_expensive = more_expensive['total'] # Show number of posts farther away than desired distance. farther = Posting.get_farther_away(session['price'], session['bedrooms'], session['origin_latitude'], session['origin_longitude'], session['max_distance']) # Show avg price by bedroom for all 3 cities. seattle_data = Posting.get_bedrooms_price('seattle') portland_data = Posting.get_bedrooms_price('portland') bay_area_data = Posting.get_bedrooms_price('sfbay') return render_template('charts.html', raw_location=session['raw_location'], price=session['price'], avg_rent=session['avg_rent'], num_results=session['num_results'], more_expensive=more_expensive, farther=farther, bayarea=bay_area_data, seattle=seattle_data, portland=portland_data, distance=session['max_distance'], bedrooms=session['bedrooms'], transport=session['transit_method'])
def create_posting(neighborhood_id, email, date, title, desc, contact_info, image_url): """Create a housing posting.""" posting = Posting(neighborhood_id=neighborhood_id, user_email=email, date=date, title=title, desc=desc, contact_info=contact_info, image_url=image_url) db.session.add(posting) db.session.commit()
def load_postings(): """load postings from job posting database.""" with open('data job posts.csv') as csvfile: reader = csv.reader(csvfile, delimiter=",") for row in reader: title, company = row[2:4] description, requirements, qualifications = row[11:14] posting = Posting(title=title, company=company, description=description, requirements=requirements, qualifications=qualifications) db.session.add(posting) db.session.commit()
def test_check_euclidean_distance(self): """ Verifies that check_distance returns list of apartment objects within Euclidean distance range of origin. """ max_rent=7000 num_rooms=1 origin_lat=37.7914448 origin_lon=-122.3929672 desired_distance=5 # Get sample list of 5 apartments. apartments = Posting.get_apartments(max_rent, num_rooms, origin_lat, origin_lon, desired_distance)[:5] for apt in apartments: distance_deg = math.sqrt((apt.latitude - origin_lat)**2 + (apt.longitude - origin_lon)**2) # Convert distance to miles distance_mi = distance_deg * 69.0 result = apt.check_euclidean_distance(origin_lat, origin_lon, desired_distance) self.assertTrue(distance_mi < desired_distance and result)
def test_get_apartments(self): """ Checks that function returns list of apartment objects. """ example_call = Posting.get_apartments(max_rent=7000, num_rooms=1, origin_lat=37.7914448, origin_lon=-122.3929672, desired_distance=5) # origin lat/lon point to an address in San Francisco. # Result should always be a list. self.assertTrue(type(example_call) is list) # Funtion should never return None. self.assertIsNotNone(example_call) # Example call should always result in at least 75 results. # Unless 1 bedrooms in SF start going for more than $7000... self.assertTrue(len(example_call) > 75) self.assertTrue(type(example_call[0].latitude) is float) self.assertTrue(type(example_call[0].longitude) is float) # Check that results are actually from San Francisco. self.assertTrue('sfbay' in example_call[0].url)
def test_calculate_outer_bounds(self): """Verify that result is within expected area.""" self.assertTrue(Posting.calculate_outer_bounds(100, 100, 5) == [99.92753623188406, 99.92753623188406, 100.07246376811594, 100.07246376811594]) self.assertTrue(Posting.calculate_outer_bounds(0, 0, 100) == [-1.4492753623188406, -1.4492753623188406, 1.4492753623188406, 1.4492753623188406])