Exemplo n.º 1
0
    def test_get_products_within_dates(self):
        start_date = convert_string_to_datetime('2015-10-31')
        end_date = convert_string_to_datetime('2015-11-01')

        products = get_products_within_dates(start_date, end_date, User.query.all())

        self.assertEqual(products[0].prod_id, 1)
Exemplo n.º 2
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)
Exemplo n.º 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)
Exemplo n.º 4
0
 def test_convert_string_to_datetime(self):
     test_date = convert_string_to_datetime("2015-11-18")
     self.assertEqual(test_date, datetime(2015, 11, 18))