Exemple #1
0
def profile(username, friends_page_number=1):
    logged_user = None
    rel = None
    friends_page = False
    friends_per_page = 3
    profile_messages = []
    user = User.getByName(username)

    if user:
        if session['username']:
            logged_user = User.getByName(session['username'])
            rel = Relationship.get_relationship_status(logged_user.id, user.id)

        # get user friends
        friends_list = Relationship.get_friends(
            user=logged_user.id,
            rel_type=RELATIONSHIP_TYPE.get(FRIENDS),
            status=STATUS_TYPE.get(APPROVED)
        )

        friends_total = len(friends_list)

        if 'friends' in request.url:
            friends_page = True

            # pagination
            limit = friends_per_page * friends_page_number
            offset = limit - friends_per_page
            if friends_total >= limit:
                friends = friends_list[offset:limit]
            else:
                friends = friends_list[offset:friends_total]
        else:
            if friends_total >= 5:
                friends = friends_list[:5]
            else:
                friends = friends_list

        form = FeedPostForm()

        if logged_user and (rel == "SAME" or rel == "FRIENDS_APPROVED"):
            profile_messages = Message.getMessages(logged_user.id)

        return render_template('user/profile.html',
                               user=user,
                               logged_user=logged_user,
                               rel=rel,
                               friends=friends,
                               friends_total=friends_total,
                               friends_page=friends_page,
                               form=form,
                               profile_messages=profile_messages
                               )
    else:
        abort(404)
def process_message(message):

    # get the from_user friends
    from_user = User.getById(message.from_user)
    friends = Relationship.get_friends(user=from_user.id,
                                       rel_type=RELATIONSHIP_TYPE.get(FRIENDS),
                                       status=STATUS_TYPE.get(APPROVED))
    """
        Fan out pattern: means that each user have a list of posts of users that are friends with him (friends_approved).
        So each user have it's feed.
        NB: Fan out pattern is Scalable.
    """

    # get the from_user's friends
    for friend in friends:
        rel_status = Relationship.get_relationship_status(
            friend.to_user, message.to_user)
        if rel_status != "BLOCKED":
            Feed(
                user=friend.to_user,
                message=message.id,
            ).save_database()

    return True