Exemplo n.º 1
0
    def test_serialization_from_string(self, clear_recipe_index, test_recipe):
        """
        Should be possible to store recipes from JSON as string using the serializer
        """

        serializer = RecipeSerializer(data=test_recipe)
        assert serializer.is_valid()
        assert serializer.validated_data[
            "name"] == "Three In One Onion Dip Recipe"
        assert serializer.save()

        recipe4 = get_object_or_404(Recipe,
                                    name="Three In One Onion Dip Recipe")
        assert recipe4.ingredients == "cheddar cheese, cheese, green onion"
Exemplo n.º 2
0
Arquivo: recipe.py Projeto: zessx/gulo
    def search(self, request, pk=None, parent_lookup_recipe=None):
        """
        Search recipes.
        """
        recipes = Recipe.objects.all()

        if 'text' in request.data:
            recipes = recipes.filter(title__icontains=request.data['text']) | \
                recipes.filter(steps__text__icontains=request.data['text']) | \
                recipes.filter(ingredients__name__icontains=request.data['text'])

        if 'tags' in request.data:
            for tag in request.data['tags']:
                recipes = recipes.filter(tags__name=tag)

        if 'dish' in request.data:
            recipes = recipes.filter(dish=request.data['dish'])

        orderbyList = ['-updated_at']
        if 'sort' in request.data:
            if request.data['sort'] in ['popular', 'popularity']:
                orderbyList = ['-popularity'] + orderbyList
            if request.data['sort'] in ['duration', 'timing']:
                orderbyList = ['duration'] + orderbyList
        recipes = recipes.order_by(*orderbyList)

        return Response(status=status.HTTP_200_OK,
                        data=RecipeSerializer(recipes.distinct(),
                                              many=True).data)
Exemplo n.º 3
0
 def test_many_recipe_objects(self, clear_recipe_index):
     """
     Should be possible to retrieve multiple recipe objs using the serializer
     """
     # GIVEN
     baker.make("recipes.Recipe", _quantity=2)
     serializer = RecipeSerializer(Recipe.objects.all(), many=True)
     assert len(serializer.data) == 2
Exemplo n.º 4
0
    def test_search_service_not_found(self, clear_recipe_index, test_recipe):
        """
        Test the search service for expected results.
        """
        # GIVEN
        serializer = RecipeSerializer(data=test_recipe)
        assert serializer.is_valid()
        assert serializer.validated_data[
            "name"] == "Three In One Onion Dip Recipe"
        serializer.save()

        # WHEN
        recipe_search = RecipeSearch()
        results = recipe_search.do_search({"ingredients": "do not find me"})

        # THEN
        assert not len(results)
Exemplo n.º 5
0
def recipe_list(request):
    """
    List all recipes, or create a new recipe.
    """
    if request.method == 'GET':
        recipes = Recipe.objects.all()
        serializer = RecipeSerializer(recipes, many=True)
        return JsonResponse(serializer.data, safe=False)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = RecipeSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data, status=201)
        return JsonResponse(serializer.errors, status=400)
Exemplo n.º 6
0
    def test_serialization_from_bytestream(self, clear_recipe_index):
        """
        Should be possible to store recipes as bytestream from JSON using the serializer
        """
        # GIVEN
        recipe = baker.make("recipes.Recipe",
                            name="testname",
                            url="http://sometestplace.com")

        # WHEN
        serializer = RecipeSerializer(recipe)
        json_content = JSONRenderer().render(serializer.data)
        stream = io.BytesIO(json_content)
        data = JSONParser().parse(stream)

        serializer = RecipeSerializer(data=data)
        assert serializer.is_valid()
        assert serializer.validated_data["name"] == "testname"
        assert serializer.save()
Exemplo n.º 7
0
def recipe_detail(request, pk):
    """
    Retrieve, update or delete a recipe.
    """
    try:
        recipe = Recipe.objects.get(pk=pk)
    except Recipe.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        serializer = RecipeSerializer(recipe)
        return JsonResponse(serializer.data)

    elif request.method == 'PUT':
        data = JSONParser().parse(request)
        serializer = RecipeSerializer(recipe, data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data)
        return JsonResponse(serializer.errors, status=400)

    elif request.method == 'DELETE':
        recipe.delete()
        return HttpResponse(status=204)