コード例 #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
コード例 #2
0
def convert_dict_to_marshmallow_result(
    data: Mapping[str, Any],
    identifier: str,
    identifier_column: str,
    domain_model: DomainModel,
    sqlalchemy_model: DeclarativeMeta,
    schema: ModelSchema,
    patch_data: Optional[Mapping[str, Any]] = None,
) -> Union[ModelSchema, List[str]]:
    result = sqlalchemy_model.query.filter_by(**{
        identifier_column: identifier
    }).options(noload('*')).first()

    if patch_data is not None:
        data = {**data, **patch_data}

    joined_entity_ids_or_errors = create_joined_entity_id_map(
        domain_model,
        data,
    )

    if isinstance(joined_entity_ids_or_errors, list):
        return joined_entity_ids_or_errors

    data = convert_properties_to_sqlalchemy_properties(
        domain_model,
        joined_entity_ids_or_errors,
        json_dict_to_python_dict(preserve_user_json(data)),
    )

    if result is not None:
        # don't use the 'id' from the json request
        data = {**data, **{'id': result.id}}

    marshmallow_result = schema.load(
        json_dict_to_python_dict(data),
        session=db.session,
        instance=result,
    )

    return marshmallow_result
コード例 #3
0
ファイル: Author.py プロジェクト: turner-townsend/genyrator
    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