Пример #1
0
def view_only(option, choice):
    if option == 'category':
        view = mongo.db.categories.find_one({option: choice})
    elif option == 'difficulty':
        view = mongo.db.difficulty.find_one({option: choice})
    else:
        view = mongo.db.glass.find_one({option: choice})

    title = view[option].title()

    # Sort Options
    # Check if session exists
    if session.get('sort_options') is None:
        session['sort_options'] = default_sort_options

    if request.method == "POST":
        sort_drinks(mongo)
        return redirect(url_for('view_only',
                                view=view,
                                option=option,
                                choice=choice))

    drinks_per_page = session['sort_options'][0]
    sort_by = session['sort_options'][2]
    sort_order = session['sort_options'][4]
    sort_options = session['sort_options']

    # Pagination
    current_page = int(request.args.get('current_page', 1))
    total_drinks = mongo.db.drinks.find({option: choice}).count()
    num_pages = range(1, int(math.ceil(total_drinks / drinks_per_page)) + 1)
    drinks = mongo.db.drinks.find({option: choice}) \
        .sort(sort_by, sort_order).skip(
        (current_page - 1) * drinks_per_page).limit(drinks_per_page)

    # Summary - (example) 'showing 1 - 9 of 15 results'
    x = current_page * drinks_per_page
    first_result_num = x - drinks_per_page + 1
    last_result_num = x if x < total_drinks else total_drinks

    return render_template('view_only.html',
                           view=view,
                           option=option,
                           choice=choice,
                           title=title,
                           drinks=drinks,
                           # Display Options
                           sort_options=sort_options,
                           # Pagination
                           current_page=current_page,
                           pages=num_pages,
                           first_result_num=first_result_num,
                           last_result_num=last_result_num,
                           # Items for Drop Downs
                           num_drinks_list=num_drinks_list,
                           sort_by_list=sort_by_list,
                           sort_order_list=sort_order_list)
Пример #2
0
def index():
    categories = mongo.db.categories.find()
    glass_types = mongo.db.glass.find()
    difficulties = mongo.db.difficulty.find()

    # Get Num (arg2) Suggested Drinks For User
    suggestions = get_suggestions(mongo, 4)

    # Sort Options
    # Check if session exists
    if session.get('sort_options') is None:
        session['sort_options'] = default_sort_options

    if request.method == "POST":
        sort_drinks(mongo)
        return redirect(url_for('index'))

    # Set Drinks Display Options
    drinks_per_page = session['sort_options'][0]
    sort_by = session['sort_options'][2]
    sort_order = session['sort_options'][4]
    sort_options = session['sort_options']

    # Pagination
    current_page = int(request.args.get('current_page', 1))
    total_drinks = mongo.db.drinks.count()
    num_pages = range(1, int(math.ceil(total_drinks / drinks_per_page)) + 1)
    drinks = mongo.db.drinks.find().sort(sort_by, sort_order).skip(
        (current_page - 1) * drinks_per_page).limit(drinks_per_page)

    # Summary - (example) 'showing 1 - 9 of 15 results'
    x = current_page * drinks_per_page
    first_result_num = x - drinks_per_page + 1
    last_result_num = x if x < total_drinks else total_drinks

    return render_template(
        'index.html',
        drinks=drinks,
        categories=categories,
        glass_types=glass_types,
        difficulties=difficulties,
        suggestions=suggestions,
        # Pagination & Sumarry
        current_page=current_page,
        pages=num_pages,
        first_result_num=first_result_num,
        last_result_num=last_result_num,
        # Display Options
        sort_options=sort_options,
        # Items for Drop Downs
        num_drinks_list=num_drinks_list,
        sort_by_list=sort_by_list,
        sort_order_list=sort_order_list)
Пример #3
0
def search():
    # Get Categories for filter dropdowns
    all_categories = mongo.db.categories.find()
    all_glass_types = mongo.db.glass.find()
    all_difficulties = mongo.db.difficulty.find()

    # Page Title
    title = "Search"

    # Search
    if request.args:
        # Put user selected filters into 'filter_dict' for use in search query
        filters = request.args.to_dict()
        filter_dict = {}
        list_of_filters = [
            'category_filter', 'glassType_filter', 'difficulty_filter'
        ]
        for k, v in filters.items():
            if k in list_of_filters and v != "":
                new_k = k.split("_")[0]
                filter_dict[new_k] = v

        # Track filter values with pagination
        category_filter = filters[
            'category_filter'] if 'category_filter' in filters else []
        glass_type_filter = filters[
            'glassType_filter'] if 'glassType_filter' in filters else []
        difficulty_filter = filters[
            'difficulty_filter'] if 'difficulty_filter' in filters else []

        # Get Search Term
        find = request.args['find']

        # Sort Options
        # Check if session exists
        if session.get('sort_options') is None:
            session['sort_options'] = default_sort_options

        if request.method == "POST":
            sort_drinks(mongo)
            # State if sort by relevance first
            session['sort_options'][
                3] = 1 if 'relevance' in request.form else 0
            return redirect(
                url_for('search',
                        category_filter=category_filter,
                        glassType_filter=glass_type_filter,
                        difficulty_filter=difficulty_filter,
                        find=find))

        # Set Drinks Display Options
        results_per_page = session['sort_options'][0]
        sort_by = session['sort_options'][2]
        sort_order = session['sort_options'][4]

        current_page = int(request.args.get('current_page', 1))

        # Set 'sort_values' based on user input
        sort_values = [(sort_by, sort_order), ('name', pymongo.ASCENDING)] if \
            session['sort_options'][3] != 1 else [
            ('score', {'$meta': 'textScore'}),
            (sort_by, sort_order),
            ('name', pymongo.ASCENDING)]

        sort_options = session['sort_options']

        # Create 'search_str' for use in search
        search_str = {
            '$text': {
                '$search': find
            }
        } if find != "" else {
            'name': {
                '$regex': ""
            }
        }

        results = mongo.db.drinks.find({
            '$and': [search_str, filter_dict]
        }, {
            'score': {
                '$meta': 'textScore'
            }
        }).sort(sort_values).skip(
            (current_page - 1) * results_per_page).limit(results_per_page)

        num_results = results.count()

        # If no results for search
        if num_results == 0:
            return render_template(
                'search.html',
                title="No Results Found",
                find=find,
                num_results=num_results,
                # Items for filters
                all_categories=all_categories,
                all_glass_types=all_glass_types,
                all_difficulties=all_difficulties)

        # Pagination & Summary
        num_pages = range(1,
                          int(math.ceil(num_results / results_per_page)) + 1)
        x = current_page * results_per_page
        first_result_num = x - results_per_page + 1
        last_result_num = x if x < num_results else num_results

        # Set 'max-weight'
        # - used to calculate score relevance as % of max score returned
        max_weight = mongo.db.drinks.find_one(
            {'$and': [{
                '$text': {
                    '$search': find
                }
            }, filter_dict]}, {'score': {
                '$meta': 'textScore'
            }},
            sort=[('score', {
                '$meta': 'textScore'
            })])['score'] if find != "" else None

        # Page Title
        title = "Search Results"

        return render_template(
            'search.html',
            title=title,
            find=find,
            # Results
            results=results,
            first_result_num=first_result_num,
            last_result_num=last_result_num,
            # Search result scores
            max_weight=max_weight,
            # Pagination | Navigation
            results_per_page=results_per_page,
            # Check if needed?
            current_page=current_page,
            pages=num_pages,
            # Pagination | Filter Choices
            category_filter=category_filter,
            glassType_filter=glass_type_filter,
            difficulty_filter=difficulty_filter,
            # Items for filters
            all_categories=all_categories,
            all_glass_types=all_glass_types,
            all_difficulties=all_difficulties,
            # Display Options
            sort_options=sort_options,
            # Items for Drop Downs
            num_drinks_list=num_drinks_list,
            sort_by_list=sort_by_list,
            sort_order_list=sort_order_list)

    return render_template(
        'search.html',
        title=title,
        # Items for filters
        all_categories=all_categories,
        all_glass_types=all_glass_types,
        all_difficulties=all_difficulties)