def post(self):
        parser = reqparse.RequestParser()

        parser.add_argument('title', required=True)
        parser.add_argument('image', required=True)
        parser.add_argument('abstract', required=True)
        parser.add_argument('text', required=True)
        parser.add_argument('is_draft', type=bool)

        args = parser.parse_args(strict=True)

        post_content = PostContent()
        post_content.text = args.get('text')
        post_content.save()

        post = Post()
        post.title = args.get('title')
        post.image = args.get('image')
        post.abstract = args.get('abstract')
        post.post_content_id = post_content.id
        post.is_draft = False
        post.save()

        return {
            **post.serialize(),
            'text': post_content.text,
        }
Esempio n. 2
0
    def get(self):
        parser = reqparse.RequestParser()
        parser.add_argument(
            'p',
            type=int,
            dest='page',
            help='Page must be a positive integer',
            required=True,
        )

        args = parser.parse_args()

        posts = Post.paginate(
            self.per_page,
            args.get('page'),
        )

        return {
            'total': posts.total,
            'per_page': posts.per_page,
            'current_page': posts.current_page,
            'last_page': posts.last_page,
            'previous_page': posts.previous_page,
            'next_page': posts.next_page,
            'data': json.loads(posts.to_json()),
        }
    def put(self, post_id=None):
        parser = reqparse.RequestParser()

        parser.add_argument('title', store_missing=False)
        parser.add_argument('image', store_missing=False)
        parser.add_argument('abstract', store_missing=False)
        parser.add_argument('text', store_missing=False)
        parser.add_argument('is_draft', type=bool, store_missing=False)

        args = parser.parse_args(strict=True)

        post = Post.find(post_id)

        if not post:
            abort(HTTPStatus.NOT_FOUND)

        post_content = PostContent.find(post.post_content_id)

        if 'text' in args:
            post_content.text = args.get('text')
            post_content.save()

            args.pop('text', None)

        for key in args:
            setattr(post, key, args.get(key))

        post.save()

        return {
            **post.serialize(),
            'text': post_content.text,
        }
def test_blog_posts_update_partial_post(client):
    post_content = factory(PostContent).create()
    post = factory(Post).create(post_content_id=post_content.id)

    params = {
        'title': 'Novo título',
    }

    response = client.put(
        f'/posts/{post.id}',
        json=params,
        headers={
            'Authorization': 'Bearer valid_token',
        },
    )

    updated_post = Post.find(post.id).serialize()
    updated_post.pop('updated_at')
    updated_title = updated_post.pop('title')

    old_post = post.serialize()
    old_post.pop('updated_at')
    old_title = old_post.pop('title')

    assert updated_title == params['title']
    assert old_post == updated_post
    assert response.get_json()['title'] == params['title']
def test_blog_posts_update_full_post(client):
    post_content = factory(PostContent).create()
    post = factory(Post).create(post_content_id=post_content.id)

    new_data = factory(Post).make()
    params = {
        'title': new_data.title,
        'image': new_data.image,
        'abstract': new_data.abstract,
        'text': 'test_text',
    }

    response = client.put(
        f'/posts/{post.id}',
        json=params,
        headers={
            'Authorization': 'Bearer valid_token',
        },
    )

    params_copy = params.copy()
    params_copy.pop('text')

    assert response.status_code == HTTPStatus.OK
    assert response.get_json().items() >= params.items()
    assert Post.find(post.id).serialize().items() >= params_copy.items()
    assert PostContent.find(post_content.id).text == params.get('text')
def test_blog_posts_create_successfully(client):
    data = factory(Post).make().serialize()

    data.pop('post_content_id')
    data.update({
        'text': 'test_text',
    })

    response = client.post(
        '/posts',
        json=data,
        headers={
            'Authorization': 'Bearer valid_token',
        },
    )

    data = response.get_json()
    saved_post = Post.find(data['id'])
    saved_content = PostContent.find(data['post_content_id'])

    assert response.status_code == HTTPStatus.OK
    assert bool(saved_post)
    assert bool(saved_content)
    assert data.items() >= saved_post.serialize().items()
    assert data['text'] == saved_content.text
    def delete(self, post_id=None):
        post = Post.find(post_id)

        if not post:
            abort(HTTPStatus.NOT_FOUND)

        post_content = PostContent.find(post.post_content_id)

        if post_content:
            post_content.delete()

        post.delete()

        return {
            'message': 'Successfully deleted',
        }
Esempio n. 8
0
def test_blog_posts_delete_successfully(client):
    post_content = factory(PostContent).create()
    post = factory(Post).create(post_content_id=post_content.id)

    response = client.delete(
        f'/posts/{post.id}',
        headers={
            'Authorization': 'Bearer valid_token',
        },
    )

    data = response.get_json()

    assert 'message' in data
    assert data.get('message') == 'Successfully deleted'
    assert not Post.find(post.id)
    assert not PostContent.find(post_content.id)
    def get(self, post_id=None):
        if post_id:
            post = Post.find(post_id)

            if not post:
                abort(HTTPStatus.NOT_FOUND)

            response = post.serialize()
            response.update({
                'text': post.post_content.text,
            })

            return response
        else:
            parser = reqparse.RequestParser()
            parser.add_argument(
                'p',
                type=int,
                dest='page',
                help='Page must be a positive integer',
                required=True,
            )

            args = parser.parse_args()

            posts = (Post
                .where('is_draft', False)
                .paginate(
                    self.per_page,
                    args.get('page'),
                )
            )

            return {
                'total': posts.total,
                'per_page': posts.per_page,
                'current_page': posts.current_page,
                'last_page': posts.last_page,
                'previous_page': posts.previous_page,
                'next_page': posts.next_page,
                'data': json.loads(posts.to_json()),
            }