Exemplo n.º 1
0
def create_posts(quantity, users_list=None):
    users = users_list or list(User.select())
    for _ in range(quantity):
        Post.from_dict({
            'title': t.title(),
            'text': t.text(),
            'author': choice(users),
        })
Exemplo n.º 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
Exemplo n.º 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
Exemplo n.º 4
0
    def test_deleting_user(self):
        utils.create_users(10)
        code, msg = utils.delete(
            self.app,
            f'{self.link}/user/3',
            self.headers
        )
        self.assertAlmostEqual(code, 200)
        self.assertAlmostEqual(User.select().count(), 10)

        code, msg = utils.delete(
            self.app,
            f'{self.link}/user/23',
            self.headers
        )
        self.assertAlmostEqual(code, 404)
        self.assertAlmostEqual(msg['error'], 'User does not exist.')

        utils.create_posts(50)
        seventh_user = User.get_by_id(7)
        self.assertTrue(seventh_user.posts.count() > 0)

        data = {'delete_posts': True}
        code, msg = utils.delete(
            self.app,
            f'{self.link}/user/7',
            self.headers,
            'application/json',
            data
        )
        self.assertAlmostEqual(code, 200)

        num_of_posts = Post.select().where(Post.author == seventh_user).count()
        self.assertAlmostEqual(num_of_posts, 0)
Exemplo n.º 5
0
def add_post(current_user):
    data = request.get_json()
    if None in (data.get('title'), data.get('text')):
        return error('Both title and text are required.', 403)
    data['author'] = current_user
    post = Post.from_dict(data)
    return jsonify(post.to_dict()), 201
Exemplo n.º 6
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
Exemplo n.º 7
0
def get_others_posts(current_user):
    posts = [
        post.to_dict()
        for post in Post.select().where(Post.author != current_user)
    ]
    if not posts:
        return error('No posts from other users.', 404)
    return jsonify(posts), 200
Exemplo n.º 8
0
    def test_deleting_users_post(self):
        utils.create_users(3)
        utils.create_posts(50)
        post_id = User.get_by_id(2).posts.first().get_id()
        code, _ = utils.delete(
            self.app,
            f'{self.link}/user/2/post/70',
            self.headers
        )
        self.assertAlmostEqual(code, 404)

        code, post = utils.delete(
            self.app,
            f'{self.link}/user/2/post/{post_id}',
            self.headers
        )
        self.assertAlmostEqual(code, 200)
        self.assertIs(None, Post.select().where(Post.id == post_id).first())
Exemplo n.º 9
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)
Exemplo n.º 10
0
 def test_delete_post(self):
     code, _ = utils.delete(self.app, f'{self.link}/:15', self.headers)
     self.assertAlmostEqual(code, 404)
     code, _ = utils.delete(self.app, f'{self.link}/:1', self.headers)
     self.assertAlmostEqual(code, 200)
     self.assertAlmostEqual(Post.select().count(), 0)
Exemplo n.º 11
0
def delete_post(post_id):
    if not post_exists(post_id):
        return error('Post does not exist.', 404)
    Post.delete_by_id(post_id)
    return message('Deleted.', 200)