def confirm_cart():
    if len(session['cart']) != 10:
        flash('10件選択してください', 'danger')
        return redirect(url_for('list_cart'))
    session['confirm'] = True
    # spots = list(map(lambda spot_id: ChootripApi.get_spot(spot_id), session['cart']))
    spots = ChootripApi.get_spots(session['cart'])
    spots = set_cart_added(spots)
    spots = set_one_image(spots)
    return render_template('confirm_cart.html', spots=spots)
def extract_10_recommend_spots(similarities_dict):
    similarities_sorted = sorted(similarities_dict.items(),
                                 key=lambda x: -x[1])
    # chunk_size件ずつ条件にあったスポットを取り出す
    chunk_size = 50
    chunked_similarities = [
        similarities_sorted[n:n + chunk_size]
        for n in range(0, len(similarities_sorted), chunk_size)
    ]
    recommend_spots = []
    for similarities_by_chunk_size in chunked_similarities:
        spot_ids = []
        for spot_similarity_info in similarities_by_chunk_size:
            spot_id = spot_similarity_info[0]
            _similarity = spot_similarity_info[1]
            spot_ids.append(spot_id)
        spots = ChootripApi.get_spots(spot_ids)
        recommend_spots.extend(spots)

        # exclude selected spots
        recommend_spots = list(
            filter(lambda s: s['id'] not in session['cart'], recommend_spots))

        # extract by counts
        recommend_spots = list(
            filter(lambda s: s['count'] >= 153, recommend_spots))

        # set similarity
        for spot in recommend_spots:
            spot['similarity'] = similarities_dict[str(spot['id'])]

        # sort
        recommend_spots = list(
            sorted(recommend_spots, key=lambda s: -s['similarity']))

        if len(recommend_spots) > 10:
            break
    return recommend_spots[:10]
def show_recommend():
    if len(session['cart']) != 10:
        flash('不正な入力です', 'danger')
        return redirect(url_for('top'))
    # TODO: もうすでにこの学籍番号のデータが登録されている場合は,ここで弾く
    session.pop('confirm', None)

    # GET: RECOMMEND_SPOTS
    # keys: ['similarities', 'user_vec']
    recommend_data = ChootripApi.get_recommend(session['cart'])
    recommend_spots = extract_10_recommend_spots(
        similarities_dict=recommend_data['similarities'])

    topics_with_words = get_topics_with_words()

    # GET: PREFERENCE
    s = SpreadSheet()
    normalized_user_vec = recommend_data['normalized_user_vec']
    s.update_normalized_topic_result(session['username'], normalized_user_vec)
    user_vec = recommend_data['user_vec']
    s.update_topic_result(session['username'], user_vec)

    selected_spots = ChootripApi.get_spots(session['cart'])
    selected_spots_name = []
    for selected_spot in selected_spots:
        selected_spots_name.append(selected_spot['title'])
    s.update_selected_spots(session['username'], selected_spots_name)

    recommend_spots_name = []
    for recommend_spot in recommend_spots:
        recommend_spots_name.append(recommend_spot['title'])
    s.update_recommend_result(session['username'], recommend_spots_name)

    return render_template('recommends.html',
                           recommend_spots=recommend_spots,
                           topics=zip(topics_with_words, normalized_user_vec))