def add_many_ingredients_to_section(section): while True: name = raw_input('name :') if name == "": break unit = raw_input('unit :') i = Ingredient(name=name, unit=unit, is_food=True, section=section) print i.name, i.unit, 'added to', i.section i.save()
def get_html(base, level): # Base case if level < 0 or base is None or not (validators.url(base)): return r = requests.get(base) soup = BeautifulSoup(r.text, 'html.parser') print base # Do any parsing on current url parent_url = url_in_rules(base) if parent_url is not None: ingredients = filter_ingredients(parent_url, soup) title = get_title_from_page(parent_url, soup) image_url = get_image_from_page(parent_url, soup) if not (title is None or image_url is None): """ Interesting thing I learned about get_or_create vs catching Exceptions: "it's easier to ask for forgiveness than for permission". """ try: recipe = Recipe(title=title, image_url=image_url, recipe_url=base) recipe.save() print "Adding recipe: {}".format(title) except IntegrityError: recipe = Recipe.objects.filter(title=title).first() for i in ingredients: try: ingredient = Ingredient(title=i, recipe=recipe) ingredient.save() print "{} to {}".format(i, recipe.title) except IntegrityError: continue # Now loop through all linked pages on the page and get their content too for link in soup.find_all('a'): page_url = link.get('href') if page_url is None or page_url == '' or page_url == '/' or page_url == base or ( validators.url(page_url) and url_in_rules(page_url) != base): continue else: if url_in_rules(page_url) != base: page_url = urljoin(base, page_url) get_html(page_url, level - 1)
def get_html(base, level): # Base case if level < 0 or base is None or not(validators.url(base)): return r = requests.get(base) soup = BeautifulSoup(r.text, 'html.parser') print base # Do any parsing on current url parent_url = url_in_rules(base) if parent_url is not None: ingredients = filter_ingredients(parent_url, soup) title = get_title_from_page(parent_url, soup) image_url = get_image_from_page(parent_url, soup) if not(title is None or image_url is None): """ Interesting thing I learned about get_or_create vs catching Exceptions: "it's easier to ask for forgiveness than for permission". """ try: recipe = Recipe(title=title, image_url=image_url, recipe_url=base) recipe.save() print "Adding recipe: {}".format(title) except IntegrityError: recipe = Recipe.objects.filter(title=title).first() for i in ingredients: try: ingredient = Ingredient(title=i,recipe=recipe) ingredient.save() print "{} to {}".format(i, recipe.title) except IntegrityError: continue # Now loop through all linked pages on the page and get their content too for link in soup.find_all('a'): page_url = link.get('href') if page_url is None or page_url == '' or page_url == '/' or page_url == base or (validators.url(page_url) and url_in_rules(page_url) != base): continue else: if url_in_rules(page_url) != base: page_url = urljoin(base, page_url) get_html(page_url, level - 1)
def init(request): drink1 = Drink(name='Mojito', description='Yum!', image='mojito.jpg') drink1.save() drink2 = Drink(name='Cuba Libre', description='Yuck!', image='cuba.jpg') drink2.save() i1 = Ingredient(name='Juice', description='So svalk', unit='kg') i1.save() i2 = Ingredient(name='Vatten', description='Such wet', unit='doge') i2.save() i3 = Ingredient(name='Vindruvor', description='Many rund', unit='pi') i3.save() i4 = Ingredient(name='Milk', description='Very ko', unit='mil-k') i4.save() Recipe(drink=drink1, ingredient=i1, amount=20).save() Recipe(drink=drink1, ingredient=i2, amount=20).save() Recipe(drink=drink1, ingredient=i4, amount=20, note="smaskens!").save() Recipe(drink=drink2, ingredient=i2, amount=20).save() Recipe(drink=drink2, ingredient=i4, amount=20, note="katt").save() return main(request)
def add_recipe(request): context = {} context['urlform'] = AddRecipebyURLForm() context['recipeform'] = RecipeForm() context['instructionform'] = InstructionsForm() context['ingredientsform'] = IngredientsForm() context['autocompleteform'] = AutocompleteSearchForm() context['profile'] = Profile.objects.get(user=request.user) if request.method == 'GET': return render(request, 'foodchaser/add_recipe.html', context) elif request.method != 'POST': return render(request, 'foodchaser/add_recipe.html', context) user = request.user recipeform = RecipeForm(request.POST, request.FILES) instructionform = InstructionsForm(request.POST, request.FILES) ingredientsform = IngredientsForm(request.POST) context['recipeform'] = recipeform context['instructionform'] = instructionform context['ingredientsform'] = ingredientsform # for debugging use, don't remove if not recipeform.is_valid() or not instructionform.is_valid() \ or not ingredientsform.is_valid(): return render(request, 'foodchaser/add_recipe.html', context) # Store ingredient name = ingredientsform.cleaned_data['name'] category = find_category(name) ingredient = Ingredient(name=name, allergy=category) ingredient.save() ingredientunit = IngredientUnit(name=ingredientsform.cleaned_data['name'],\ quantity = ingredientsform.cleaned_data['quantity'], \ unit = ingredientsform.cleaned_data['unit'], \ notes = ingredientsform.cleaned_data['notes'], \ ingredient = ingredient) ingredientunit.save() ingredientunit.ingreID = ingredientunit.id ingredientunit.save() # Store instructions step = Step(text = instructionform.cleaned_data['text'], \ preptime = instructionform.cleaned_data['preptime'], \ cooktime = instructionform.cleaned_data['cooktime'], \ picture = instructionform.cleaned_data['picture']) step.save() step.stepID = step.id step.save() recipe = Recipe( recipe_name = recipeform.cleaned_data['recipe_name'], \ description = recipeform.cleaned_data['description'], \ category1 = recipeform.cleaned_data['category1'], \ category2 = recipeform.cleaned_data['category2'], \ spicy = recipeform.cleaned_data['spicy'], \ estimated_time= recipeform.cleaned_data['estimated_time'],\ owner = user,\ photo = recipeform.cleaned_data['photo']) recipe.save() recipe.ingredients.add(ingredientunit) recipe.steps.add(step) # Add additional ingredients and steps # Parse additional steps i = 2 while (('text' + str(i)) in request.POST): request.POST['text'] = request.POST['text' + str(i)] request.POST['preptime'] = request.POST['preptime' + str(i)] request.POST['cooktime'] = request.POST['cooktime' + str(i)] if (('picture' + str(i)) in request.FILES): request.FILES['picture'] = request.FILES['picture' + str(i)] else: request.FILES['picture'] = None instructionf = InstructionsForm(request.POST, request.FILES) if not instructionf.is_valid(): return render(request, 'foodchaser/add_recipe.html', context) step = Step(text = instructionf.cleaned_data['text'], \ preptime = instructionf.cleaned_data['preptime'], \ cooktime = instructionf.cleaned_data['cooktime'], \ picture = instructionf.cleaned_data['picture']) step.save() step.stepID = step.id step.save() recipe.steps.add(step) i += 1 # Parse additional ingredients i = 2 while (('name' + str(i)) in request.POST): request.POST['name'] = request.POST['name' + str(i)] request.POST['quantity'] = request.POST['quantity' + str(i)] request.POST['unit'] = request.POST['unit' + str(i)] request.POST['notes'] = request.POST['notes' + str(i)] ingredientsf = IngredientsForm(request.POST) if not ingredientsf.is_valid(): return render(request, 'foodchaser/add_recipe.html', context) # Store ingredient name = ingredientsf.cleaned_data['name'] category = find_category(name) ingredient = Ingredient(name=name, allergy=category) ingredient.save() ingredientunit = IngredientUnit(name = ingredientsf.cleaned_data['name'],\ quantity = ingredientsf.cleaned_data['quantity'], \ unit = ingredientsf.cleaned_data['unit'], \ notes = ingredientsf.cleaned_data['notes'], \ ingredient = ingredient) ingredientunit.save() ingredientunit.ingreID = ingredientunit.id ingredientunit.save() recipe.ingredients.add(ingredientunit) i += 1 recipe.save() recipes = Recipe.objects.all().filter(owner=request.user) context['recipes'] = recipes context['user'] = user context['profile'] = Profile.objects.get(user=request.user) # Add scores on the user ranking userlevel = LevelData.objects.get(user=request.user) update_level_data(userlevel) return render(request, 'foodchaser/recipebox_maindish.html', context)
def edit(request, id): context = {} context['urlform'] = AddRecipebyURLForm() context['autocompleteform'] = AutocompleteSearchForm() recipe = get_object_or_404(Recipe, id=id) context['recipe'] = recipe context['user'] = request.user ingredients = recipe.ingredients.all() steps = recipe.steps.all() ingredientforms = [] stepforms = [] for ingredient in ingredients: ingredientforms.append(IngredientsForm(instance=ingredient)) for step in steps: stepforms.append(InstructionsForm(instance=step)) context['ingredientforms'] = ingredientforms context['stepforms'] = stepforms context['ingredients'] = ingredients context['steps'] = steps context['user'] = request.user context['rating'] = RatingForm() context['comment'] = CommentForm() context['comments'] = recipe.comments.all() context['profile'] = Profile.objects.get(user=request.user) # Just display the registration form if this is a GET request if request.method == 'GET': recipeform = RecipeForm(instance=recipe) # Creates form from the context['recipeform'] = recipeform # existing entry. return render(request, 'foodchaser/edit_recipe.html', context) # if method is POST, get form data to update the model rform = RecipeForm(request.POST, request.FILES, instance=recipe) if not rform.is_valid(): context['recipeform'] = rform return render(request, 'foodchaser/edit_recipe.html', context) rform.save() # Add additional ingredients and steps # Parse additional steps i = 1 while (('text' + str(i)) in request.POST): request.POST['text'] = request.POST['text' + str(i)] request.POST['preptime'] = request.POST['preptime' + str(i)] request.POST['cooktime'] = request.POST['cooktime' + str(i)] if ('stepID' + str(i) in request.POST): request.POST['stepID'] = request.POST['stepID' + str(i)] else: request.POST['stepID'] = '' if (('picture' + str(i)) in request.FILES): request.FILES['picture'] = request.FILES['picture' + str(i)] else: request.FILES['picture'] = None instructionf = InstructionsForm(request.POST, request.FILES) if not instructionf.is_valid(): return render(request, 'foodchaser/edit_recipe.html', context) # Store step if (not recipe.steps.all().filter( stepID=instructionf.cleaned_data['stepID'])): step = Step(text = instructionf.cleaned_data['text'], \ preptime = instructionf.cleaned_data['preptime'], \ cooktime = instructionf.cleaned_data['cooktime'], \ picture = instructionf.cleaned_data['picture']) step.save() step.stepID = step.id step.save() recipe.steps.add(step) i += 1 # Parse additional ingredients i = 1 while (('name' + str(i)) in request.POST): request.POST['name'] = request.POST['name' + str(i)] request.POST['quantity'] = request.POST['quantity' + str(i)] request.POST['unit'] = request.POST['unit' + str(i)] request.POST['notes'] = request.POST['notes' + str(i)] if ('ingreID' + str(i) in request.POST): request.POST['ingreID'] = request.POST['ingreID' + str(i)] else: request.POST['ingreID'] = '' ingredientsf = IngredientsForm(request.POST) if not ingredientsf.is_valid(): return render(request, 'foodchaser/edit_recipe.html', context) # Store ingredient if (not recipe.ingredients.all().filter( ingreID=ingredientsf.cleaned_data['ingreID'])): name = ingredientsf.cleaned_data['name'] category = find_category(name) ingredient = Ingredient(name=name, allergy=category) ingredient.save() ingredientunit = IngredientUnit(name = ingredientsf.cleaned_data['name'],\ quantity = ingredientsf.cleaned_data['quantity'], \ unit = ingredientsf.cleaned_data['unit'], \ notes = ingredientsf.cleaned_data['notes'], \ ingredient = ingredient) ingredientunit.save() ingredientunit.ingreID = ingredientunit.id ingredientunit.save() recipe.ingredients.add(ingredientunit) i += 1 ingredients = recipe.ingredients.all() steps = recipe.steps.all() context['ingredients'] = ingredients context['steps'] = steps recipes = Recipe.objects.all().filter(owner=request.user) context['recipes'] = recipes return render(request, 'foodchaser/recipe_view.html', context)
def add_recipe(request): context = {} context['urlform'] = AddRecipebyURLForm() context['recipe'] = RecipeForm() context['instruction'] = AddInstructionsForm() context['ingredients'] = AddIngredientsForm() context['autocompleteform'] = AutocompleteSearchForm() if request.method == 'GET': return render(request, 'foodchaser/add_recipe.html', context) user = request.user recipef = RecipeForm(request.POST, request.FILES) instructionf = AddInstructionsForm(request.POST, request.FILES) ingredientsf = AddIngredientsForm(request.POST) if not recipef.is_valid() or not instructionf.is_valid() or not \ ingredientsf.is_valid(): return render(request, 'foodchaser/add_recipe.html', context) # Store ingredient ingredient = Ingredient(name = ingredientsf.cleaned_data['item']) ingredient.save() ingredientunit = IngredientUnit(quantity = ingredientsf.cleaned_data['quantity'], \ unit = ingredientsf.cleaned_data['unit'], \ notes = ingredientsf.cleaned_data['notes'], \ ingredient = ingredient) ingredientunit.save() # Store instructions step = Step(description = instructionf.cleaned_data['des'], \ preptime = instructionf.cleaned_data['preptime'], \ cooktime = instructionf.cleaned_data['cooktime'], \ picture = instructionf.cleaned_data['stepPic']) step.save() recipe = Recipe(name = recipef.cleaned_data['name'], \ description = recipef.cleaned_data['description'], \ category1 = recipef.cleaned_data['category1'], \ category2 = recipef.cleaned_data['category2'], \ spicy = recipef.cleaned_data['spicy'], \ estimated_time= recipef.cleaned_data['estimated_time'],\ owner = user,\ picture = recipef.cleaned_data['picture']) recipe.save() recipe.ingredients.add(ingredientunit) recipe.steps.add(step) # Add additional ingredients and steps # Parse additional steps i = 2 while (('des'+str(i)) in request.POST): request.POST['des'] = request.POST['des'+str(i)] request.POST['preptime'] = request.POST['preptime'+str(i)] request.POST['cooktime'] = request.POST['cooktime'+str(i)] if (('stepPic'+str(i)) in request.FILES): request.FILES['stepPic'] = request.FILES['stepPic'+str(i)] else: request.FILES['stepPic'] = None instructionf = AddInstructionsForm(request.POST, request.FILES) if not instructionf.is_valid(): return render(request, 'foodchaser/add_recipe.html', context) step = Step(description = instructionf.cleaned_data['des'], \ preptime = instructionf.cleaned_data['preptime'], \ cooktime = instructionf.cleaned_data['cooktime'], \ picture = instructionf.cleaned_data['stepPic']) step.save() recipe.steps.add(step) i += 1 # Parse additional ingredients i = 2 while (('item'+str(i)) in request.POST): request.POST['item'] = request.POST['item'+str(i)] request.POST['quantity'] = request.POST['quantity'+str(i)] request.POST['unit'] = request.POST['unit'+str(i)] request.POST['notes'] = request.POST['notes'+str(i)] ingredientsf = AddIngredientsForm(request.POST) if not ingredientsf.is_valid(): return render(request, 'foodchaser/add_recipe.html', context) # Store ingredient ingredient = Ingredient(name = ingredientsf.cleaned_data['item']) ingredient.save() ingredientunit = IngredientUnit(quantity = ingredientsf.cleaned_data['quantity'], \ unit = ingredientsf.cleaned_data['unit'], \ notes = ingredientsf.cleaned_data['notes'], \ ingredient = ingredient) ingredientunit.save() recipe.ingredients.add(ingredientunit) i += 1 recipe.save() recipes = Recipe.objects.all().filter(owner = request.user) context['recipes'] = recipes context['user'] = user return render(request, 'foodchaser/recipebox_maindish.html', context)
def save_foods(foodstuffs, types, add_beans=False, restrictions=''): for food in foodstuffs: cal = 0 if 'Energy' in food.data: cal = food.data['Energy'][0] total_fat = 0 if 'Total lipid (fat)' in food.data: total_fat = food.data['Total lipid (fat)'][0] sat_fat = 0 if 'Fatty acids, total saturated' in food.data: sat_fat = food.data['Fatty acids, total saturated'][0] mono_fat = 0 if 'Fatty acids, total monounsaturated' in food.data: mono_fat = food.data['Fatty acids, total monounsaturated'][0] poly_fat = 0 if 'Fatty acids, total polyunsaturated' in food.data: poly_fat = food.data['Fatty acids, total polyunsaturated'][0] trans_fat = 0 if 'Fatty acids, total trans' in food.data: trans_fat = food.data['Fatty acids, total trans'][0] cholesterol = 0 if 'Cholesterol' in food.data: cholesterol = food.data['Cholesterol'][0] sodium = 0 if 'Sodium, Na' in food.data: sodium = food.data['Sodium, Na'][0] potassium = 0 if 'Potassium, K' in food.data: potassium = food.data['Potassium, K'][0] carbs = 0 if 'Carbohydrate, by difference' in food.data: carbs = food.data['Carbohydrate, by difference'][0] fiber = 0 if 'Fiber, total dietary' in food.data: fiber = food.data['Fiber, total dietary'][0] sugar = 0 if 'Sugars, total' in food.data: sugar = food.data['Sugars, total'][0] protein = 0 if 'Protein' in food.data: protein = food.data['Protein'][0] vit_a = 0 if 'Vitamin A, RAE' in food.data: vit_a = food.data['Vitamin A, RAE'][0] vit_c = 0 if 'Vitamin C, total ascorbic acid' in food.data: vit_c = food.data['Vitamin C, total ascorbic acid'][0] calcium = 0 if 'Calcium, Ca' in food.data: calcium = food.data['Calcium, Ca'][0] iron = 0 if 'Iron, Fe' in food.data: iron = food.data['Iron, Fe'][0] vit_d = 0 if 'Vitamin D (D2 + D3)' in food.data: vit_d = food.data['Vitamin D (D2 + D3)'][0] vit_b_6 = 0 if 'Vitamin B-6' in food.data: vit_b = food.data['Vitamin B-6'][0] vit_b_12 = 0 if 'Vitamin B-12' in food.data: vit_b_12 = food.data['Vitamin B-12'][0] magnesium = 0 if 'Magnesium, Mg' in food.data: magnesium = food.data['Magnesium, Mg'][0] if add_beans: food.name = food.name + ' beans' new_food = Ingredient(name=food.name, modifier=food.modifier, calories=cal, total_fat=total_fat, saturated_fat=sat_fat, ingredient_type=IngredientType.objects.get(name=types),polyunsaturated_fat=poly_fat, monounsaturated_fat=mono_fat, trans_fat=trans_fat, cholesterol=cholesterol, sodium = sodium, dietary_fiber = fiber, potassium = potassium, total_carbohydrates=carbs, sugar=sugar, protein=protein, vitamin_a=vit_a, vitamin_c=vit_c, calcium=calcium, iron=iron, vitamin_d=vit_d, vitamin_b_6=vit_b_6, vitamin_b_12=vit_b_12 ,magnesium=magnesium, restrictions=restrictions) new_food.save() for unit in food.units: new_unit = ServingSize(name=unit[0], gram_conversion=unit[1], ingredients = new_food) new_unit.save()