Esempio n. 1
0
    def patch(self, relatedBookUuid):  # type: ignore
        result: Optional[RelatedBook] = RelatedBook.query.filter_by(related_book_uuid=relatedBookUuid)\
            .options(noload('*')).first()  # noqa: E501

        if result is None:
            abort(404)

        data = request.get_json(force=True)
        if not isinstance(data, dict):
            abort(400)

        marshmallow_schema_or_errors = convert_dict_to_marshmallow_result(
            data=json_dict_to_python_dict(model_to_dict(result)),
            identifier=relatedBookUuid,
            identifier_column='related_book_uuid',
            domain_model=related_book_domain_model,
            sqlalchemy_model=RelatedBook,
            schema=related_book_schema,
            patch_data=data,
        )

        if isinstance(marshmallow_schema_or_errors, list):
            abort(400, marshmallow_schema_or_errors)
        if marshmallow_schema_or_errors.errors:
            abort(
                400,
                python_dict_to_json_dict(marshmallow_schema_or_errors.errors))

        db.session.add(marshmallow_schema_or_errors.data)
        db.session.commit()

        return python_dict_to_json_dict(
            model_to_dict(marshmallow_schema_or_errors.data, )), 200
Esempio n. 2
0
 def get(self, reviewId):  # type: ignore
     result: Optional[Review] = Review.query.filter_by(
         review_id=reviewId).first()  # noqa: E501
     if result is None:
         abort(404)
     response = python_dict_to_json_dict(model_to_dict(result, )), 200
     return response
Esempio n. 3
0
    def post(self):  # type: ignore
        data = request.get_json(force=True)
        if not isinstance(data, dict):
            return abort(400)

        data['authorId'] = uuid.uuid4()

        marshmallow_schema_or_errors = convert_dict_to_marshmallow_result(
            data=data,
            identifier=data['authorId'],
            identifier_column='author_id',
            domain_model=author_domain_model,
            sqlalchemy_model=Author,
            schema=author_schema,
        )

        if isinstance(marshmallow_schema_or_errors, list):
            abort(400, marshmallow_schema_or_errors)
        if marshmallow_schema_or_errors.errors:
            abort(
                400,
                python_dict_to_json_dict(marshmallow_schema_or_errors.errors))

        db.session.add(marshmallow_schema_or_errors.data)
        db.session.commit()

        return python_dict_to_json_dict(
            model_to_dict(marshmallow_schema_or_errors.data, )), 201
Esempio n. 4
0
 def get(self):
     query = Book.query
     param_book_id = request.args.get('book_id')
     if param_book_id:
         query = query.filter_by(book_id=param_book_id)
     param_name = request.args.get('name')
     if param_name:
         query = query.filter_by(name=param_name)
     param_rating = request.args.get('rating')
     if param_rating:
         query = query.filter_by(rating=param_rating)
     param_author_id = request.args.get('author_id')
     if param_author_id:
         query = query.filter_by(author_id=param_author_id)
     param_collaborator_id = request.args.get('collaborator_id')
     if param_collaborator_id:
         query = query.filter_by(collaborator_id=param_collaborator_id)
     param_published = request.args.get('published')
     if param_published:
         query = query.filter_by(published=param_published)
     param_created = request.args.get('created')
     if param_created:
         query = query.filter_by(created=param_created)
     param_updated = request.args.get('updated')
     if param_updated:
         query = query.filter_by(updated=param_updated)
     result = query.all()
     return python_dict_to_json_dict({"data": [model_to_dict(r) for r in result]})
Esempio n. 5
0
    def put(self, relatedBookUuid):  # type: ignore
        data = request.get_json(force=True)
        if not isinstance(data, dict):
            abort(400)

        if 'id' not in data:
            data['id'] = relatedBookUuid

        marshmallow_schema_or_errors = convert_dict_to_marshmallow_result(
            data=data,
            identifier=relatedBookUuid,
            identifier_column='related_book_uuid',
            domain_model=related_book_domain_model,
            sqlalchemy_model=RelatedBook,
            schema=related_book_schema,
        )

        if isinstance(marshmallow_schema_or_errors, list):
            abort(400, marshmallow_schema_or_errors)
        if marshmallow_schema_or_errors.errors:
            abort(
                400,
                python_dict_to_json_dict(marshmallow_schema_or_errors.errors))

        db.session.add(marshmallow_schema_or_errors.data)
        db.session.commit()

        return python_dict_to_json_dict(
            model_to_dict(marshmallow_schema_or_errors.data, )), 201
Esempio n. 6
0
 def get(self, relatedBookUuid):  # type: ignore
     result: Optional[RelatedBook] = RelatedBook.query.filter_by(
         related_book_uuid=relatedBookUuid).first()  # noqa: E501
     if result is None:
         abort(404)
     response = python_dict_to_json_dict(model_to_dict(result, )), 200
     return response
Esempio n. 7
0
 def get(self, authorId):  # type: ignore
     result: Optional[Author] = Author.query.filter_by(
         author_id=authorId).first()  # noqa: E501
     if result is None:
         abort(404)
     response = python_dict_to_json_dict(model_to_dict(result, )), 200
     return response
Esempio n. 8
0
 def get(self, bookGenreId):  # type: ignore
     result: Optional[BookGenre] = BookGenre.query.filter_by(
         book_genre_id=bookGenreId).first()  # noqa: E501
     if result is None:
         abort(404)
     response = python_dict_to_json_dict(model_to_dict(result, )), 200
     return response
Esempio n. 9
0
 def get(self):
     query = Book.query
     param_book_id = request.args.getlist('book_id')
     if param_book_id:
         query = query.filter(Book.book_id.in_(param_book_id))
     param_name = request.args.getlist('name')
     if param_name:
         query = query.filter(Book.name.in_(param_name))
     param_rating = request.args.getlist('rating')
     if param_rating:
         query = query.filter(Book.rating.in_(param_rating))
     param_author_id = request.args.getlist('author_id')
     if param_author_id:
         query = query.filter(Book.author_id.in_(param_author_id))
     param_collaborator_id = request.args.getlist('collaborator_id')
     if param_collaborator_id:
         query = query.filter(
             Book.collaborator_id.in_(param_collaborator_id))
     param_published = request.args.getlist('published')
     if param_published:
         query = query.filter(Book.published.in_(param_published))
     param_created = request.args.getlist('created')
     if param_created:
         query = query.filter(Book.created.in_(param_created))
     param_updated = request.args.getlist('updated')
     if param_updated:
         query = query.filter(Book.updated.in_(param_updated))
     result = query.all()
     return python_dict_to_json_dict(
         {"data": [model_to_dict(r) for r in result]})
Esempio n. 10
0
 def get(self):
     query = Genre.query
     param_genre_id = request.args.get('genre_id')
     if param_genre_id:
         query = query.filter_by(genre_id=param_genre_id)
     param_title = request.args.get('title')
     if param_title:
         query = query.filter_by(title=param_title)
     result = query.all()
     return python_dict_to_json_dict({"data": [model_to_dict(r) for r in result]})
Esempio n. 11
0
    def get(self, bookGenreId):  # type: ignore
        id_validation_errors = book_genre_schema.validate(
            {'book_genre_id': bookGenreId}, session=db.session, partial=True)
        if id_validation_errors:
            abort(404)

        result: Optional[BookGenre] = BookGenre.query.filter_by(
            book_genre_id=bookGenreId).first()  # noqa: E501
        if result is None:
            abort(404)
        response = python_dict_to_json_dict(model_to_dict(result, )), 200
        return response
Esempio n. 12
0
 def get(self):
     query = RelatedBook.query
     param_related_book_uuid = request.args.getlist('related_book_uuid')
     if param_related_book_uuid:
         query = query.filter(RelatedBook.related_book_uuid.in_(param_related_book_uuid))
     param_book1_id = request.args.getlist('book1_id')
     if param_book1_id:
         query = query.filter(RelatedBook.book1_id.in_(param_book1_id))
     param_book2_id = request.args.getlist('book2_id')
     if param_book2_id:
         query = query.filter(RelatedBook.book2_id.in_(param_book2_id))
     result = query.all()
     return python_dict_to_json_dict({"data": [model_to_dict(r) for r in result]})
Esempio n. 13
0
 def get(self):
     query = BookGenre.query
     param_book_genre_id = request.args.get('book_genre_id')
     if param_book_genre_id:
         query = query.filter_by(book_genre_id=param_book_genre_id)
     param_book_id = request.args.get('book_id')
     if param_book_id:
         query = query.filter_by(book_id=param_book_id)
     param_genre_id = request.args.get('genre_id')
     if param_genre_id:
         query = query.filter_by(genre_id=param_genre_id)
     result = query.all()
     return python_dict_to_json_dict({"data": [model_to_dict(r) for r in result]})
Esempio n. 14
0
    def get(self, authorId):  # type: ignore
        id_validation_errors = author_schema.validate({'author_id': authorId},
                                                      session=db.session,
                                                      partial=True)
        if id_validation_errors:
            abort(404)

        result: Optional[Author] = Author.query.filter_by(
            author_id=authorId).first()  # noqa: E501
        if result is None:
            abort(404)
        response = python_dict_to_json_dict(model_to_dict(result, )), 200
        return response
Esempio n. 15
0
    def patch(self, authorId):  # type: ignore
        id_validation_errors = author_schema.validate({'author_id': authorId},
                                                      session=db.session,
                                                      partial=True)
        if id_validation_errors:
            abort(404)

        result: Optional[Author] = Author.query.filter_by(author_id=authorId)\
            .options(noload('*')).first()  # noqa: E501

        if result is None:
            abort(404)

        data = request.get_json(force=True)
        if not isinstance(data, dict):
            abort(400)

        marshmallow_schema_or_errors = convert_dict_to_marshmallow_result(
            data=json_dict_to_python_dict(model_to_dict(result)),
            identifier=authorId,
            identifier_column='author_id',
            domain_model=author_domain_model,
            sqlalchemy_model=Author,
            schema=author_schema,
            patch_data=data,
        )

        if isinstance(marshmallow_schema_or_errors, list):
            abort(400, marshmallow_schema_or_errors)
        if marshmallow_schema_or_errors.errors:
            abort(
                400,
                python_dict_to_json_dict(marshmallow_schema_or_errors.errors))

        db.session.add(marshmallow_schema_or_errors.data)
        db.session.commit()

        return python_dict_to_json_dict(
            model_to_dict(marshmallow_schema_or_errors.data, )), 200
Esempio n. 16
0
    def get(self, reviewId):  # type: ignore
        id_validation_errors = review_schema.validate({'review_id': reviewId},
                                                      session=db.session,
                                                      partial=True)
        if id_validation_errors:
            abort(404)

        result: Optional[Review] = Review.query.filter_by(
            review_id=reviewId).first()  # noqa: E501
        if result is None:
            abort(404)
        response = python_dict_to_json_dict(model_to_dict(result, )), 200
        return response
Esempio n. 17
0
 def get(self):
     query = Review.query
     param_review_id = request.args.getlist('review_id')
     if param_review_id:
         query = query.filter(Review.review_id.in_(param_review_id))
     param_text = request.args.getlist('text')
     if param_text:
         query = query.filter(Review.text.in_(param_text))
     param_book_id = request.args.getlist('book_id')
     if param_book_id:
         query = query.filter(Review.book_id.in_(param_book_id))
     result = query.all()
     return python_dict_to_json_dict(
         {"data": [model_to_dict(r) for r in result]})
Esempio n. 18
0
 def get(self):
     query = Review.query
     param_review_id = request.args.get('review_id')
     if param_review_id:
         query = query.filter_by(review_id=param_review_id)
     param_text = request.args.get('text')
     if param_text:
         query = query.filter_by(text=param_text)
     param_book_id = request.args.get('book_id')
     if param_book_id:
         query = query.filter_by(book_id=param_book_id)
     result = query.all()
     return python_dict_to_json_dict(
         {"data": [model_to_dict(r) for r in result]})
Esempio n. 19
0
 def get(self):
     query = Author.query
     param_author_id = request.args.get('author_id')
     if param_author_id:
         query = query.filter_by(author_id=param_author_id)
     param_name = request.args.get('name')
     if param_name:
         query = query.filter_by(name=param_name)
     param_favourite_author_id = request.args.get('favourite_author_id')
     if param_favourite_author_id:
         query = query.filter_by(favourite_author_id=param_favourite_author_id)
     param_hated_author_id = request.args.get('hated_author_id')
     if param_hated_author_id:
         query = query.filter_by(hated_author_id=param_hated_author_id)
     result = query.all()
     return python_dict_to_json_dict({"data": [model_to_dict(r) for r in result]})
Esempio n. 20
0
    def get(self, authorId):  # type: ignore
        result: Optional[Author] = Author \
            .query \
            .options(
                joinedload('books')
            ) \
            .filter_by(
                author_id=authorId) \
            .first()  # noqa: E501
        if result is None:
            abort(404)
        result_dict = python_dict_to_json_dict(model_to_dict(
            sqlalchemy_model=result,
            paths=[
                'books',
            ],
        ))

        return result_dict
Esempio n. 21
0
    def get(self, bookId):  # type: ignore
        result: Optional[Book] = Book \
            .query \
            .options(
                joinedload('genre')
            ) \
            .filter_by(
                book_id=bookId) \
            .first()  # noqa: E501
        if result is None:
            abort(404)
        result_dict = python_dict_to_json_dict(model_to_dict(
            sqlalchemy_model=result,
            paths=[
                'genre',
            ],
        ))

        return result_dict
Esempio n. 22
0
 def get(self):
     query = Author.query
     param_author_id = request.args.getlist('author_id')
     if param_author_id:
         query = query.filter(Author.author_id.in_(param_author_id))
     param_name = request.args.getlist('name')
     if param_name:
         query = query.filter(Author.name.in_(param_name))
     param_favourite_author_id = request.args.getlist('favourite_author_id')
     if param_favourite_author_id:
         query = query.filter(
             Author.favourite_author_id.in_(param_favourite_author_id))
     param_hated_author_id = request.args.getlist('hated_author_id')
     if param_hated_author_id:
         query = query.filter(
             Author.hated_author_id.in_(param_hated_author_id))
     result = query.all()
     return python_dict_to_json_dict(
         {"data": [model_to_dict(r) for r in result]})
Esempio n. 23
0
}

with app.app_context():
    db.drop_all()
    db.create_all()
    db.session.add(author_model)
    db.session.add(book_model)
    db.session.add(review_model)
    db.session.commit()

with description('model_to_dict') as self:
    with it('converts a flat model into a dict'):
        with app.app_context():
            retrieved_book = Book.query.filter_by(book_id=BOOK_UUID).first()
        result = model_to_dict(
            sqlalchemy_model=retrieved_book,
        )
        expect(result).to(have_keys(**book_dict))

    with it('converts a singly-nested relationship'):
        with app.app_context():
            retrieved_book = Book.query.\
                filter_by(book_id=BOOK_UUID).\
                options(joinedload('author')).\
                first()
            result = model_to_dict(
                sqlalchemy_model=retrieved_book,
                paths=['author'],
            )
        expect(result).to(have_keys(**book_dict))
        expect(result['author']).to(have_keys(**author_dict))
Esempio n. 24
0
    book_id=1,
    genre_id=1,
)

with app.app_context():
    db.drop_all()
    db.create_all()
    db.session.add(author_model)
    db.session.add(book_model)
    db.session.commit()

with description('model_to_dict') as self:
    with it('converts a flat model into a dict'):
        with app.app_context():
            retrieved_book = Book.query.filter_by(book_id=BOOK_UUID).first()
        result = model_to_dict(sqlalchemy_model=retrieved_book, )
        expect(result).to(have_keys(**book_dict))

    with it('converts a singly-nested relationship'):
        with app.app_context():
            retrieved_book = Book.query.\
                filter_by(book_id=BOOK_UUID).\
                options(joinedload('author')).\
                first()
            result = model_to_dict(
                sqlalchemy_model=retrieved_book,
                paths=['author'],
            )
        expect(result).to(have_keys(**book_dict))
        expect(result['author']).to(have_keys(**author_dict))