Exemple #1
0
def user_to_dict(user):
    from configs import config
    from controllers import get_video_states
    from util import get_users_stats
    
    user_stats = get_users_stats([user.id])

    user_dict = {
        'id': user.id,
        'email': user.email,
        'username': user.username,
        'facebook_id': user.facebook_id,
        'twitter_id': user.twitter_id,
        'instagram_id': user.twitter_id,
        'google_id': user.google_id,
        'first_name': user.first_name,
        'last_name': None,
        'bio': user.bio or config.DEFAULT_BIO,
        'profile_picture': user.profile_picture,
        'cover_picture': user.cover_picture,
        'profile_video': user.profile_video,
        'gender': user.gender,
        'follower_count': user_stats[user.id]['follower_count'],
        'following_count': user_stats[user.id]['following_count'],
        'answer_count': user_stats[user.id]['answer_count'],
        'likes_count': 0,
        'location': location_dict(user.lat, user.lon, user.location_name, user.country_name, user.country_code),
        'last_updated': int(time.mktime(user.last_updated.timetuple())),
        'facebook_write_permission': user.facebook_write_permission,
        'twitter_write_permission': bool(user.twitter_token),
        'admin_level': 1 if user.id in config.ADMIN_USERS else 0,
        'view_count': user_stats[user.id]['view_count'],
        'web_link': 'http://frankly.me/{username}'.format(username=user.username),
        'channel_id': 'user_{user_id}'.format(user_id=user.id),
        'profile_video_requested': user_stats[user.id]['is_requested'],

        'user_type': user.user_type,
        'user_title': user.user_title,
        'question_count': user_stats[user.id]['question_count'],
        'interests': [],
        'profile_videos': get_video_states({user.profile_video: user.cover_picture})[user.profile_video] if user.profile_video else {}
    }

    if user_dict['profile_video']:
        user_dict['answer_count'] = user_dict['answer_count']+1
    return user_dict
Exemple #2
0
def guest_users_to_dict(users, current_user_id, cur_user_interest_tags=None):
    if not users:
        return []
    from configs import config
    from controllers import get_video_states
    from util import get_users_stats

    user_stats = get_users_stats([user.id for user in users], cur_user_id=current_user_id)
    
    user_dicts = []

    for user in users:

        user_dict = {
            'id': user.id,
            'username': user.username,
            'first_name': user.first_name,
            'last_name': None,
            'bio': user.bio or config.DEFAULT_BIO,
            'profile_picture': user.profile_picture,
            'cover_picture': user.cover_picture,
            'profile_video': user.profile_video,
            'gender': user.gender,
            'follower_count': user_stats[user.id]['follower_count'],
            'following_count': user_stats[user.id]['following_count'],
            'answer_count': user_stats[user.id]['answer_count'],
            'likes_count': 0,
            'location': location_dict(user.lat, user.lon, user.location_name, user.country_name, user.country_code),
            'is_follower':False,
            'is_following':user_stats[user.id]['is_following'],
            'allow_anonymous_question': user.allow_anonymous_question,
            'view_count' : user_stats[user.id]['view_count'],
            'user_type':user.user_type,
            'user_title':user.user_title,
            'profile_videos':get_video_states({user.profile_video:user.cover_picture})[user.profile_video] if user.profile_video else {},
            'web_link':'http://frankly.me/{username}'.format(username=user.username),
            'channel_id':'user_{user_id}'.format(user_id=user.id),
            'profile_video_requested':user_stats[user.id]['is_requested'],
            'twitter_handle':user.twitter_handle,
            'twitter_invite_text':"Hey @{twitter_handle}, I have a question for you. Can you please reply Frankly? @franklymeapp".format(twitter_handle=user.twitter_handle or '')
        }
        if user_dict['profile_video']:
            user_dict['answer_count'] = user_dict['answer_count']+1
        user_dicts.append(user_dict)
    return user_dicts
Exemple #3
0
def posts_to_dict(posts, cur_user_id=None, distance=None, include_comments=3):
    if not posts:
        return []

    from configs import config
    from controllers import get_video_states, get_questions, get_thumb_users, get_posts_stats, get_comments_for_posts
    from util import get_users_stats
    
    user_list = set()
    answer_media_urls = {}
    question_ids = []
    post_ids = []
    
    for p in posts:
        user_list.add(p.question_author)
        user_list.add(p.answer_author)
        answer_media_urls[p.media_url] = p.thumbnail_url
        question_ids.append(p.question)
        post_ids.append(p.id)

    users = get_thumb_users(user_list, cur_user_id=cur_user_id)
    media_urls = get_video_states(answer_media_urls)
    questions = get_questions(question_ids, cur_user_id=cur_user_id)
    post_stats = get_posts_stats(post_ids, cur_user_id)
    user_stats = get_users_stats(user_list, cur_user_id)
    
    if include_comments:
        all_comments = get_comments_for_posts(cur_user_id, post_ids, offset=0, limit=include_comments)

    posts_dict = []
    for post in posts:
        
        p_dict = {
            'id': post.id,
            'question_author': {
                'id':users[post.question_author]['id'] if not questions[post.question]['is_anonymous'] else '',
                'username': users[post.question_author]['username'] if not questions[post.question]['is_anonymous'] else 'Anonymous',
                'first_name': users[post.question_author]['first_name'] if not questions[post.question]['is_anonymous'] else 'Anonymous',
                'last_name': None,
                'gender': users[post.question_author]['gender'],
                'profile_picture': users[post.question_author]['profile_picture'] if not questions[post.question]['is_anonymous'] else None,
                'channel_id':'user_{user_id}'.format(user_id=users[post.question_author]['id'])
            },
            'answer_author': {
                'id':users[post.answer_author]['id'],
                'username':users[post.answer_author]['username'],
                'first_name': users[post.answer_author]['first_name'],
                'last_name': None,
                'profile_picture': users[post.answer_author]['profile_picture'],
                'location': users[post.answer_author]['location'],
                'gender': users[post.answer_author]['gender'],
                'bio':users[post.answer_author]['bio'] or config.DEFAULT_BIO,
                'user_type':users[post.answer_author]['user_type'],
                'user_title':users[post.answer_author]['user_title'],
                'allow_anonymous_question': users[post.answer_author]['user_title'],
                'is_following':users[post.answer_author]['is_following'],
                'follower_count': user_stats[post.answer_author]['follower_count'],
                'channel_id':'user_{user_id}'.format(user_id=users[post.answer_author]['id'])

            },
            'question': {
                'id':questions[post.question]['id'],
                'body': questions[post.question]['body'],
                'question_type': 'text',
                'timestamp': int(time.mktime(questions[post.question]['timestamp'].timetuple())),
                'tags': [],
                'is_anonymous': bool(questions[post.question]['is_anonymous']),
                'slug':questions[post.question]['slug'],
                'open_question':questions[post.question]['open_question']
            },
            'answer': {
                'body': '',
                'media': media_dict(post.media_url, post.thumbnail_url),
                'type': post.answer_type,
                'timestamp': int(time.mktime(post.timestamp.timetuple())),
                'tags': [],
                'media_urls':media_urls[post.media_url]
            },
            'liked_count': post_stats[post.id]['like_count'],
            # to store count and list of user ids
            'comment_count': post_stats[post.id]['comment_count'],
            'is_liked': post_stats[post.id]['is_liked'],
            #'is_reshared': is_reshared(post.id, cur_user_id),
            'deleted': post.deleted,
            'tags':[],
            'location': location_dict(post.lat, post.lon, post.location_name, post.country_name, post.country_code),
            'distance':distance,
            'client_id':post.client_id,
            'ready':post.ready,
            'popular':post.popular,
            'view_count':post_stats[post.id]['view_count'],
            'web_link':'http://frankly.me/p/{client_id}'.format(client_id=post.client_id),
            'whatsapp_share_count':post_stats[post.id]['whatsapp_share_count'],
            'other_share_count':post_stats[post.id]['other_share_count'],
            'comments':all_comments[post.id] if include_comments else {}
        }
        p_dict['answer']['media']['thumbnail_url'] = p_dict['answer']['media_urls']['thumb']
        posts_dict.append(p_dict)


    return posts_dict