Skip to content

Commit

Permalink
Merge pull request #1334 from betagouv/replace-ingredient-request
Browse files Browse the repository at this point in the history
Option pour remplacer un ingrédient demandé par un ingrédient existant
  • Loading branch information
alemangui authored Jan 7, 2025
2 parents ff4a814 + 450c030 commit 6494d62
Show file tree
Hide file tree
Showing 18 changed files with 707 additions and 182 deletions.
104 changes: 40 additions & 64 deletions api/serializers/declaration.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,26 @@ def update(self, instance, validated_data):
)


class DeclaredIngredientCommonSerializer(PrivateFieldsSerializer):
class DeclaredElementNestedField:
def create(self, validated_data):
# DRF ne gère pas automatiquement la création des nested-fields :
# https://www.django-rest-framework.org/api-guide/serializers/#writable-nested-representations
element = validated_data.pop(self.nested_field_name, None)
if element:
id = element.get("id")
try:
validated_data[self.nested_field_name] = self.nested_model.objects.get(pk=id)
except self.nested_model.DoesNotExist:
raise ProjectAPIException(
field_errors={
f"declared_{self.nested_field_name}s": f"L'ingrédient avec l'id « {id} » spécifiée n'existe pas."
}
)

return super().create(validated_data)


class DeclaredIngredientCommonSerializer(DeclaredElementNestedField, PrivateFieldsSerializer):
private_fields = ("request_private_notes", "request_status")


Expand All @@ -113,6 +132,9 @@ class DeclaredPlantSerializer(DeclaredIngredientCommonSerializer):
used_part = serializers.PrimaryKeyRelatedField(queryset=PlantPart.objects.all(), required=False, allow_null=True)
declaration = serializers.PrimaryKeyRelatedField(read_only=True)

nested_field_name = "plant"
nested_model = Plant

class Meta:
model = DeclaredPlant
fields = ADDABLE_ELEMENT_FIELDS + (
Expand All @@ -125,25 +147,17 @@ class Meta:
"unit",
"quantity",
"preparation",
"type",
)

def create(self, validated_data):
# DRF ne gère pas automatiquement la création des nested-fields :
# https://www.django-rest-framework.org/api-guide/serializers/#writable-nested-representations
plant = validated_data.pop("plant", None)
if plant:
try:
validated_data["plant"] = Plant.objects.get(pk=plant.get("id"))
except Plant.DoesNotExist:
raise ProjectAPIException(field_errors={"declared_plants": "La plante spécifiée n'existe pas."})

return super().create(validated_data)


class DeclaredMicroorganismSerializer(DeclaredIngredientCommonSerializer):
element = PassthroughMicroorganismSerializer(required=False, source="microorganism", allow_null=True)
declaration = serializers.PrimaryKeyRelatedField(read_only=True)

nested_field_name = "microorganism"
nested_model = Microorganism

class Meta:
model = DeclaredMicroorganism
fields = ADDABLE_ELEMENT_FIELDS + (
Expand All @@ -156,28 +170,18 @@ class Meta:
"activated",
"strain",
"quantity",
"type",
)

def create(self, validated_data):
# DRF ne gère pas automatiquement la création des nested-fields :
# https://www.django-rest-framework.org/api-guide/serializers/#writable-nested-representations
microorganism = validated_data.pop("microorganism", None)
if microorganism:
try:
validated_data["microorganism"] = Microorganism.objects.get(pk=microorganism.get("id"))
except Microorganism.DoesNotExist:
raise ProjectAPIException(
field_errors={"declared_microorganisms": "Le micro-organisme spécifié n'existe pas."}
)

return super().create(validated_data)


class DeclaredIngredientSerializer(DeclaredIngredientCommonSerializer):
element = PassthroughIngredientSerializer(required=False, source="ingredient", allow_null=True)
unit = serializers.PrimaryKeyRelatedField(queryset=SubstanceUnit.objects.all(), required=False, allow_null=True)
declaration = serializers.PrimaryKeyRelatedField(read_only=True)

nested_field_name = "ingredient"
nested_model = Ingredient

class Meta:
model = DeclaredIngredient
fields = ADDABLE_ELEMENT_FIELDS + (
Expand All @@ -189,25 +193,17 @@ class Meta:
"active",
"quantity",
"unit",
"type",
)

def create(self, validated_data):
# DRF ne gère pas automatiquement la création des nested-fields :
# https://www.django-rest-framework.org/api-guide/serializers/#writable-nested-representations
ingredient = validated_data.pop("ingredient", None)
if ingredient:
try:
validated_data["ingredient"] = Ingredient.objects.get(pk=ingredient.get("id"))
except Ingredient.DoesNotExist:
raise ProjectAPIException(field_errors={"declared_ingredients": "L'ingrédient spécifié n'existe pas."})

return super().create(validated_data)


class DeclaredSubstanceSerializer(DeclaredIngredientCommonSerializer):
element = PassthroughSubstanceSerializer(required=False, source="substance", allow_null=True)
declaration = serializers.PrimaryKeyRelatedField(read_only=True)

nested_field_name = "substance"
nested_model = Substance

class Meta:
model = DeclaredSubstance
fields = ADDABLE_ELEMENT_FIELDS + (
Expand All @@ -218,25 +214,17 @@ class Meta:
"active",
"quantity",
"unit",
"type",
)

def create(self, validated_data):
# DRF ne gère pas automatiquement la création des nested-fields :
# https://www.django-rest-framework.org/api-guide/serializers/#writable-nested-representations
substance = validated_data.pop("substance", None)
if substance:
try:
validated_data["substance"] = Substance.objects.get(pk=substance.get("id"))
except Substance.DoesNotExist:
raise ProjectAPIException(field_errors={"declared_substances": "La substance spécifiée n'existe pas."})

return super().create(validated_data)


class ComputedSubstanceSerializer(serializers.ModelSerializer):
class ComputedSubstanceSerializer(DeclaredElementNestedField, serializers.ModelSerializer):
substance = PassthroughSubstanceSerializer()
unit = serializers.PrimaryKeyRelatedField(queryset=SubstanceUnit.objects.all(), required=False, allow_null=True)

nested_field_name = "substance"
nested_model = Substance

class Meta:
model = ComputedSubstance
fields = (
Expand All @@ -246,18 +234,6 @@ class Meta:
"unit",
)

def create(self, validated_data):
# DRF ne gère pas automatiquement la création des nested-fields :
# https://www.django-rest-framework.org/api-guide/serializers/#writable-nested-representations
substance = validated_data.pop("substance", None)
if substance:
try:
validated_data["substance"] = Substance.objects.get(pk=substance.get("id"))
except Substance.DoesNotExist:
raise ProjectAPIException(field_errors={"declared_substances": "La substance spécifiée n'existe pas."})

return super().create(validated_data)


class AttachmentSerializer(IdPassthrough, serializers.ModelSerializer):
file = Base64FileField(required=False, allow_null=True)
Expand Down
Loading

0 comments on commit 6494d62

Please sign in to comment.