def recommend(self, request): params = set(normalize_recipe_params(request.GET.get('q', None))) exact_recipes = [] nearly_there_recipes = [] for recipe in models.Recipe.objects.all(): ingredients = set([x.ingredient.id for x in recipe.recipe_components.all()]) intersection = params & ingredients if len(intersection) > 0: difference = ingredients - intersection if len(difference) is 0: exact_recipes.append(recipe) else: nearly_there_recipes.append({ 'recipe': recipe.pk, 'missing': list(difference), }) nearly_there_recipes.sort(key=lambda x: len(x['missing'])) return JsonResponse({ 'recipes': [recipe.pk for recipe in exact_recipes], 'nearly_there': nearly_there_recipes })
def recommend_old(self, request): params = set(normalize_recipe_params(request.GET.get('q', None))) exact_recipes = [] nearly_there_recipes = [] for recipe in models.Recipe.objects.all(): ingredients = set([x.ingredient.id for x in recipe.recipe_components.all()]) intersection = params & ingredients if len(intersection) > 0: difference = ingredients - intersection difference_length = len(difference) if difference_length is 0: exact_recipes.append(recipe) else: nearly_there_recipes.append({ 'recipe': RecipeSerializer(recipe, many=False).data, 'missing_count': difference_length, }) nearly_there_recipes.sort(key=lambda x: x['missing_count']) return JsonResponse({ 'recipes': RecipeSerializer(exact_recipes, many=True).data, 'nearly_there': json.JSONDecoder().decode( json.dumps(nearly_there_recipes)), })
def testing_recipe(request): params = set(normalize_recipe_params(request.GET.get("q", None))) exact_recipes = [] nearly_there_recipes = [] for recipe in Recipe.objects.all(): ingredients = set([x.ingredient.id for x in recipe.recipe_components.all()]) intersection = params & ingredients if len(intersection) > 0: difference = ingredients - intersection difference_length = len(difference) if difference_length is 0: exact_recipes.append(recipe) else: nearly_there_recipes.append( {"recipe": RecipeSerializer(recipe, many=False).data, "missing": difference_length} ) nearly_there_recipes.sort(key=lambda x: x["missing_count"]) return JsonResponse( { "recipes": RecipeSerializer(exact_recipes, many=True).data, "nearly_there": json.JSONDecoder().decode(json.dumps(nearly_there_recipes)), } )
def recommend_recipes(request): # params = normalize_recipe_params(request.GET.get('q', None)) # print('Ingredients: ' + str(params)) # if params: # params = set(params) # exact_recipes = [] # nearly_there_recipes = [] # probable_recipes = Recipe.objects.has_ingredients(params) # for recipe in probable_recipes: # ingredients = set([x.ingredient.id for x in recipe.recipe_components.all()]) # # if ingredients.issubset(params): # if ingredients == params: # exact_recipes.append(recipe) # else: # nearly_there_recipes.append({ # 'recipe': RecipeSerializer(recipe, many=False).data, # 'missing_count': len(ingredients) - len(params), # }) # nearly_there_recipes.sort(key=lambda x: x['missing_count']) # return JsonResponse({ # 'recipes': RecipeSerializer(exact_recipes, many=True).data, # 'nearly_there': json.JSONDecoder().decode(json.dumps(nearly_there_recipes)), # }) # else: # return JsonResponse({ # 'recipes': [], # 'nearly_there': [], # }) params = set(normalize_recipe_params(request.GET.get("q", None))) exact_recipes = [] nearly_there_recipes = [] for recipe in Recipe.objects.all(): ingredients = set([x.ingredient.id for x in recipe.recipe_components.all()]) intersection = params & ingredients if len(intersection) > 0: difference = ingredients - intersection difference_length = len(difference) if difference_length is 0: exact_recipes.append(recipe) else: nearly_there_recipes.append( {"recipe": RecipeSerializer(recipe, many=False).data, "missing_count": difference_length} ) nearly_there_recipes.sort(key=lambda x: x["missing_count"]) return JsonResponse( { "recipes": RecipeSerializer(exact_recipes, many=True).data, "nearly_there": json.JSONDecoder().decode(json.dumps(nearly_there_recipes)), } )