Esempio n. 1
0
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)
Esempio n. 2
0
    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)
Esempio n. 3
0
    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)