示例#1
0
def post_exists(post_id, return_post=False):
    try:
        post = Post.get_by_id(post_id)
    except Post.DoesNotExist:
        return False
    if return_post:
        return post
    return True
示例#2
0
def edit_post(post_id):
    if not post_exists(post_id):
        return error('Post does not exist.', 404)
    data = request.get_json()
    post_data = {
        field: data[field]
        for field in Post._meta.allowed_fields if data.get(field) is not None
    }
    Post.update(post_data).where(Post.id == post_id).execute()
    return jsonify(Post.get_by_id(post_id).to_dict()), 200
示例#3
0
def edit_users_post(user_id, post_id):
    user = user_exists(user_id, return_user=True)
    if not user:
        return error('User does not exist.', 404)
    if user.posts.where(Post.id == post_id).first() is None:
        return error('Selected user does not have the post.', 404)
    data = request.get_json()
    post_data = {field: data[field] for field in Post._meta.allowed_fields
                 if data.get(field) is not None}
    Post.update(post_data).where(Post.id == post_id).execute()
    post = Post.get_by_id(post_id)
    return jsonify(post.to_dict()), 200
示例#4
0
    def test_getting_users_posts(self):
        utils.create_users(10)
        utils.create_posts(50)
        code, _ = utils.get(
            self.app,
            f'{self.link}/user/40/posts',
            self.headers
        )
        self.assertAlmostEqual(code, 404)

        code, posts = utils.get(
            self.app,
            f'{self.link}/user/7/posts',
            self.headers
        )
        self.assertAlmostEqual(code, 200)
        self.assertTrue(len(posts) > 0)

        code, _ = utils.get(
            self.app,
            f'{self.link}/user/40/post/100', self.headers
        )
        self.assertAlmostEqual(code, 404)

        post_id = User.get_by_id(2).posts.first().get_id()
        code, post = utils.get(
            self.app,
            f'{self.link}/user/2/post/{post_id}',
            self.headers
        )
        self.assertAlmostEqual(code, 200)
        self.assertDictEqual(post, Post.get_by_id(post_id).to_dict())

        code, _ = utils.get(
            self.app,
            f'{self.link}/user/2/post/70',
            self.headers
        )
        self.assertAlmostEqual(code, 404)