def make_resp_multitype(current_user_id, resp, order_key, reverse_sort=True):
    users = filter(lambda x: resp[x]['type'] == 'user', resp)
    posts = filter(lambda x: resp[x]['type'] == 'post', resp)
    questions = filter(lambda x: resp[x]['type'] == 'question', resp)

    users = User.query.filter(User.id.in_(users)).all()
    posts = Post.query.filter(Post.id.in_(posts)).all()
    questions = Question.query.filter(Question.id.in_(questions)).all()

    users = [{'type':'user',
              order_key:resp[u.id][order_key],
              'user':guest_user_to_dict(u, current_user_id)
             } for u in users]

    questions = questions_to_dict(questions, current_user_id)
    questions = [{'type':'question',
                  order_key:resp[q['id']][order_key],
                  'question':q
                 } for q in questions]

    posts = posts_to_dict(posts, current_user_id)
    posts = [{'type':'post',
              order_key:resp[p['id']][order_key],
              'post':p
             } for p in posts]

    final_list = users + questions + posts
    final_list.sort(key=lambda item: item[order_key], reverse=reverse_sort)

    return final_list
Exemple #2
0
def get_home_feed(cur_user_id, offset, limit=10, club_questions=True):
    from math import sqrt
    from random import randint

    celebs_following = get_celebs_following(cur_user_id)

    fed_upto_time, fed_upto = get_and_update_fed_marker(cur_user_id, offset)

    print 'fed_upto', fed_upto
    print 'fed_upto_time', fed_upto_time

    # TODO: rewrite query to get feeds after LAST FED MARKER
    posts = db.session.execute(text("""SELECT posts.show_after, posts.id,
                                        posts.question_author, posts.question,
                                        posts.answer_author, posts.media_url,
                                        posts.thumbnail_url, posts.answer_type,
                                        posts.timestamp, posts.deleted, posts.lat,
                                        posts.lon, posts.location_name,
                                        posts.country_name, posts.country_code,
                                        posts.ready, posts.popular,
                                        posts.view_count, posts.client_id
                                        FROM posts INNER JOIN user_follows 
                                        ON user_follows.followed = posts.answer_author
                                            AND user_follows.user = :cur_user_id and user_follows.unfollowed=:unfollowed 
                                        WHERE deleted=false 
                                            AND answer_author != :cur_user_id
                                            AND posts.timestamp <= :fed_upto_time
                                        ORDER BY posts.timestamp DESC,posts.show_after DESC
                                        LIMIT :offset, :limit
                                    """),
                                    params={
                                        'cur_user_id':cur_user_id,
                                        'offset':offset,
                                        'limit':limit,
                                        'unfollowed':False,
                                        'fed_upto_time':fed_upto_time
                                        }
                                )

    posts = list(posts)
    posts = posts_to_dict(posts, cur_user_id)

    shortner = 0
    questions = []
    feeds = []
    _q_len = 0

    question_pages = []
    question_pages_fetched = 0

    if len(celebs_following) > 0:
        while question_pages_fetched < 4:
            # TODO: logic to get question pages from different celebs.
            _q_len, questions, following = get_question_from_followings(celebs_following, cur_user_id=cur_user_id)
            if questions:
                shortner += 1
                question_pages.append((following, questions))
            question_pages_fetched += 1

    if posts:
        print 'posts to hain bhai : ', len(posts)

        # added logic to get count of posts + question pages ----> limit
        if len(posts) + shortner <= limit:
            shortner = 0
        else:
            shortner = len(posts) + shortner - limit

        feeds = [{'type':'post', 'post':post}
                 for post in posts[:len(posts) - shortner]
                 ]

    for question_page in question_pages:
        if club_questions:
            question_user = thumb_user_to_dict(User.query.filter(User.id == question_page[0]).first(), cur_user_id)
            question_user['questions'] = []
            for q in question_page[1]:
                question_user['questions'].append(question_to_dict(q, cur_user_id))
                if len(q.body) > 150:
                    break
                if randint(0, 9) % 2 == 0:
                    break
            idx = randint(0, len(feeds))
            print 'inserting question_page at index : ', idx
            feeds.insert(idx, {'questions': question_user, 'type': 'questions'})
        else:
            questions = []
            for question_page in question_pages:
                questions.extend(question_page[1])
            questions = questions_to_dict(questions, cur_user_id)
            for q in questions:
                idx = randint(0, len(feeds))
                print 'inserting question at index : ', idx
                feeds.insert(idx, {'question': q, 'type': 'question'})




    tentative_idx = -1
    if int(len(celebs_following) * sqrt(_q_len)):
        tentative_idx = offset + limit + int(len(celebs_following) * sqrt(_q_len))

    # TODO: limit must be replaced by len(posts)
    next_index = offset+limit-shortner if posts else tentative_idx if offset < 40 else -1

    return feeds, next_index