Пример #1
0
    def put(self, author_id):
        try:
            request_data = json.loads(request.data)
        except ValueError:
            return 'Request is not valid JSON', 400

        if set(request_data.keys()) - {'description', 'name'}:
            # There is a parameter differ then {'description', 'name'}
            error_msg = 'Unexpected params: %s' % \
                ', '.join(set(request_data.keys()) - {'description', 'name'})
            return error_msg, 400

        form = AuthorUpdateForm.from_json(request_data)

        if not form.validate():
            return 'Invalid request data', 400

        author = AuthorModel.query.filter_by(id=author_id).first()

        if author is None:
            error_msg = 'User %d doest\'t exist' % int(author_id)
            return error_msg, 404

        if 'name' in request_data:
            author.name = request_data['name']

        if 'description' in request_data:
            author.description = request_data['description']

        db_session.add(author)

        db_session.commit()
        return 'Ok', 200
Пример #2
0
    def post(self):
        try:
            request_data = json.loads(request.data)
        except ValueError:
            return 'Request body is not a valid JSON', 400

        if len(request_data) != 3:
            return 'Invalid request data', 400

        form = BookCreateForm.from_json(request_data)

        if not form.validate():
            return 'Invalid request data', 400

        book = BookModel(name=request_data['name'],
                         description=request_data['description'])
        db_session.add(book)

        for author_id in request_data['authors']:
            author = AuthorModel.query.filter_by(id=author_id).first()

            if author is None:
                msg = 'Author %d doesn\'t exist' % author_id
                db_session.rollback()
                return msg, 404
            book.authors.append(author)

        db_session.commit()

        return str(book.id), 200
Пример #3
0
    def add_book(self, test_name, authors=[]):
        book = BookModel(name=test_name, description=test_name)
        db_session.add(book)
        db_session.commit()

        for author in authors:
            book.authors.append(author)

        return book
Пример #4
0
    def delete(self, book_id):
        book = BookModel.query.filter_by(id=book_id)

        if book.first() is None:
            return 'Book not found', 404

        book.delete()

        db_session.commit()

        return 'Ok', 200
Пример #5
0
    def delete(self, author_id):
        author = AuthorModel.query.filter_by(id=author_id)

        if author.first() is None:
            error_msg = 'User %d doest\'t exist' % int(author_id)
            return error_msg, 404

        author.delete()

        db_session.commit()

        return 'Ok', 200
Пример #6
0
    def post(self):
        try:
            request_data = json.loads(request.data)
        except ValueError:
            return 'Invalid request body', 400

        form = AuthorCreateForm.from_json(request_data)

        if not form.validate():
            return 'Invalid request', 400

        author = AuthorModel(name=request_data['name'],
                             description=request_data['description'])
        db_session.add(author)
        db_session.commit()

        return str(author.id), 200
Пример #7
0
    def put(self, book_id):
        try:
            request_data = json.loads(request.data)
        except ValueError:
            return 'Request body is not a valid JSON', 400

        if not request_data or (set(request_data.keys()) -
                                {'name', 'description', 'authors'}):
            return 'Invalid request data', 400

        form = BookUpdateForm.from_json(request_data)

        if not form.validate():
            return 'Invalid request data', 400

        new_authors = []
        if 'authors' in request_data:
            for author_id in request_data['authors']:
                new_author = AuthorModel.query.filter_by(id=author_id).first()
                if new_author is None:
                    msg = 'Author with id = %d is noe existing' % author_id
                    return msg, 404

                new_authors.append(new_author)

        book = BookModel.query.filter_by(id=book_id).first()

        if book is None:
            msg = 'Book with id=%d doesn\'t exist' % book_id
            return msg, 404

        db_session.add(book)

        if 'name' in request_data:
            book.name = request_data['name']

        if 'description' in request_data:
            book.description = request_data['description']

        if 'authors' in request_data:
            book.authors = new_authors

        db_session.commit()

        return 'Ok', 200
Пример #8
0
    def test_005_put_single_field(self):
        author = self.add_author('test_005_put_single_field')
        author_id = author.id

        # update name
        url = '/authors/{id:d}/'.format(id=author.id)
        data = {
            'name': 'newname005',
        }
        response = self.app.put(url,
                                data=json.dumps(data),
                                follow_redirects=True)

        author = AuthorModel.query.filter_by(id=author_id).first()

        author_name = author.name
        author_description = author.description
        self.assertEqual('newname005', author_name)
        self.assertEqual('test_005_put_single_field', author_description)

        author.name = 'test_005_put_single_field'
        db_session.add(author)
        db_session.commit()

        # update description
        data = {
            'description': 'newdescription005',
        }
        response = self.app.put(url,
                                data=json.dumps(data),
                                follow_redirects=True)

        author = AuthorModel.query.filter_by(id=author_id).first()
        author_name = author.name
        author_description = author.description
        self.assertEqual('test_005_put_single_field', author_name)
        self.assertEqual('newdescription005', author_description)
Пример #9
0
    def add_author(self, test_name):
        author = AuthorModel(name=test_name, description=test_name)
        db_session.add(author)
        db_session.commit()

        return author