def put(self, request, recipe_id): recipe = get_object_or_404(Recipe, pk=recipe_id) serializer = RecipeSerializer(instance=recipe, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response({'errors': serializer.errors})
def recipe_detail(request, recipe_id): if request.method == 'GET': recipe = get_object_or_404(Recipe, id=recipe_id) # recipe = Recipe.objects.filter(pk=recipe_id).first() # if not recipe: # return Response({ 'detail': f'Recipe with {recipe_id} id/pk not founded' }) serializer = RecipeReadSerializer(recipe, context={'request': request}) return Response(serializer.data, status=HTTP_200_OK) if request.method == 'PUT': recipe_data = request.data recipe = Recipe.objects.get(pk=recipe_id) serializer = RecipeSerializer(data=recipe_data, instance=recipe, context={'request': request}) serializer.is_valid(raise_exception=True) serializer.save() return Response(serializer.data, status=HTTP_200_OK) if request.method == 'PATCH': recipe_data = request.data recipe = Recipe.objects.get(pk=recipe_id) serializer = RecipeSerializer(data=recipe_data, instance=recipe, partial=True, context={'request': request}) serializer.is_valid(raise_exception=True) serializer.save() return Response(serializer.data, status=HTTP_200_OK) if request.method == 'DELETE': recipe = Recipe.objects.get(pk=recipe_id).delete() return Response(status=HTTP_204_NO_CONTENT)
def recipe(request, id): response_data = {'message': 'success', 'action': 'read'} status_code = HTTP_200_OK if request.method == 'GET': recipe = get_object_or_404(Recipe, id=id) serializer = RecipeReadSerializer(recipe) response_data['recipe'] = serializer.data elif request.method == 'PUT': recipe = get_object_or_404(Recipe, id=id) serializer = RecipeSerializer(recipe, request.data) serializer.is_valid(raise_exception=True) serializer.save() response_data['recipe'] = serializer.data response_data['action'] = 'modify' elif request.method == 'PATCH': recipe = get_object_or_404(Recipe, id=id) serializer = RecipeSerializer( instance=recipe, data=request.data, partial=True, ) serializer.is_valid(raise_exception=True) serializer.save() response_data['recipe'] = serializer.data response_data['action'] = 'partial_modify' elif request.method == 'DELETE': recipe = get_object_or_404(Recipe, id=id) recipe.delete() response_data = {} status_code = HTTP_204_NO_CONTENT return Response(response_data, status=status_code)
def post(self, request): serializer = RecipeSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response({'error': serializer.errors}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def recipes_api(request): response_data = {'message': 'success', 'action': 'read'} status_code = HTTP_200_OK if request.method == 'GET': category_titles = request.GET.get('categories') tag_titles = request.GET.get('tags') if category_titles: category_title = category_titles.split(",") recipes = Recipe.objects.filter(category__title__in=category_title) elif tag_titles: tag_title = tag_titles.split(",") recipes = Recipe.objects.filter(tags__title__in=tag_title) else: recipes = Recipe.objects.filter(is_published=True) serializer = RecipeReadSerializer(recipes, many=True) response_data['recipe'] = serializer.data # return Response(serializer.data, status=HTTP_200_OK) elif request.method == 'POST': recipe_data = request.data serializer = RecipeSerializer(data=recipe_data) serializer.is_valid(raise_exception=True) serializer.save() response_data['recipe'] = serializer.data response_data['action'] = 'modify' status_code = HTTP_201_CREATED # return Response(serializer.data, status=HTTP_201_CREATED) return Response(response_data, status=status_code)
def index(request): recipes = Recipe.objects.all() resultJson = [] serrecipe = RecipeSerializer(recipes, many=True) for recipe in serrecipe.data: category = Category.objects.get(category_key=recipe["category_key"]) recipe["category"] = category.name try: steps = CookStep.objects.filter(recipe_id=recipe["id"]) except Exception as e: return JsonResponse({"error": str(e), "code": 1}, status=404) stepList = [] for step in steps.values(): stepList.append(step["howto_step"]) recipe["howto"] = stepList try: ingredients = IngredientsFull.objects.filter( recipe_id=recipe["id"]) except Exception as e: return JsonResponse({"error": str(e), "code": 1}, status=404) ingredientList = [] for ingredient in ingredients.values(): ingredientList.append(ingredient["ingredient"]) recipe["ingredients_full"] = ingredientList try: ingredientSmall = Ingredient.objects.filter(recipe_id=recipe["id"]) except Exception as e: return JsonResponse({"error": str(e), "code": 1}, status=404) ingredientSmallList = [] for ingredient in ingredientSmall.values(): ingredientSmallList.append({ 'name': ingredient["name"], 'amount': ingredient["amount"] }) recipe["ingredients"] = ingredientSmallList resultJson.append(recipe) ingredients = Ingredient.objects.values() ingredientsList = [] for ingredient in ingredients: ingredientsList.append(ingredient["name"]) return JsonResponse( { "menuList": list(resultJson), "ingredients": list(ingredientsList), "code": 0 }, safe=False)
def test_retrieve_recipe(self): sample_recipe(name="Cheese") sample_recipe(name="Pasta") sample_recipe(name="Frijoles") sample_recipe(name="Costa") res = self.client.get(RECIPES_URL) recipes = Recipe.objects.all() serializer = RecipeSerializer(recipes, many=True) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(res.data, serializer.data) self.assertEqual(len(res.data), 4)
def recipes(request): if request.method == 'GET': recipe_list = Recipe.objects.all() serializer = RecipeReadSerializer(recipe_list, context={ 'request': request, }, many=True) return Response(serializer.data, status=HTTP_200_OK) elif request.user and request.user.is_authenticated and request.method == 'POST': recipe_data = request.data serializer = RecipeSerializer( data=recipe_data, context={ 'request': request, }, ) serializer.is_valid(raise_exception=True) serializer.save() return Response(serializer.data, status=HTTP_201_CREATED) else: raise PermissionDenied
def get_recipes_by_category(request, category_id): main_category = Category.objects.get(id=category_id) recipes = Recipe.objects.all() resultJson = [] serrecipe = RecipeSerializer(recipes, many=True) for recipe in serrecipe.data: category = Category.objects.get(category_key=recipe["category_key"]) if (category.name == main_category.name): try: steps = CookStep.objects.filter(recipe_id=recipe["id"]) except Exception as e: return JsonResponse({"error": str(e), "code": 1}, status=404) stepList = [] for step in steps.values(): stepList.append(step["howto_step"]) recipe["howto"] = stepList try: ingredients = IngredientsFull.objects.filter( recipe_id=recipe["id"]) except Exception as e: return JsonResponse({"error": str(e), "code": 1}, status=404) ingredientList = [] for ingredient in ingredients.values(): ingredientList.append(ingredient["ingredient"]) recipe["ingredients_full"] = ingredientList resultJson.append(recipe) return JsonResponse({ "recipeList": list(resultJson), "code": 0 }, safe=False)
def test_view_recipe_detail(self): recipe = sample_recipe() url = detail_url(recipe.id) res = self.client.get(url) serializer = RecipeSerializer(recipe) self.assertEqual(res.data, serializer.data)
def get(self, request, category_id): recipes = Recipe.objects.filter(categoryId=category_id) serializer = RecipeSerializer(recipes, many=True) return Response(serializer.data)
def get(self, request, recipe_id): # recipe = self.get_object(recipe_id) recipe = get_object_or_404(Recipe, pk=recipe_id) serializer = RecipeSerializer(recipe) return Response(serializer.data)
def get(self, request): recipies = Recipe.objects.all() serializer = RecipeSerializer(recipies, many=True) return Response(serializer.data)
def search(request, name): recipes = Recipe.objects.filter(name=name) if request.method == 'GET': recipes_serializer = RecipeSerializer(recipes, many=True) return JsonResponse(recipes_serializer.data, safe=False)