Example #1
0
    def find_post_by_author_pagination(cls, author_id, page=1, per_page=10):
        """
        Find all post publish by specific author

        :param author_id: id of author to find post by
        :param page: page index begin at 1
        :param per_page:
        :return:
        """

        # valid user if
        if not is_id_valid(author_id):
            raise InvalidFieldError("author id does not valid.", ["author_id"])

        # confirm user existent
        author = DBUser.get_by_id(author_id)
        if not author:
            raise UserNotFoundError("User with id = %d does not exist")

        args = {"user_id": author_id}

        # validate pagination info
        if not is_id_valid(page):
            page = 1

        if int(per_page) <= 0 or int(per_page) >= 50:
            per_page = 10

        pagination = DBPost.pagination_get(filter_dict=args, page=page, per_page=per_page, order_by="time desc")
        return pagination, author
Example #2
0
    def delete_comment(cls, user_id, comment_id):
        # not necessary to check user_id
        # if not is_id_valid(user_id):
        #     raise InvalidFieldError("user id is invalid", ["user_id"])

        if not is_id_valid(comment_id):
            raise InvalidFieldError("comment id is invalid", ["comment_id"])

        user = DBUser.get_by_id(user_id)
        if not user:
            raise UserNotFoundError("User with id = %d  does not exist" % user_id)

        comment = DBComment.get_by_id(comment_id)
        if not comment:
            raise CommentNotFoundError(comment_id=comment_id)

        # only allow commenter/post author to delete comment
        if comment.user_id != user_id and comment.post.author.id != user_id:
            raise AccessDeniedError("You cannot delete others comment")

        try:
            comment.delete()
            return comment
        except:
            raise
Example #3
0
    def update_comment(cls, user_id, comment_id, content):

        # not necessary to check user_id
        # if not is_id_valid(user_id):
        #     raise InvalidFieldError("user id is invalid", ["user_id"])

        if not is_id_valid(comment_id):
            raise InvalidFieldError("comment id is invalid", ["comment_id"])

        commenter = DBUser.get_by_id(user_id)
        if not commenter:
            raise UserNotFoundError("User with id = %d  does not exist" % user_id)

        comment = DBComment.get_by_id(comment_id)
        if not comment:
            raise CommentNotFoundError(comment_id=comment_id)

        if len(content) < 10:
            raise InvalidFieldError("comment is too short", ["content"])

        # only allow commenter to update comment
        if comment.user_id != user_id:
            raise AccessDeniedError("You cannot edit others comment")

        comment.content = content
        try:
            comment.update()
            return comment
        except:
            raise
Example #4
0
    def find_post_by_category(cls, category_name, page=1, per_page=10):
        """
        Find all post publish by specific author

        :param category_name: category name
        :param page: page index begin at 1
        :param per_page:
        :return:
        """

        if not category_name:
            raise InvalidFieldError("category cannot be empty", ["category"])
        cat_slug = get_slug_from_string(category_name)
        args = {"categories": "`%s`" % cat_slug}

        # validate pagination info
        if not is_id_valid(page):
            page = 1

        if int(per_page) <= 0 or int(per_page) >= 50:
            per_page = 10

        start = (page - 1) * per_page  # id in sql start at 1
        post_list = DBPost.search(search_dict=args, start=start, per_page=per_page, order_by="time desc")
        return post_list
Example #5
0
    def find_post_by_keyword_pagination(cls, keyword, page=1, per_page=10):
        """
        Find all post publish by specific author

        :param category_name: category name
        :param page: page index begin at 1
        :param per_page:
        :return:
        """

        if not keyword:
            raise InvalidFieldError("category cannot be empty", ["search string"])

        args = {
            "title": "%s" % keyword,
            "content": "%s" % keyword,
        }

        # validate pagination info
        if not is_id_valid(page):
            page = 1
        else:
            page = int(page)

        if int(per_page) <= 0 or int(per_page) >= 50:
            per_page = 10

        try:
            start = (page - 1) * per_page   # id in sql start at 1
            post_list = DBPost.pagination_search(search_dict=args, page=page, per_page=per_page, order_by="time desc")
            return post_list
        except Exception as e:
            raise InvalidFieldError("Can not search with given string", ["search string"])
Example #6
0
    def add_comment(cls, post_id, user_id, content):
        if not is_id_valid(post_id):
            raise InvalidFieldError("post id is invalid", ["post_id"])

        # not need to check user id for logged in user
        # if not is_id_valid(user_id):
        #     raise InvalidFieldError("user id is invalid", ["user_id"])

        post = DBPost.get_by_id(post_id)
        if not post:
            raise PostNotFoundError(post_id=post_id)

        if len(content) < 10:
            raise InvalidFieldError("comment is too short", ["content"])

        args = {
            "post_id": post_id,
            "user_id": user_id,
            "content": content
        }
        comment = DBComment(**args)
        try:
            comment.save()
            return comment
        except:
            return None
Example #7
0
    def get_post(cls, post_id):
        if not is_id_valid(post_id):
            raise InvalidFieldError("Post id is invalid", ["post_id"])
        post = DBPost.get_by_id(post_id)

        # if post:
        #     comments = post.comments.all()
        #
        #     post.comments = comments
        return post
Example #8
0
    def get_posts_pagination(cls, page=1, per_page=10):
        """
        Get many post at a time, order by post time
        :param page: page index begin at 1
        :param per_page:
        :return:
        """
        if not is_id_valid(page):
            page = 1

        if int(per_page) <= 0 or int(per_page) >= 50:
            per_page = 10

        pagination = DBPost.pagination_get(page=page, per_page=per_page, order_by="time desc")
        return pagination
Example #9
0
    def get_posts(cls, page=1, per_page=10):
        """
        Get many post at a time, order by post time
        :param page: page index begin at 1
        :param per_page:
        :return:
        """
        if not is_id_valid(page):
            page = 1

        if int(per_page) <= 0 or int(per_page) >= 50:
            per_page = 10

        start = (page - 1) * per_page   # id in sql start at 1
        post_list = DBPost.get(start=start, per_page=per_page, order_by="time desc")
        return post_list
Example #10
0
    def delete_post(cls, user_id, post_id):
        user = DBUser.get_by_id(user_id)
        if not user:
            raise UserNotFoundError("User with id = %d does not exist" % user_id)

        if not is_id_valid(post_id):
            raise InvalidFieldError("Post id is invalid", ["post_id"])

        post = DBPost.get_by_id(post_id)
        if not post:
            raise PostNotFoundError(post_id=post_id)

        # only allow author and manager to delete post
        if post.author.id != user_id and user.role != "manager":
            raise AccessDeniedError("You don't have permission to delete this post.")

        post.delete()
Example #11
0
    def update_post(cls, user_id, post_id, title=None, content=None, feature_image=None, tags=None, categories=None,
                    draft=False):
        # only allow author or manager to edit post
        user = DBUser.get_by_id(user_id)
        if not user:
            raise UserNotFoundError("User with id = %d does not exist" % user_id)

        if not is_id_valid(post_id):
            raise InvalidFieldError("Post id is invalid", ["post_id"])

        post = DBPost.get_by_id(post_id)
        if not post:
            raise PostNotFoundError(post_id=post_id)

        if post.author.id != user_id and user.role != "manager":
            raise AccessDeniedError("You cannot edit post not published by you.")

        if title:
            post.title = title

        if content:
            post.content = content
        elif content is not None and len(content) == 0:
            raise InvalidFieldError("Post's content cannot be empty", ["content"])

        if feature_image:
            post.feature_image = feature_image

        if tags:
            post.tags = tags

        if categories:
            post.categories = ",".join("`%s`"%cat for cat in categories)

        post.update()
        return post