Exemple #1
0
    def test_googlemaps_api(self):
        searchcenter = '94612'
        postalcodes = [('94608',), ('94102',), ('94040',), ('95376',), ('95451',),
                       ('92277',), ('10013',), ('02139',)]

        within20 = search_radius(searchcenter, postalcodes, 20)
        within60 = search_radius(searchcenter, postalcodes, 60)
        within200 = search_radius(searchcenter, postalcodes, 200)

        self.assertEqual(sorted(within20), sorted(['94608', '94102']))
        self.assertEqual(sorted(within60), sorted(['94608', '94102', '94040',
                                                   '95376']))
        self.assertEqual(sorted(within200), sorted(['94608', '94102', '94040',
                                                    '95376', '95451']))
Exemple #2
0
    def test_search_radius(self):
        searchcenter1 = '94612'
        searchcenter2 = '94109'
        searchcenter3 = '94040'
        searchcenter4 = '10013'
        postalcodes = [('94612',), ('94109',), ('94040',), ('95376',), ('10013',)]

        self.assertEqual(sorted(search_radius(searchcenter1, postalcodes, 20)), sorted(['94109', '94612']))
        self.assertEqual(sorted(search_radius(searchcenter1, postalcodes, 60)), sorted(['94109', '94612', '94040', '95376']))
        self.assertEqual(sorted(search_radius(searchcenter2, postalcodes, 20)), sorted(['94109', '94612']))
        self.assertEqual(sorted(search_radius(searchcenter2, postalcodes, 60)), sorted(['94109', '94612', '94040']))
        self.assertEqual(sorted(search_radius(searchcenter3, postalcodes, 20)), ['94040'])
        self.assertEqual(sorted(search_radius(searchcenter4, postalcodes, 20)), ['10013'])
Exemple #3
0
def show_results():
    """ Shows search results based on location and optional filters for
        category and brand. Uses geolocation-python libary, which uses the
        GoogleMaps API.

    """

    try:
        search_miles = int(request.args.get("search_miles"))
    except ValueError:
        flash("Search radius must be an integer. Please try again.")
        return redirect('/success')

    try:
        search_start_date = convert_string_to_datetime(
            request.args.get("search_start_date"))
    except ValueError:
        flash("Search dates must be formatted YYYY-mm-dd. Please try again.")
        return redirect('/success')

    try:
        search_end_date = convert_string_to_datetime(
            request.args.get("search_end_date"))
    except ValueError:
        flash("Search dates must be formatted YYYY-mm-dd. Please try again.")
        return redirect('/success')

    search_area = request.args.get("search_area")
    category_id = int(request.args.get("category_id"))
    brand_id = int(request.args.get("brand_id"))

    # This is the number of rental days.
    days = (search_end_date - search_start_date).days + 1

    # Puts this search info into the session so we can refer to it in
    # future pages (the "return to your search results" link).
    session['search_start_date'] = search_start_date
    session['search_end_date'] = search_end_date
    session['num_days'] = days
    session['search_area'] = search_area
    session['search_radius'] = search_miles
    session['search_category_id'] = category_id
    session['search_brand_id'] = brand_id

    # Find distinct postal codes in the database.
    query = db.session.query(User.postalcode).distinct()
    postalcodes = query.all()

    # Get zipcodes in the database that are within the search radius.
    # This uses a helper function in search_helpers, which in turn uses
    # the geolocation-python library.
    # In future version of project, save these distance searches in the
    # database.
    postal_codes = search_radius(search_center=search_area,
                                 postalcodes=postalcodes,
                                 radius=search_miles)
    # We use list of zipcodes we get above and to then get users within in those
    # zipcodes. We remove the logged in user so we don't show his or her own
    # products.
    logged_in_user = User.query.filter(User.email == session['user']).one()
    users_in_area = get_users_in_area(postal_codes, logged_in_user.user_id)

    # From the list of users we get above, get products those users have listed
    # for rent and are currently available within the search dates.
    available_products = get_products_within_dates(
        start_date=search_start_date,
        end_date=search_end_date,
        users=users_in_area)
    # Filter out products based on optional category and brand filters.
    filtered_products = filter_products(available_products,
                                        category_id=category_id,
                                        brand_id=brand_id)
    # Get categories of interest. If the vale is -1, the user is interested in
    # all categories (can currently select only one or all.)
    if category_id < 0:
        search_categories = Category.query.all()
    else:
        search_categories = [Category.query.get(category_id)]

    # Make a dictionary of available products with categories as the keys of the
    # dictionary.
    products_by_category = categorize_products(categories=search_categories,
                                               products=filtered_products)

    # Create a list of sorted category names so we can display products by
    # category in some kind of consistent order.
    sorted_category_names = sorted(products_by_category.keys())

    # Get all categories and brands to show in the drop downs in the re-search
    # form on top of the search results page.
    all_categories = Category.query.all()
    all_brands = Brand.query.all()

    return render_template("search-results.html",
                           location=search_area,
                           miles=search_miles,
                           search_categories=sorted_category_names,
                           products=products_by_category,
                           product_categories=all_categories,
                           product_brands=all_brands)
Exemple #4
0
def show_results():
    """ Shows search results based on location and optional filters for
        category and brand. Uses geolocation-python libary, which uses the
        GoogleMaps API.

    """

    try:
        search_miles = int(request.args.get("search_miles"))
    except ValueError:
        flash("Search radius must be an integer. Please try again.")
        return redirect('/success')

    try:
        search_start_date = convert_string_to_datetime(request.args.get("search_start_date"))
    except ValueError:
        flash("Search dates must be formatted YYYY-mm-dd. Please try again.")
        return redirect('/success')

    try:
        search_end_date = convert_string_to_datetime(request.args.get("search_end_date"))
    except ValueError:
        flash("Search dates must be formatted YYYY-mm-dd. Please try again.")
        return redirect('/success')

    search_area = request.args.get("search_area")
    category_id = int(request.args.get("category_id"))
    brand_id = int(request.args.get("brand_id"))

    # This is the number of rental days.
    days = (search_end_date - search_start_date).days + 1

    # Puts this search info into the session so we can refer to it in
    # future pages (the "return to your search results" link).
    session['search_start_date'] = search_start_date
    session['search_end_date'] = search_end_date
    session['num_days'] = days
    session['search_area'] = search_area
    session['search_radius'] = search_miles
    session['search_category_id'] = category_id
    session['search_brand_id'] = brand_id

    # Find distinct postal codes in the database.
    query = db.session.query(User.postalcode).distinct()
    postalcodes = query.all()

    # Get zipcodes in the database that are within the search radius.
    # This uses a helper function in search_helpers, which in turn uses
    # the geolocation-python library.
    # In future version of project, save these distance searches in the
    # database.
    postal_codes = search_radius(search_center=search_area,
                                 postalcodes=postalcodes, radius=search_miles)
    # We use list of zipcodes we get above and to then get users within in those
    # zipcodes. We remove the logged in user so we don't show his or her own
    # products.
    logged_in_user = User.query.filter(User.email == session['user']).one()
    users_in_area = get_users_in_area(postal_codes, logged_in_user.user_id)

    # From the list of users we get above, get products those users have listed
    # for rent and are currently available within the search dates.
    available_products = get_products_within_dates(start_date=search_start_date,
                                                   end_date=search_end_date,
                                                   users=users_in_area)
    # Filter out products based on optional category and brand filters.
    filtered_products = filter_products(available_products, category_id=category_id,
                                        brand_id=brand_id)
    # Get categories of interest. If the vale is -1, the user is interested in
    # all categories (can currently select only one or all.)
    if category_id < 0:
        search_categories = Category.query.all()
    else:
        search_categories = [Category.query.get(category_id)]

    # Make a dictionary of available products with categories as the keys of the
    # dictionary.
    products_by_category = categorize_products(categories=search_categories,
                                               products=filtered_products)

    # Create a list of sorted category names so we can display products by
    # category in some kind of consistent order.
    sorted_category_names = sorted(products_by_category.keys())

    # Get all categories and brands to show in the drop downs in the re-search
    # form on top of the search results page.
    all_categories = Category.query.all()
    all_brands = Brand.query.all()

    return render_template("search-results.html", location=search_area,
                           miles=search_miles,
                           search_categories=sorted_category_names,
                           products=products_by_category,
                           product_categories=all_categories,
                           product_brands=all_brands)