def add_food(req): user_id = 0 if req.session.has_key('user_id'): user_id = req.session['user_id'] else: user_id = req.POST.dict()['user_id'] food_dict = req.POST.dict() food_dict['food_owner'] = user_id # get nutrition values from api ingredients = json.loads(food_dict['ingredients']) food_recipe = "" ingredient_list = [] for ingredient in ingredients: food_recipe += ingredient["value"] + " " + ingredient[ "ingredient"] + "\n" ingredient_list.append(ingredient["ingredient"]) try: nutrition_dict = request_nutrition(food_recipe, food_dict['number_of_servings']) except: nutrition_dict = request_nutrition(food_recipe) food_dict['food_recipe'] = food_recipe is_success = False reason = "" if nutrition_dict is not None: # insert food new_food_id = db_insert_food(food_dict, nutrition_dict, ingredient_list) if new_food_id: # add tags tag_dict = {} tag_dict['generic_id'] = new_food_id tag_dict["type"] = "Food" tag_list = json.loads(food_dict['food_tags']) for tag_item in tag_list: tag_dict['tag_name'] = tag_item['tag_name'] tag_dict['tag_id'] = tag_item['tag_id'] tag_dict['tag_label'] = tag_item['tag_label'] tag_dict['tag_description'] = tag_item['tag_description'] db_insert_tag(tag_dict) #print(req.session['username'] + " has added a food successfully.") is_success = True else: reason = 'Adding food failed.' else: reason = 'Nutritional value calculation failed.' return HttpResponse(json.dumps({ 'is_success': is_success, 'reason': reason }), content_type='application/json')
def add_food(req): user_id = 0 if req.session.has_key('user_id'): user_id = req.session['user_id'] else: user_id = req.POST.dict()['user_id'] food_dict = req.POST.dict() food_dict['food_owner'] = user_id # get nutrition values from api ingredients = json.loads(food_dict['ingredients']) food_recipe = "" ingredient_list = [] for ingredient in ingredients: food_recipe += ingredient["value"] + " " + ingredient["ingredient"] + "\n" ingredient_list.append(ingredient["ingredient"]) try: nutrition_dict = request_nutrition(food_recipe, food_dict['number_of_servings']) except: nutrition_dict = request_nutrition(food_recipe) food_dict['food_recipe'] = food_recipe is_success = False reason = "" if nutrition_dict is not None: # insert food new_food_id = db_insert_food(food_dict, nutrition_dict, ingredient_list) if new_food_id: # add tags tag_dict = {} tag_dict['generic_id'] = new_food_id tag_dict["type"] = "Food" tag_list = json.loads(food_dict['food_tags']) for tag_item in tag_list: tag_dict['tag_name'] = tag_item['tag_name'] tag_dict['tag_id'] = tag_item['tag_id'] tag_dict['tag_label'] = tag_item['tag_label'] tag_dict['tag_description'] = tag_item['tag_description'] db_insert_tag(tag_dict) #print(req.session['username'] + " has added a food successfully.") is_success = True else: reason = 'Adding food failed.' else: reason = 'Nutritional value calculation failed.' return HttpResponse(json.dumps({'is_success': is_success, 'reason': reason}), content_type='application/json')
def get_nutritional_values(req): ingredients = json.loads(req.POST.dict()['ingredients']) food_recipe = "" for ingredient in ingredients: food_recipe += ingredient["value"] + " " + ingredient["ingredient"] + "\n" nutrition_dict = request_nutrition(food_recipe) return HttpResponse(json.dumps(nutrition_dict), content_type='application/json')
def get_nutritional_values(req): ingredients = json.loads(req.POST.dict()['ingredients']) food_recipe = "" for ingredient in ingredients: food_recipe += ingredient["value"] + " " + ingredient[ "ingredient"] + "\n" nutrition_dict = request_nutrition(food_recipe) return HttpResponse(json.dumps(nutrition_dict), content_type='application/json')