Exemplo n.º 1
0
 def post(self, request):
     data = JSONParser().parse(request)
     serializer = IngredientSerializer(data=data)
     if serializer.is_valid():
         serializer.save()
         return JSONResponse(serializer.data,
                             status=status.HTTP_201_CREATED)
     return JSONResponse(serializer.errors,
                         status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 2
0
 def patch(self, request, pk):
     try:
         data = Ingredient.objects.get(pk=pk)
     except Ingredient.DoesNotExist:
         return HttpResponse(status=status.HTTP_404_NOT_FOUND)
     parsed_data = JSONParser().parse(request)
     serializer = IngredientSerializer(data, data=parsed_data, partial=True)
     if serializer.is_valid():
         serializer.save()
         return JSONResponse(serializer.data)
     return JSONResponse(serializer.errors,
                         status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 3
0
 def get(self, request, pk):
     try:
         data = Ingredient.objects.get(pk=pk)
     except Ingredient.DoesNotExist:
         return HttpResponse(status=status.HTTP_404_NOT_FOUND)
     serializer = IngredientSerializer(data)
     return JSONResponse(serializer.data)
Exemplo n.º 4
0
class SaladSerializer(serializers.ModelSerializer):
    ingredients = IngredientSerializer(many=True)
    photo = serializers.ImageField(max_length=None, use_url=True)

    class Meta:
        model = Salad
        exclude = ("created", "updated")
Exemplo n.º 5
0
class ProductSerializer(serializers.ModelSerializer):

    categoryProduct = CategorySerializer()
    ingredientProduct = IngredientSerializer(many=True)

    class Meta:
        model = Product
        fields = '__all__'
Exemplo n.º 6
0
class IngredientListView(APIView):
    @csrf_exempt
    @swagger_auto_schema(
        operation_description="Gets a list of Ingredient objects.",
        responses={200: IngredientSerializer(many=True)},
        tags=['Ingredient'],
    )
    def get(self, request, *args, **kwargs):
        objects: QuerySet[Ingredient] = Ingredient.objects.all()
        serializer = IngredientSerializer(objects, many=True)
        return JSONResponse(serializer.data)

    @csrf_exempt
    @swagger_auto_schema(
        operation_description=
        "Creates a new Ingredient entry with a given name",
        # query_serializer=IngredientSerializer,
        request_body=openapi.Schema(
            type=openapi.TYPE_OBJECT,
            required=['name'],
            properties={
                'name':
                openapi.Schema(description='The ingredients unique name.',
                               type=openapi.TYPE_STRING)
            },
        ),
        responses={
            200:
            IngredientSerializer(many=False),
            400:
            """
                The required request parameters are not met.
                """,
        },
        tags=['Ingredient'],
    )
    def post(self, request):
        data = JSONParser().parse(request)
        serializer = IngredientSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data,
                                status=status.HTTP_201_CREATED)
        return JSONResponse(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 7
0
class UsageSerializer(WritableNestedModelSerializer):
    ingredient = IngredientSerializer()

    class Meta:
        model = Usage
        fields = ("ingredient", "amount_in_units")
        extra_kwargs = {'recipe_id': {'validators': []}}

    '''    
Exemplo n.º 8
0
class InvoiceDetailSerializer(serializers.ModelSerializer):

    comboInvoiceDetail = ComboSerializer()
    productInvoiceDetail = ProductSerializer()
    ingredientInvoiceDetail = IngredientSerializer(many=True)

    class Meta:
        model = InvoiceDetail
        fields = '__all__'
Exemplo n.º 9
0
class RecipeSerializer(serializers.ModelSerializer):

    user = serializers.ReadOnlyField(source='user.id')
    ingredients = IngredientSerializer(many=True, required=False)

    class Meta:
        model = Recipe
        fields = [
            'title', 'description', 'cuisine', 'user', 'id', 'ingredients'
        ]
Exemplo n.º 10
0
def test_serialize_ingredient_serializer(ingredient):
    """
    IngredientSerializer should serialize ingredient instance correctly
    """
    serialized_ingredient = IngredientSerializer(instance=ingredient)

    assert serialized_ingredient.data["name"] == ingredient.name.name
    assert serialized_ingredient.data["unit"] == ingredient.unit.name
    assert serialized_ingredient.data["group"] == ingredient.group.id
    assert serialized_ingredient.data["amount"] == ingredient.amount
Exemplo n.º 11
0
class RecipeIngredientSerializer(serializers.ModelSerializer):
    recipe = RecipeSerializer()
    ingredient = IngredientSerializer()
    amount = serializers.FloatField(required=True)

    class Meta:
        model = RecipeIngredient
        fields = ('id', 'recipe', 'ingredient', 'amount')
        depth = 1

    @transaction.atomic
    def create(self, validated_data):
        return super(RecipeIngredientSerializer, self).create(validated_data)
Exemplo n.º 12
0
class IngredientByClientRestSerializer(serializers.ModelSerializer):
    ingredient_topping = IngredientSerializer()

    class Meta:
        model = IngredientByClient
        fields = '__all__'
Exemplo n.º 13
0
class IngredientDetailView(APIView):
    @csrf_exempt
    @swagger_auto_schema(
        operation_description="Gets an Ingredient object for a given id.",
        responses={
            200:
            IngredientSerializer(many=False),
            404:
            """
               The object could not be retrieved, since it doesn't exist.
               """,
        },
        tags=['Ingredient'],
    )
    def get(self, request, pk):
        try:
            data = Ingredient.objects.get(pk=pk)
        except Ingredient.DoesNotExist:
            return HttpResponse(status=status.HTTP_404_NOT_FOUND)
        serializer = IngredientSerializer(data)
        return JSONResponse(serializer.data)

    @csrf_exempt
    @swagger_auto_schema(
        operation_description="Updates an Ingredient object with a given id.",
        # query_serializer=IngredientSerializer,
        request_body=openapi.Schema(
            type=openapi.TYPE_OBJECT,
            required=['name'],
            properties={
                'name':
                openapi.Schema(description='The ingredients unique name.',
                               type=openapi.TYPE_STRING)
            },
        ),
        responses={
            200:
            IngredientSerializer(many=False),
            400:
            """
                The required request parameters are not met.
                """,
            404:
            """
               The object could not be updated, since it doesn't exist.
               """,
        },
        tags=['Ingredient'],
    )
    def put(self, request, pk):
        try:
            data = Ingredient.objects.get(pk=pk)
        except Ingredient.DoesNotExist:
            return HttpResponse(status=status.HTTP_404_NOT_FOUND)

        parsed_data = JSONParser().parse(request)
        serializer = IngredientSerializer(data, data=parsed_data)
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data)
        return JSONResponse(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)

    @csrf_exempt
    @swagger_auto_schema(
        operation_description="Updates an Ingredient object with a given id.",
        # query_serializer=IngredientSerializer,
        request_body=openapi.Schema(
            type=openapi.TYPE_OBJECT,
            required=[],
            properties={
                'name':
                openapi.Schema(description='The ingredients unique name.',
                               type=openapi.TYPE_STRING)
            },
        ),
        responses={
            200:
            IngredientSerializer(many=False),
            400:
            """
                The required request parameters are not met.
                """,
            404:
            """
               The object could not be updated, since it doesn't exist.
               """,
        },
        tags=['Ingredient'],
    )
    def patch(self, request, pk):
        try:
            data = Ingredient.objects.get(pk=pk)
        except Ingredient.DoesNotExist:
            return HttpResponse(status=status.HTTP_404_NOT_FOUND)
        parsed_data = JSONParser().parse(request)
        serializer = IngredientSerializer(data, data=parsed_data, partial=True)
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data)
        return JSONResponse(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)

    @csrf_exempt
    @swagger_auto_schema(
        operation_description="Deletes an Ingredient object with a given id.",
        responses={
            203:
            None,
            404:
            """
                The object could not be deleted, since it doesn't exist.
                """,
        },
        tags=['Ingredient'],
    )
    def delete(self, request, pk):
        try:
            data = Ingredient.objects.get(pk=pk)
        except Ingredient.DoesNotExist:
            return HttpResponse(status=status.HTTP_404_NOT_FOUND)

        data.delete()
        return HttpResponse(status=status.HTTP_204_NO_CONTENT)
Exemplo n.º 14
0
 def get(self, request, *args, **kwargs):
     objects: QuerySet[Ingredient] = Ingredient.objects.all()
     serializer = IngredientSerializer(objects, many=True)
     return JSONResponse(serializer.data)
Exemplo n.º 15
0
def test_ingredient_serializer_fields(data, is_valid):
    """
    test whether each data passed to serializer is valid
    """
    deserialized_data = IngredientSerializer(data=data)
    assert deserialized_data.is_valid() == is_valid
Exemplo n.º 16
0
def test_deserialize_ingredient_serializer(ingredient_data):
    """
    IngredientSerializer should deserialize ingredient instance correctly
    """
    deserialized_data = IngredientSerializer(data=ingredient_data)
    assert deserialized_data.is_valid()