def ingredients(req):
    """
    Get all ingredients, or create a new one
    """
    if req.method == 'GET':
        ingredients = Ingredient.objects.all()
        serializer = IngredientSerializer(ingredients, many=True)
        return Response(serializer.data)

    elif req.method == 'POST':
        serializer = IngredientSerializer(data=req.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemple #2
0
def search(req):
    """
    Search ingredient
    """
    q = slugify(req.GET.get('query', ''))
    ingredients = Ingredient.objects.filter(slug__startswith=q)
    seriealizer = IngredientSerializer(ingredients, many=True)
    return Response(seriealizer.data)
Exemple #3
0
def ingredient(req, ingredientId):
    """
    Retrive, modify or delete single ingredient by id
    """
    try:
        ingredient = Ingredient.objects.get(id=ingredientId)
    except Ingredient.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if req.method == 'GET':
        serializer = IngredientSerializer(ingredient)
        return Response(serializer.data)

    elif req.method == 'PUT':
        serializer = IngredientSerializer(ingredient, data=req.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        else:
            return Response(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)

    elif req.method == 'DELETE':
        ingredient.delete()
        return HttpResponse(status=status.HTTP_204_NO_CONTENT)
Exemple #4
0
class InclusionReadSerializer(serializers.HyperlinkedModelSerializer):

    value = serializers.FloatField(min_value=0, default=1)
    unit = serializers.CharField(default='g')
    food = serializers.ReadOnlyField(source='food.id')
    name = serializers.ReadOnlyField(source='ingredient.name')
    slug = serializers.ReadOnlyField(source='ingredient.slug')
    ingredient = IngredientSerializer()

    class Meta:
        model = Inclusion
        fields = ('food', 'value', 'unit', 'name', 'slug', 'ingredient')
def ingredient(req, ingredientId):
    """
    Retrive, modify or delete single ingredient by id
    """
    try:
        ingredient = Ingredient.objects.get(id=ingredientId)
    except Ingredient.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if req.method == 'GET':
        serializer = IngredientSerializer(ingredient)
        return Response(serializer.data)

    elif req.method == 'PUT':
        serializer = IngredientSerializer(ingredient, data=req.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    elif req.method == 'DELETE':
        ingredient.delete()
        return HttpResponse(status=status.HTTP_204_NO_CONTENT)
Exemple #6
0
def slug(req, slug):
    """
    Retrive or delete single ingredient by slug
    """
    try:
        ingredient = Ingredient.objects.get(slug=slug)
    except Ingredient.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if req.method == 'GET':
        serializer = IngredientSerializer(ingredient)
        return Response(serializer.data)

    elif req.method == 'DELETE':
        ingredient.delete()
        return HttpResponse(status=status.HTTP_204_NO_CONTENT)
Exemple #7
0
def ingredients(req):
    """
    Get all ingredients, or create a new one
    """
    if req.method == 'GET':
        ingredients = Ingredient.objects.all()
        serializer = IngredientSerializer(ingredients, many=True)
        return Response(serializer.data)

    elif req.method == 'POST':
        serializer = IngredientSerializer(data=req.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemple #8
0
class AteIngredientDetailSerializer(serializers.ModelSerializer):
    ingredient = IngredientSerializer()

    class Meta:
        model = AteIngredient
        fields = ('ingredient', 'value', 'unit', 'created')