示例#1
0
def news_feed():
    """ Get all posts by auth user and friends

        Returns:
            success (boolean)
            posts (list)
            total (int)
            query_args (dic)
        """
    try:
        user_ids = [
            (
                friend.requester_id if friend.requester_id != auth_user_id()
                else friend.receiver_id
            ) for friend in filter_friends()
        ]
        user_ids.append(auth_user_id())

        query = Post.query.filter(Post.user_id.in_(user_ids))
        posts = filter_model(Post, query, count_only=False)
        total = filter_model(Post, query, count_only=True)
        return jsonify({
            'success': True,
            'posts': [
                post.format() for post in posts
            ],
            'total': total,
            'query_args': request.args,
        })
    except Exception as e:
        abort(400)
示例#2
0
def toggle_post_reacts(post_id):
    """ Create new react or delete if exist

        Parameters:
            post_id (int): Id of post to which react will belong

        Returns:
            success (boolean)
            react (list)
    """
    try:

        post = Post.query.filter(Post.id == post_id).first_or_404()

        user_react = React.query.filter(
            React.post_id == post_id,
            React.user_id == auth_user_id()).one_or_none()

        # toggle react
        if user_react is None:
            # create react
            react = React(**{'user_id': auth_user_id(), 'post_id': post_id})
            react.insert()
        else:
            # delete
            user_react.delete()

        return jsonify({"success": True, "post": post.format()})

    except Exception as e:
        abort(400)
示例#3
0
def delete_posts(post_id):
    """ Delete posts

        Parameters:
            post_id (int): Id of post

        Internal Parameters:
            user_id (string): Internal parameter extracted from current_user

        Returns:
            success (boolean)
            deleted_post (dict)
    """
    # vars
    post = Post.query.filter(Post.id == post_id).first_or_404()

    # can delete own post only
    if post.user_id != auth_user_id():
        abort(403)

    try:
        # delete images
        for image in post.images:
            image.delete()

        # delete post
        post.delete()

        return jsonify({"success": True, "deleted_id": post.id})
    except Exception as e:
        abort(400)
示例#4
0
def create_comments():
    """ Create new comments

        Post data:
            post_id (int): Id of post to which comment will belong
            content (string): Content for the comment

        Returns:
            success (boolean)
            comment (dict)
    """
    # vars
    data = request.get_json()

    validate_comment_create_data(data)

    # create comment
    comment = Comment(
        **{
            'content': data.get('content'),
            'user_id': auth_user_id(),
            'post_id': data.get('post_id')
        })

    try:
        comment.insert()

        return jsonify({"success": True, "comment": comment.format()})
    except Exception as e:
        abort(400)
示例#5
0
def create_images():
    """ Create new images

        Internal Parameters:
            image (FileStorage): Image

        Returns:
            success (boolean)
            image (list)
    """
    # vars
    image_file = request.files.get('image')

    validate_image_data({"image": image_file})

    image_url_set = create_img_set(image_file)

    # create image
    image = Image(**{
        "user_id": auth_user_id(),
        "url": json.dumps(image_url_set)
    })

    try:
        image.insert()
        # return the result
        return jsonify({'success': True, 'image': image.format()})
    except Exception as e:
        abort(400)
示例#6
0
def attach_images_to_post(post_id):
    """ Attach images to post

        Parameters:
            post_id (int): Id of post

        Post data:
            image_ids (list): List of image ids

        Returns:
            success (boolean)
            post (dict)
    """
    # vars
    post = Post.query.filter(Post.id == post_id).first_or_404()

    # can attach images to own post only
    if post.user_id != auth_user_id():
        abort(403)

    # attach images
    data = request.get_json()
    image_ids = data.get('image_ids')
    images = get_images_list_using_ids(image_ids)

    try:
        post.images = images
        post.update()
        return jsonify({"success": True, "post": post.format()})
    except Exception as e:
        abort(400)
示例#7
0
def update_comments(comment_id):
    """ Update comments

        Parameters:
            comment_id (int): Id of comment

        Patch data:
            content (string): Content for the comment

        Returns:
            success (boolean)
            comments (list)
            total_comments (int)
    """
    # vars
    data = request.get_json()

    validate_comment_data(data)

    # get comment
    comment = Comment.query.filter(Comment.id == comment_id).first_or_404()

    # can update own comment only
    if comment.user_id != auth_user_id():
        abort(403)

    # update comment
    comment.content = data.get('content')

    try:
        comment.update()
        return jsonify({"success": True, "comment": comment.format()})
    except Exception as e:
        abort(400)
示例#8
0
def update_profile():
    """ Update profile

    Patch data:
        first_name (string)
        last_name (string)
        phone_number (string)
        password (string)
        confirm_password (string)
        profile_picture (string)
        cover_picture (string)

    Returns:
        success (boolean)
        user (dict)
    """
    data = validate_profile_data(request.get_json())

    # get user
    user = User.query.filter(User.id == auth_user_id()).first_or_404()

    try:
        user.query.update(data)
        user.update()
        return jsonify({
            "success": True,
            "user": user.format()
        })
    except Exception as e:
        abort(400)
示例#9
0
def reply_comments(comment_id):
    """ Reply comments

        Parameters:
            comment_id (int): Id of comment

        Post Data:
            content (string): reply content

        Returns:
            success (boolean)
            comment: (dict)
    """
    # vars
    data = request.get_json()
    validate_comment_data(data)

    # get comment
    comment = Comment.query.filter(Comment.id == comment_id).first_or_404()

    try:
        # reply comment
        reply = Comment(
            **{
                'content': data.get('content'),
                'parent_id':
                comment.parent_id if comment.parent_id else comment.id,
                'user_id': auth_user_id(),
                'post_id': comment.post_id
            })
        reply.insert()

        return jsonify({"success": True, "comment": reply.format()})
    except Exception as e:
        abort(400)
示例#10
0
def create_posts():
    """ Create new posts

        Post data:
            content (string): Content for the post
            image_ids (list|optional): Ids of uploaded images

        Returns:
            success (boolean)
            post (list)
    """
    # vars
    data = request.get_json()

    validate_post_data(data)

    # create post
    post = Post(**{'content': data.get('content'), 'user_id': auth_user_id()})

    try:
        post.insert()

        # attach images
        image_ids = data.get('image_ids')
        if image_ids:
            images = get_images_list_using_ids(image_ids)
            post.images = images
            post.update()

        return jsonify({"success": True, "post": post.format()})
    except Exception as e:
        abort(400)
示例#11
0
def filter_images(count_only=False):
    query = Image.query

    # Filter current user's images
    query = query.filter(Image.user_id == auth_user_id())

    # return filtered data
    return filter_model(Image, query, count_only=count_only)
示例#12
0
def get_images_list_using_ids(image_ids):
    images = []
    for image_id in image_ids:
        image = Image.query.filter(
            Image.id == image_id,
            Image.user_id == auth_user_id()).first_or_404()

        images.append(image)

    return images
示例#13
0
def get_profile():
    """ Get auth user info

    Returns:
        success (boolean)
        user (dict)
    """
    user = User.query.filter(User.id == auth_user_id()).first_or_404()
    return jsonify({
        "success": True,
        "user": user.format()
    })
示例#14
0
def update_posts(post_id):
    """ Update posts

        Parameters:
            post_id (int): Id of post

        Patch data:
            content (string|optional): Content for the post
            image_ids (list|optional):
                Internal parameter extracted from current_user

        Returns:
            success (boolean)
            posts (list)
            total_posts (int)
    """
    # vars
    data = request.get_json()

    # get post
    post = Post.query.filter(Post.id == post_id).first_or_404()

    # can update own post only
    if post.user_id != auth_user_id():
        abort(403)

    try:
        # update post
        content = data.get('content')
        if content:
            post.content = data.get('content')
            post.update()

        # update images
        image_ids = data.get('image_ids')
        if image_ids:
            # delete images
            for image in post.images:
                image.delete()

            # attach images
            images = get_images_list_using_ids(image_ids)
            post.images = images
            post.update()

        return jsonify({"success": True, "post": post.format()})
    except Exception as e:
        abort(400)
示例#15
0
def get_post(post_id):
    """ Get a single post

        Parameters:
            post_id (int): Id of post

        Returns:
            success (boolean)
            post (dict)
    """
    # get post
    post = Post.query.filter(Post.id == post_id).first_or_404()

    # can update own post only
    if post.user_id != auth_user_id():
        abort(403)

    try:
        return jsonify({"success": True, "post": post.format()})
    except Exception as e:
        abort(400)
示例#16
0
def get_image(image_id):
    """ Update images

        Parameters:
            image_id (int): Id of image

        Returns:
            success (boolean)
            image (dist)
    """
    # get image
    image = Image.query.filter(Image.id == image_id).first_or_404()

    # can retrieve own image only
    if image.user_id != auth_user_id():
        abort(403)

    try:
        # return the result
        return jsonify({'success': True, 'image': image.format()})
    except Exception as e:
        abort(400)
示例#17
0
def delete_comments(comment_id):
    """ Delete comments

        Parameters:
            comment_id (int): Id of comment

        Returns:
            success (boolean)
            deleted_id (int)
    """
    # vars
    comment = Comment.query.filter(Comment.id == comment_id).first_or_404()

    # can delete own comment only
    if comment.user_id != auth_user_id():
        abort(403)

    try:
        comment.delete()
        return jsonify({"success": True, "deleted_id": comment.id})
    except Exception as e:
        abort(400)
示例#18
0
def delete_images(image_id):
    """ Delete images

        Parameters:
            image_id (int): Id of image

        Returns:
            success (boolean)
            images: (list)
            delete_id (int)
    """
    # vars
    image = Image.query.filter(Image.id == image_id).first_or_404()

    # can delete own image only
    if image.user_id != auth_user_id():
        abort(403)

    try:
        image.delete()
        return jsonify({"success": True, "deleted_id": image.id})
    except Exception as e:
        abort(400)
示例#19
0
def timeline():
    """ Get all posts by auth user

    Returns:
        success (boolean)
        posts (list)
        total (int)
        query_args (dic)
    """
    try:
        query = Post.query.filter(Post.user_id == auth_user_id())
        posts = filter_model(Post, query, count_only=False)
        total = filter_model(Post, query, count_only=True)
        return jsonify({
            'success': True,
            'posts': [
                post.format() for post in posts
            ],
            'total': total,
            'query_args': request.args,
        })
    except Exception as e:
        abort(400)