Exemplo n.º 1
0
    def add_post(cls, user_id, title, content, feature_image=None, tags=None, categories=None, draft=False):

        # not need to check author existent

        # if title is empty, use curent date as title
        if len(title) == 0:
            time = datetime.now()
            title = time.strftime("%A %d %B %Y")

        if not content:
            raise InvalidFieldError("Post's content could not be empty", ["content"])

        if not categories:
            categories = [default.DEFAULT_CATEGORY]

        args = {
            "user_id": user_id,
            "title": title,
            "content": content,
            "feature_image": feature_image,
            "tags": tags,
            "categories": categories,
            "draft": draft
        }

        post = DBPost(**args)
        try:
            post.save()
            return post
        except:
            raise
Exemplo n.º 2
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
Exemplo n.º 3
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
Exemplo n.º 4
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"])
Exemplo n.º 5
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
Exemplo n.º 6
0
    def test_delete_post_by_manager(self):
        args = {
            "user_id": 1,
            "post_id": 2
        }

        Post.delete_post(**args)
        post = DBPost.get_by_id(2)
        self.assertIsNone(post)
Exemplo n.º 7
0
    def test_delete_post_all_field_valid(self):
        args = {
            "user_id": 1,
            "post_id": 1
        }

        Post.delete_post(**args)
        post = DBPost.get_by_id(1)
        self.assertIsNone(post)
Exemplo n.º 8
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
Exemplo n.º 9
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
Exemplo n.º 10
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
Exemplo n.º 11
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()
Exemplo n.º 12
0
    def test_add_post_without_title(self):
        args ={
            "user_id": 2,
            "title": "",
            "content": "<p>Sed itaque dignissimos eligendi reprehenderit, nesciunt ducimus voluptates dolores suscipit fugit ipsam aperiam praesentium laborum odit qui libero ipsum tempora, eos quis hic, sapiente perspiciatis amet labore voluptatibus alias. Vitae.</p>",
            "feature_image":"http://www.keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image1.jpg",
            "tags": "wallpaper, nature",
            "categories":"Uncategorized",
            "draft": True,
        }

        p = Post.add_post(**args)
        self.assertIsNotNone(p)

        post = DBPost.get_by_id(p.id)
        self.assertEqual(post.user_id, args["user_id"])
        self.assertGreater(len(post.title), 0)
        self.assertEqual(post.content, args["content"])
        self.assertEqual(post.feature_image, args["feature_image"])
        self.assertEqual(post.tags, args["tags"])
        self.assertEqual(post.categories, args["categories"])
Exemplo n.º 13
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
Exemplo n.º 14
0
def set_up_db():
    args = {
        "email": "*****@*****.**",
        "password": hashlib.md5("123456").hexdigest(),
        "first_name": "Admin",
        "last_name": "Nguyen",
        "brief": "Hello world",
        "role": "manager"
    }
    user = User(**args)
    user.save()

    args["email"] = "*****@*****.**"
    args["first_name"] = "Editor2"
    args["role"] = 'editor'
    user = User(**args)
    user.save()

    args["email"] = "*****@*****.**"
    args["first_name"] = "Editor3"
    user = User(**args)
    user.save()

    args["email"] = "*****@*****.**"
    args["first_name"] = "Editor4"
    user = User(**args)
    user.save()

    args = {
        "title": "Post 1 title",
        "content": """<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eveniet doloribus qui, adipisci inventore sequi fugiat dolores ullam, provident a, accusantium, necessitatibus ab nisi aliquam. Ipsam voluptas dolores magni necessitatibus provident.</p>
        <p>Sunt quo placeat fugiat nesciunt vel assumenda dolorem incidunt provident eligendi ipsa, quam autem optio id nostrum beatae corporis a. Tempore saepe quod nemo hic magni in veritatis illum natus.</p>
        <p>Et beatae ipsam repellat officiis similique cupiditate distinctio expedita rem at, aut aspernatur, voluptate quibusdam! Voluptatum aut quos porro eos nulla officiis adipisci magnam perferendis, dicta minima quis eligendi enim.</p>
        <p>Sed itaque dignissimos eligendi reprehenderit, nesciunt ducimus voluptates dolores suscipit fugit ipsam aperiam praesentium laborum odit qui libero ipsum tempora, eos quis hic, sapiente perspiciatis amet labore voluptatibus alias. Vitae.</p>""",
        "user_id": 1,
        "categories": ["c++","python"]
    }

    post = Post(**args)
    post.save()

    args["title"] = "Post 2 title"
    args["user_id"] = 2

    post = Post(**args)
    post.save()

    args["title"] = "Post 3 title"
    args["user_id"] = 3

    post = Post(**args)
    post.save()

    args["title"] = "Post 4 title"
    args["user_id"] = 4

    post = Post(**args)
    post.save()

    args["title"] = "Post Hello title"
    args["user_id"] = 1
    args["categories"] = ["non-it"]

    post = Post(**args)
    post.save()

    args["title"] = "Good morning"
    args["user_id"] = 2

    post = Post(**args)
    post.save()

    args["title"] = "First day at Moscow"
    args["user_id"] = 2
    args["categories"] = ["journey","non-it"]

    post = Post(**args)
    post.save()

    args["title"] = "Surprising"
    args["user_id"] = 3

    post = Post(**args)
    post.save()

    args["title"] = "So awesome lake"
    args["user_id"] = 3

    post = Post(**args)
    post.save()

    args["title"] = "My new Phone"
    args["user_id"] = 3
    args["categories"] = ["photo","non-it"]

    post = Post(**args)
    post.save()

    args["title"] = "Photo with new phone"
    args["user_id"] = 3

    post = Post(**args)
    post.save()

    args["title"] = "List of useful app for Blackberry"
    args["user_id"] = 3
    args["categories"] = ["uncategorized"]

    post = Post(**args)
    post.save()


    cmt_args={
        "content":"<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut ipsum ad, mollitia repellendus harum dignissimos rem beatae, dolore minus. Sapiente saepe mollitia magnam molestiae natus officiis corrupti voluptatibus, qui repudiandae.</p>",
        "post_id":1,
        "user_id":2,
    }
    cmt = Comment(**cmt_args)
    cmt.save()


    cmt_args["post_id"] = 1
    cmt_args["user_id"] = 3
    cmt = Comment(**cmt_args)
    cmt.save()

    cmt_args["post_id"] = 1
    cmt_args["user_id"] = 4
    cmt = Comment(**cmt_args)
    cmt.save()

    cmt_args["post_id"] = 3
    cmt_args["user_id"] = 4
    cmt = Comment(**cmt_args)
    cmt.save()

    cmt_args["post_id"] = 3
    cmt_args["user_id"] = 2
    cmt = Comment(**cmt_args)
    cmt.save()
    cmt_args["post_id"] = 4
    cmt_args["user_id"] = 1
    cmt = Comment(**cmt_args)
    cmt.save()
    cmt_args["post_id"] = 7
    cmt_args["user_id"] = 1
    cmt = Comment(**cmt_args)
    cmt.save()
    cmt_args["post_id"] = 4
    cmt_args["user_id"] = 2
    cmt = Comment(**cmt_args)
    cmt.save()

    cmt_args["post_id"] = 3
    cmt_args["user_id"] = 3
    cmt = Comment(**cmt_args)
    cmt.save()

    cmt_args["post_id"] = 2
    cmt_args["user_id"] = 4
    cmt = Comment(**cmt_args)
    cmt.save()

    cmt_args["post_id"] = 2
    cmt_args["user_id"] = 1
    cmt = Comment(**cmt_args)
    cmt.save()

    cmt_args["post_id"] = 2
    cmt_args["user_id"] = 3
    cmt = Comment(**cmt_args)
    cmt.save()

    cmt_args["post_id"] = 2
    cmt_args["user_id"] = 4
    cmt = Comment(**cmt_args)
    cmt.save()

    cmt_args["post_id"] = 5
    cmt_args["user_id"] = 3
    cmt = Comment(**cmt_args)
    cmt.save()

    cmt_args["post_id"] = 5
    cmt_args["user_id"] = 2
    cmt = Comment(**cmt_args)
    cmt.save()

    cmt_args["post_id"] = 5
    cmt_args["user_id"] = 3
    cmt = Comment(**cmt_args)
    cmt.save()

    cmt_args["post_id"] = 5
    cmt_args["user_id"] = 4
    cmt = Comment(**cmt_args)
    cmt.save()

    cmt_args["post_id"] = 6
    cmt_args["user_id"] = 4
    cmt = Comment(**cmt_args)
    cmt.save()

    cmt_args["post_id"] = 6
    cmt_args["user_id"] = 1
    cmt = Comment(**cmt_args)
    cmt.save()

    cmt_args["post_id"] = 9
    cmt_args["user_id"] = 1
    cmt = Comment(**cmt_args)
    cmt.save()

    cmt_args["post_id"] = 9
    cmt_args["user_id"] = 1
    cmt = Comment(**cmt_args)
    cmt.save()

    cmt_args["post_id"] = 9
    cmt_args["user_id"] = 3
    cmt = Comment(**cmt_args)
    cmt.save()