def addrecipe(request): if request.method == 'POST': form = RecipeForm(request.POST) if form.is_valid(): data = form.cleaned_data r = Recipe() r.name = data['name'] r.servings = data['servings'] r.ingredients = data['ingredients'] r.instructions = data['instructions'] r.save() return redirect(recipe_list) else: form = RecipeForm() return render(request, 'addrecipe.html', {'form': form})
def test_redirects_after_save(self): user = User() user.name = 'ben' user.save() recipe = Recipe() recipe.title = 'cacio e pepe' recipe.url_name = 'cacio-e-pepe' recipe.ingredients = 'kosher salt\n6 oz. pasta \n3 Tbsp. unsalted butter\n 1 tsp. freshly cracked black pepper' recipe.directions = 'bring water to a boil\ncook pasta\nadd butter and pepper' recipe.servings = '4' recipe.user = user recipe.save() response = self.client.post('/users/%s/recipe/%s/edit' % (user.name, recipe.url_name), data={'title': 'Cacio e Pepe'}) self.assertRedirects(response, '/users/%s/recipe/%s' % (user.name, recipe.url_name))
def edit(request): """Create a form that can be used to add a new recipe. Save data submitted through the form to the database as a new recipe. """ if request.method == 'POST': form = RecipeForm(request.POST) if form.is_valid(): data = form.cleaned_data r = Recipe() r.name = data['name'] r.servings = data['servings'] r.ingredients = data['ingredients'] r.instructions = data['instructions'] r.save() return redirect(recipe_list) else: form = RecipeForm() return render(request, 'edit.html', { 'form': form})
def test_save_a_post_request_for_an_existing_recipe(self): user = User() user.name = 'ben' user.save() recipe = Recipe() recipe.title = 'cacio e pepe' recipe.url_name = 'cacio-e-pepe' recipe.ingredients = 'kosher salt\n6 oz. pasta \n3 Tbsp. unsalted butter\n 1 tsp. freshly cracked black pepper' recipe.directions = 'bring water to a boil\ncook pasta\nadd butter and pepper' recipe.servings = '4' recipe.user = user recipe.save() self.client.post('/users/%s/recipe/%s/edit' % (user.name, recipe.url_name), data={'title': 'Cacio e Pepe'}) edited_recipe = Recipe.objects.first() self.assertEqual(edited_recipe.title, 'Cacio e Pepe')
def addrecipe(request): """Create a form that can be used to add a new recipe. Save data submitted through the form to the database as a new recipe. """ if request.method == 'POST': form = RecipeForm(request.POST) if form.is_valid(): data = form.cleaned_data r = Recipe() r.name = data['name'] r.servings = data['servings'] r.description = data['description'] r.ingredients = data['ingredients'] r.instructions = data['instructions'] r.save() return redirect(recipe_list) else: form = RecipeForm() return render(request, 'addrecipe.html', {'form': form})
def post(self, request, *args, **kwargs): form = self.form_class(request.POST, request.FILES) if form.is_valid(): if request.user is not None: recipe = Recipe() recipe.title = form.cleaned_data['title'] recipe.ingredients = form.cleaned_data['ingredients'] recipe.text = form.cleaned_data['text'] recipe.owner = User.objects.get(username=request.user) recipe.is_published = True recipe.pic = form.cleaned_data['pic'] recipe.save() try: form.save() except Exception: pass # profile = Profile.objects.filter(user=User.objects.get(username=request.user)) # profile.update(avatar=form.cleaned_data['avatar']) return super(RecipeCreateView, self).form_valid(form) else: return self.form_invalid(form)
plats = Category(name="Plats", description="Les plats...") plats.save() entrees = Category(name="Entrées", description="Les entrées...") entrees.save() ### # Recipes ### recipe = Recipe(title="Cake Olives-Jambon", author=admin, preparation_time="20min", portion="5-6") save_file(recipe.picture, 'test/cake jambon olives.jpg') recipe.ingredients = u"""- vin blanc sec: 15cL - huile d'olive: 15cL - oeufs: 4 - gruyère rapé: 100g - farine: 250g - levure: 1 paquet - sel: 1 c. à café - dés de jambon: 200g - olives vertes: 200g""" recipe.content = u"""- Dans un saladier, travailler le vin, l'huile et les oeufs cassés un par un. - Ajouter la farine, le gruyère rapé, la levure et sel. Terminer par le jambon et les olives coupées en 2. - Faire cuire dans un moule à cake beurré et fariné.""" recipe.category = plats recipe.cost = 1 recipe.difficulty = 1 recipe.save()
def __init__(self, recipe: Recipe, team:Team): self.recipe = recipe self.can_perform = recipe.can_perform(team) (self.needs, self.creates, self.consumes) = recipe.ingredients()