Пример #1
0
    def find_post_by_author(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

        start = (page - 1) * per_page   # id in sql start at 1
        post_list = DBPost.get(filter_dict=args, start=start, per_page=per_page, order_by="time desc")
        return post_list, author
Пример #2
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