From 39aa9b324bbd85ecbc51bc84008a95ca5aea1009 Mon Sep 17 00:00:00 2001 From: Armand Didierjean <95971503+armanddidierjean@users.noreply.github.com> Date: Fri, 23 Aug 2024 17:11:07 +0200 Subject: [PATCH] Use model_fields_set to check if at least a field is to be updated (#532) to prevent `False` values to be interpreted as not provided fields Related to #355 --- app/modules/cdr/cruds_cdr.py | 16 +++++----------- app/modules/cdr/endpoints_cdr.py | 9 ++++----- .../recommendation/cruds_recommendation.py | 2 +- 3 files changed, 10 insertions(+), 17 deletions(-) diff --git a/app/modules/cdr/cruds_cdr.py b/app/modules/cdr/cruds_cdr.py index 35f6cdfb6..6a55414e2 100644 --- a/app/modules/cdr/cruds_cdr.py +++ b/app/modules/cdr/cruds_cdr.py @@ -101,7 +101,7 @@ async def update_seller( seller_id: UUID, seller: schemas_cdr.SellerEdit, ): - if not any(seller.model_dump().values()): + if not bool(seller.model_fields_set): return await db.execute( @@ -167,11 +167,8 @@ async def update_product( product_id: UUID, product: schemas_cdr.ProductEdit, ): - if not any( - product.model_dump( - exclude_none=True, - exclude={"product_constraints", "document_constraints"}, - ), + if not bool( + product.model_fields_set - {"product_constraints", "document_constraints"}, ): # If there isn't any field to update, we do nothing return @@ -287,11 +284,8 @@ async def update_product_variant( variant_id: UUID, product_variant: schemas_cdr.ProductVariantEdit, ): - if not any( - product_variant.model_dump( - exclude_none=True, - exclude={"allowed_curriculum"}, - ).values(), + if not bool( + product_variant.model_fields_set - {"allowed_curriculum"}, ): return diff --git a/app/modules/cdr/endpoints_cdr.py b/app/modules/cdr/endpoints_cdr.py index daebbf06b..4c109ef38 100644 --- a/app/modules/cdr/endpoints_cdr.py +++ b/app/modules/cdr/endpoints_cdr.py @@ -448,7 +448,7 @@ async def update_seller( """ await check_request_consistency(db=db, seller_id=seller_id) - if not any(seller.model_dump().values()): + if not bool(seller.model_fields_set): raise HTTPException( status_code=400, detail="You must specify at least one field to update", @@ -617,7 +617,7 @@ async def update_product( **User must be part of the seller's group to use this endpoint** """ - if not any(product.model_dump().values()): + if not bool(product.model_fields_set): # We verify that some fields are to be changed # These fields may be `product_constraints` or `document_constraints` that are updated manually raise HTTPException( @@ -825,9 +825,8 @@ async def update_product_variant( **User must be part of the seller's group to use this endpoint** """ - if not any( - product_variant.model_dump().values(), - ): + + if not bool(product_variant.model_fields_set): raise HTTPException( status_code=400, detail="You must specify at least one field to update", diff --git a/app/modules/recommendation/cruds_recommendation.py b/app/modules/recommendation/cruds_recommendation.py index c89ee5d49..dad734f3b 100644 --- a/app/modules/recommendation/cruds_recommendation.py +++ b/app/modules/recommendation/cruds_recommendation.py @@ -28,7 +28,7 @@ async def update_recommendation( recommendation: schemas_recommendation.RecommendationEdit, db: AsyncSession, ): - if not any(recommendation.model_dump().values()): + if not bool(recommendation.model_fields_set): return result = await db.execute(