Skip to content

Commit

Permalink
Use model_fields_set to check if at least a field is to be updated (#532
Browse files Browse the repository at this point in the history
)

to prevent `False` values to be interpreted as not provided fields

Related to #355
  • Loading branch information
armanddidierjean authored Aug 23, 2024
1 parent 1a5e843 commit 39aa9b3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
16 changes: 5 additions & 11 deletions app/modules/cdr/cruds_cdr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
9 changes: 4 additions & 5 deletions app/modules/cdr/endpoints_cdr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion app/modules/recommendation/cruds_recommendation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 39aa9b3

Please sign in to comment.