def _modify_recipe(request, recipe=None): if request.method == _HTTP_METHOD_POST: form = RecipeForm(request.POST, instance=recipe) if form.is_valid(): recipe = form.save() messages.success(request, "Your recipe has been saved successfully") return redirect(recipe) else: if recipe is None and _IMPORTED_DATA_KEY in request.session: initial_data = request.session.pop(_IMPORTED_DATA_KEY) raw_ingredients = initial_data.pop("ingredients", "") form_recipe = Recipe(**initial_data) form_recipe.raw_ingredients = raw_ingredients form = RecipeForm(instance=form_recipe) else: form = RecipeForm(instance=recipe) if recipe: back_link = recipe.get_absolute_url() else: back_link = reverse("recipe_listing") context = {"form": form, "recipe": recipe, "back_link": back_link} return TemplateResponse( request, "recipes/recipe_form.html", context)
def test_set_all_ingredients(self): recipe = Recipe() recipe.ingredients = [ {"heading": False, "text": "Sugar"}, {"heading": False, "text": "Butter"}, {"heading": False, "text": "Milk"}, ] self.assertEqual("Sugar\nButter\nMilk", recipe.raw_ingredients)
def test_set_with_heading_marker_in_text(self): recipe = Recipe() recipe.ingredients = [ {"heading": False, "text": "Sugar|Treacle"}, {"heading": False, "text": "Butter"}, {"heading": False, "text": "Milk"}, ] self.assertEqual("Sugar|Treacle\nButter\nMilk", recipe.raw_ingredients)
def test_set_heading(self): recipe = Recipe() recipe.ingredients = [ {"heading": True, "text": "For the batter"}, {"heading": False, "text": "Butter"}, {"heading": False, "text": "Milk"}, {"heading": True, "text": "Another"}, ] self.assertEqual( "|For the batter\nButter\nMilk\n|Another", recipe.raw_ingredients)
def test_set_empty(self): recipe = Recipe() recipe.ingredients = [] self.assertEqual("", recipe.raw_ingredients)