Exemplo n.º 1
0
def add_ingredient_to_recipe(request):
    """
    Add an ingredient to a recipe
    :param request: the http request
    :return: The recipe view updated
    """
    ingredient = get_object_or_404(Ingredient, pk=request.POST['ingredient_id'])
    ir = IngredientRecipe()
    ir.ingredient = ingredient
    if request.POST['recipe_id']:
        recipe = get_object_or_404(Recipe, pk=request.POST['recipe_id'])
    else:
        recipe = Recipe()
    ir.recipe = recipe
    ir.quantity = float(request.POST['quantity'])
    ir.measurement = request.POST['measurement']
    ir.save()
    context = {
        'recipe': recipe,
        'action': request.POST['action'],
        'ingredients': IngredientRecipe.objects.filter(recipe=recipe),
        'all_ingredients': Ingredient.objects.all().order_by('-name'),
        'all_measurements': dict((key, _(value)) for (key, value) in MEASUREMENT),
    }
    return render(request, 'recipe_form.html', context)
 def test_create_ingredient_recipe(self):
     """
     Create an ingredientrecipe and test if the relations with recipe and ingredient is correct
     :return:
     """
     recipe, ingredient = util.create_recipe_and_ingredient()
     ingredient_recipe = IngredientRecipe()
     ingredient_recipe.recipe = recipe
     ingredient_recipe.ingredient = ingredient
     ingredient_recipe.quantity=500
     ingredient_recipe.measurement='GR'
     ingredient_recipe.save()
     self.assertTrue(ingredient_recipe.recipe.id == recipe.id)
     self.assertTrue(ingredient_recipe.ingredient.id == ingredient.id)
 def test_update_ingredient_recipe(self):
     """
     Create another ingredient , change the ingredient from a recipe with the new one , test the relations
     :return:
     """
     now = timezone.now()
     recipe, ingredient = util.create_recipe_and_ingredient()
     ingredient_recipe = IngredientRecipe()
     ingredient_recipe.recipe = recipe
     ingredient_recipe.ingredient = ingredient
     ingredient_recipe.quantity=500
     ingredient_recipe.measurement='GR'
     ingredient_recipe.save()
     self.assertTrue(ingredient_recipe.ingredient.id == ingredient.id)
     ingredient_2 = Ingredient(name='Pomme', type='FT', created=now, modified=now, user_mod="cooking_user_test")
     ingredient_2.save()
     self.assertTrue(ingredient_2.id is not None)
     ingredient_recipe.ingredient = ingredient_2
     ingredient_recipe.save()
     self.assertFalse(ingredient_recipe.ingredient.id == ingredient.id)
     self.assertTrue(ingredient_recipe.ingredient.id == ingredient_2.id)
 def test_delete_ingredient_recipe(self):
     with self.assertRaises(ObjectDoesNotExist):
         """
         delete the relation between recipe and ingredient , and test the deletion
         :return:
         """
         recipe, ingredient = util.create_recipe_and_ingredient()
         ingredient_recipe = IngredientRecipe()
         ingredient_recipe.recipe = recipe
         ingredient_recipe.ingredient = ingredient
         ingredient_recipe.quantity=500
         ingredient_recipe.measurement='GR'
         ingredient_recipe.save()
         self.assertTrue(ingredient_recipe.ingredient.id == ingredient.id and
                         ingredient_recipe.recipe.id == recipe.id)
         ingredient_from_rel = IngredientRecipe.objects.get(ingredient = ingredient).ingredient
         recipe_from_db = IngredientRecipe.objects.get(recipe=recipe).recipe
         self.assertTrue(ingredient_from_rel.id is not None and
                         recipe_from_db.id is not None)
         ingredient_recipe.delete()
         IngredientRecipe.objects.get(recipe=recipe_from_db)
         IngredientRecipe.objects.get(ingredient=ingredient_from_rel)