Beispiel #1
0
def SearchRecipeByName(request, recipe_name: string):
    recipe = recipes.objects.all()
    recipe = recipe.filter(recipe_name__contains=recipe_name)[:10]
    if recipe is not None:
        if request.method == 'GET':
            recipes_serializer = recipesSerializer(recipe, many=True)
            return JsonResponse(recipes_serializer.data, safe=False)
    return JsonResponse({'message': 'The recipe does not exist'},
                        status=status.HTTP_404_NOT_FOUND)
Beispiel #2
0
def New(request):
    if request.method == 'POST':
        recipe_data = JSONParser().parse(request)
        recipe_serializer = recipesSerializer(data=recipe_data)
        if recipe_serializer.is_valid():
            recipe_serializer.save()
            return JsonResponse(recipe_serializer.data,
                                status=status.HTTP_201_CREATED)
        return JsonResponse(recipe_serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
Beispiel #3
0
def GetSuggestions(request, recipe_id):
    if request.method == 'GET':
        topic = recipe_topic.objects.get(recipe_id=recipe_id)
        suggestions_ids = sorted(recipe_topic.objects.filter(
            topic_id=topic.topic_id).values_list('recipe_id', flat=True),
                                 key=lambda x: random.random())[:2]

        user_recipes_query_set = recipes.objects.filter(
            recipe_id__in=suggestions_ids)
        serialized_user_recipes = recipesSerializer(user_recipes_query_set,
                                                    many=True)

        return JsonResponse(serialized_user_recipes.data, safe=False)
Beispiel #4
0
def RecipeID(request, recipe_id):

    try:
        recipe = recipes.objects.get(recipe_id=recipe_id)
    except recipes.DoesNotExist:
        return JsonResponse({'message': 'The recipe does not exist'},
                            status=status.HTTP_404_NOT_FOUND)
    if request.method == 'GET':
        recipes_serializer = recipesSerializer(recipe)
        return JsonResponse(recipes_serializer.data)
    elif request.method == 'PUT':
        recipe_data = JSONParser().parse(request)
        recipes_serializer = recipesSerializer(recipe, data=recipe_data)
        if recipes_serializer.is_valid():
            recipes_serializer.save()
            return JsonResponse(recipes_serializer.data)
        return JsonResponse(recipes_serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
    elif request.method == 'DELETE':
        recipe.delete()
        return JsonResponse({'message': 'Recipe was deleted successfully!'},
                            status=status.HTTP_204_NO_CONTENT)
Beispiel #5
0
def UserRecipePage(request):
    if request.method == 'GET':
        user_recipes_id = user_recipes.objects.filter(
            users_id_id=request.user.id)
        user_recipes_id_list = []

        for recipe_user_id_pairs in user_recipes_id.values_list():
            user_recipes_id_list.append(recipe_user_id_pairs[1])

        user_recipes_query_set = recipes.objects.filter(
            recipe_id__in=user_recipes_id_list)
        serialized_user_recipes = recipesSerializer(user_recipes_query_set,
                                                    many=True)

        return JsonResponse(serialized_user_recipes.data, safe=False)
Beispiel #6
0
def SearchRecipeByIngredients(request):
    if request.body:
        body = json.loads(request.body)
        try:
            ingredient_names = [
                item['ingredient_name'] for item in body.get('ingredient')
            ]
            ingredient_ids = [
                item['ingredient_id'] for item in body.get('ingredient')
            ]
            ingredients_found = ingredients.objects.filter(
                ingredient_name__in=ingredient_names)
            if ingredients_found:
                recipe_ids = recipe_ingredients.objects.filter(
                    ingredient_id__in=ingredient_ids)[:10]
                recipe_ids = [
                    item['recipe_id_id'] for item in recipe_ids.values()
                ]
                recipes_res = recipes.objects.filter(recipe_id__in=recipe_ids)
                res = []
                for item in recipes_res.values():
                    res.append(
                        dict(recipe_id=item['recipe_id'],
                             recipe_name=item['recipe_name'],
                             image=item['image'],
                             cooking_method=item['cooking_method']))
            else:
                raise
        except:
            return JsonResponse(
                {'message': 'Recipe with these ingredients does not exist'},
                status=status.HTTP_404_NOT_FOUND)
        if request.method == 'POST':
            recipes_serializer = recipesSerializer(res, many=True)
            return JsonResponse(recipes_serializer.data, safe=False)
    else:
        return JsonResponse({'message': 'No ingredients given'},
                            status=status.HTTP_400_BAD_REQUEST)