def post(self, request): serializer = RecipeSerializer(data=request.data) if serializer.is_valid(raise_exception=ValueError): data = serializer.create(validated_data=request.data) return Response(json.dumps(data), status=status.HTTP_201_CREATED) return Response(serializer.error_messages, status=status.HTTP_400_BAD_REQUEST)
def partial_update(self, request, *args, **kwargs): if kwargs['pk']: id = int(kwargs['pk']) recipe = Recipe.objects.get(id=id) if request.data.get('description'): description = request.data.get('description') recipe.description = description if request.data.get('name'): name = request.data.get('name') recipe.name = name recipe.save() if isinstance(request.data, dict): past_ingredients = Ingredient.objects.filter( recipe=recipe).delete() if request.data.get('ingredients'): ingredients_data = request.data.get('ingredients') for ingredient in ingredients_data: Ingredient.objects.create(name=ingredient['name'], recipe=recipe) else: past_ingredients = Ingredient.objects.filter( recipe=recipe).delete() if request.data.getlist('ingredients'): ingredients_data = request.data.getlist('ingredients') for ingredient in ingredients_data: ing_map = ast.literal_eval(ingredient) ingredient = Ingredient.objects.create( name=ing_map['name'], recipe=recipe) return Response(json.dumps(RecipeSerializer(recipe).data), status=status.HTTP_200_OK) else: return Response(json.dumps('{}'), status=status.HTTP_400_BAD_REQUEST)
def test_serialize_only_ingredients_of_recipe(self): """ Test that a recipe only serializes its ingredients""" recipeName = 'Pizza' recipeDescription = 'A la italiana' recipe1 = Recipe.objects.create(name=recipeName, description=recipeDescription) recipeName = 'Llenties rares' recipeDescription = 'Demanar un tupper' recipe2 = Recipe.objects.create(name=recipeName, description=recipeDescription) ingredientName = 'Tomate' ingredient1 = Ingredient.objects.create(name=ingredientName, recipe=recipe1) ingredientName = 'Pinya' ingredient2 = Ingredient.objects.create(name=ingredientName, recipe=recipe2) expectedResult = { 'id': 1, 'name': 'Pizza', 'description': 'A la italiana', 'ingredients': [{ 'name': 'Tomate' }] } self.assertEqual(expectedResult, RecipeSerializer(recipe1).data)
def test_serializer_recipe(self): """ Test serializer for recipes """ recipeName = 'Llenties' recipeDescription = 'Demanar un tupper' recipe = Recipe.objects.create(name=recipeName, description=recipeDescription) expectedResult = { 'id': 1, 'name': 'Llenties', 'description': 'Demanar un tupper', 'ingredients': [] } self.assertEqual(expectedResult, RecipeSerializer(recipe).data)
def test_create_recipe(self): """ Test creating a recipe """ payload = { 'id': 1, 'name': 'Curry boar', 'description': 'Make it in the BBQ', 'ingredients':[] } response = self.client.post(RECIPES_URL, payload) exists = Recipe.objects.filter( name=payload['name'], description=payload['description'], ).exists() self.assertTrue(exists) self.assertEqual(response.status_code, status.HTTP_201_CREATED) recipe = Recipe.objects.get(name=payload['name']) serializedRecipe = RecipeSerializer(recipe) self.assertEqual(response.data.replace('\"','\''), str(payload))
def test_serializer_ingredient(self): """ Test serializer for ingredients """ recipeName = 'Pizza' recipeDescription = 'A la italiana' recipe = Recipe.objects.create(name=recipeName, description=recipeDescription) ingredientName = 'Tomate' ingredient = Ingredient.objects.create(name=ingredientName, recipe=recipe) expectedResult = { 'id': 1, 'name': 'Pizza', 'description': 'A la italiana', 'ingredients': [{ 'name': 'Tomate' }] } self.assertEqual(expectedResult, RecipeSerializer(recipe).data)
def get(self, request): """ Return a list of all recipes. If needed, filtered by name""" queryset = self.get_queryset() serializer = RecipeSerializer(queryset, many=True) return Response(json.dumps(serializer.data))