コード例 #1
0
ファイル: post.py プロジェクト: impiyush83/insta-backend
def get_user_post(username, post_id):
    current_user = generate_user_from_auth_token(
        request.headers.get('access-token'))
    post_user = UserMethods.get_user_by_username(username)
    if current_user.id == post_user.id or \
            post_user.status.value == "public" or \
            FollowerMethods.is_following(current_user.id, post_user.id):
        # send all data like comments, likes and post details
        if not PostMethods.get_record_with_id(post_id):
            raise NotFound("Post not found")
        post = PostMethods.get_record_with_id(post_id)
        comments = post.post_comments
        like_count = post.total_number_of_likes
        likes = post.post_likes
        post_data = dict(caption=post.caption,
                         image=post.image._public_url,
                         post_id=post.id,
                         user_id=post.user_id)
        response = dict(post=post_data,
                        comments=comments,
                        like_count=like_count,
                        likes=likes)
        return jsonify(response)
    else:
        return jsonify(
            dict(message="Profile is private! Follow to see the post !")), 400
コード例 #2
0
ファイル: feed.py プロジェクト: impiyush83/insta-backend
def custom_feed():
    auth_token = request.headers.get('access_token')
    current_user = generate_user_from_auth_token(auth_token)
    #  HIT REDIS FOR ALL POSTS RELATED T
    followees = FollowerMethods.get_followees(current_user.id)
    posts = []
    for followee in followees:
        user = UserMethods.get_record_with_id(followee.followee_id)
        start_time = datetime.utcnow()
        user_posts = redis_client.lrange(user.username, 0, -1)
        print(datetime.utcnow() - start_time)
        for post in user_posts:
            posts.append(json.loads(post))
    return jsonify({"posts": posts})
コード例 #3
0
def follow_requests():
    current_user = generate_user_from_auth_token(
        request.headers.get('access-token'))
    requestors = FollowRequestMethods.get_follow_requests(current_user.id)
    request_list = []
    # HATEOS CONCEPT USAGE
    for request_item in requestors:
        request_list.append(
            dict(requestor_id=request_item.follower_id,
                 follow_accept_url='/friendships/request/accept/' +
                 str(request_item.follower_id),
                 follow_decline_url='/friendships/request/decline/' +
                 str(request_item.follower_id)))
    response = dict(requestors=request_list)
    return jsonify(response), 200
コード例 #4
0
ファイル: like.py プロジェクト: impiyush83/insta-backend
def unlike_action_on_post(username, post_id):
    current_user = generate_user_from_auth_token(
        request.headers.get('access-token'))
    post_user = UserMethods.get_user_by_username(username)
    if current_user.id == post_user.id or \
            post_user.status.value == "public" or \
            FollowerMethods.is_following(current_user.id, post_user.id):
        # send all data like comments, likes and post details
        if not PostMethods.get_record_with_id(post_id):
            raise NotFound("Post not found")
        LikeMethods.delete_record(post_id, post_user.id)
        db.commit()
        return jsonify(dict(message="Successfully unliked the post"))
    else:
        return jsonify(
            dict(message="Profile is private! Follow to unlike the post !")
        ), 400
コード例 #5
0
ファイル: feed.py プロジェクト: impiyush83/insta-backend
def feed():
    auth_token = request.headers.get('access_token')
    current_user = generate_user_from_auth_token(auth_token)
    #  HIT REDIS FOR ALL POSTS RELATED TO
    followees = FollowerMethods.get_followees(current_user.id)
    posts = []
    for followee in followees:
        user = UserMethods.get_record_with_id(followee.followee_id)
        start_time = datetime.utcnow()
        user_posts = db.query(Post).filter(Post.user_id == user.id).all()
        print(datetime.utcnow() - start_time)
        for post in user_posts:
            posts.append(
                dict(caption=post.caption,
                     image=post.image._public_url,
                     user_id=post.user_id))
    return jsonify({"posts": posts})
コード例 #6
0
def unfollow(followee_username):
    follower_user = generate_user_from_auth_token(
        request.headers.get('access-token'))
    followee_user = UserMethods.get_user_by_username(followee_username)
    if followee_user is None:
        raise NotFound('No user with username {username} found'.format(
            username=followee_username))
    # same user id unfollow request
    if follower_user.id == followee_user.id:
        raise BadRequest('Request cannot be proceesed')

    # check if it follows
    if FollowerMethods.is_following(follower_user.id, followee_user.id):
        unfollow_user(follower_user.id, followee_user.id)

    db.commit()
    return jsonify({"message": "Successfully unfollowed"}), 200
コード例 #7
0
ファイル: user.py プロジェクト: impiyush83/insta-backend
def user_profile(username):
    current_user = generate_user_from_auth_token(
        request.headers.get('access-token'))
    profile_user = UserMethods.get_user_by_username(username)

    response = dict(
        name=profile_user.name,
        username=profile_user.username,
        display_picture=dict(profile_user.display_picture)
    )
    #  HATEOS CONCEPT USED FOR POSTS
    #  no circular checking
    if current_user.username != username:
        # check if following
        if FollowerMethods.is_following(current_user.id, profile_user.id):
            response['posts'] = profile_user.all_posts
    else:
        response['posts'] = profile_user.all_posts
    return response
コード例 #8
0
ファイル: post.py プロジェクト: impiyush83/insta-backend
def create_new_post():
    current_user = generate_user_from_auth_token(
        request.headers.get('access-token'))
    request_data = request.form
    image = request.files.getlist('image')[0]
    post_data = dict(caption=request_data.get('caption'),
                     image=image,
                     user_id=current_user.id)
    post = PostMethods.create_record(**post_data)
    # put in redis list
    post_data['image'] = post.image._public_url
    post_data['post_id'] = post.id
    post_data['username'] = current_user.username
    total_user_posts = redis_client.lrange(current_user.username, 0, -1)
    if len(total_user_posts) == int(app.config.get('MAX_REDIS_CACHED_POSTS')):
        redis_client.rpop()

    redis_client.lpush(current_user.username, json.dumps(post_data))

    db.commit()
    return jsonify({"message": "Hurray your post is on its way !!"}), 200
コード例 #9
0
def follow(followee_username):
    follower_user = generate_user_from_auth_token(
        request.headers.get('access-token'))
    followee_user = UserMethods.get_user_by_username(followee_username)
    if followee_user is None:
        raise NotFound('No user with username {username} found'.format(
            username=followee_username))

    # same user id follow request
    if follower_user.id == followee_user.id:
        raise BadRequest('Request cannot be proceesed')

    # check if not follows
    if not FollowerMethods.is_following(follower_user.id, followee_user.id):
        if followee_user.status.value == "public":
            follow_public_user(follower_user.id, followee_user.id)
            db.commit()
            return jsonify({"message": "Successfully followed"}), 200
        else:
            send_follow_request(follower_user.id, followee_user.id)
            db.commit()
            return jsonify({"message": "Follow request sent"}), 200
コード例 #10
0
def decline_follow_request(follower_id):
    followee_user = generate_user_from_auth_token(
        request.headers.get('access-token'))
    decline_follow_requests(follower_id, followee_user.id)
    db.commit()
    return jsonify({"message": "Follow request declined"}), 200