def create_test_ingredient(): """ :return: """ now = timezone.now() ingredient = Ingredient(name='Mandarine', type='FT', created=now, modified=now, user_mod="cooking_user_test") ingredient.save() return ingredient
def add_modify(request): """ Perform the update/creation of an ingredient with information in POST :param request: the http request :return:the ingredient view filled with the created/updated ingredient """ ingredient = Ingredient() if ('add' == request.POST['action'] and request.user.has_perm('cooking.add_ingredient')) or ( 'modify' == request.POST['action'] and request.user.has_perm('cooking.change_ingredient')): if 'add' == request.POST['action']: ingredient.created = timezone.now() else: ingredient = get_object_or_404(Ingredient, pk=request.POST['id']) ingredient.name = request.POST['name'] ingredient.type = request.POST['type'] ingredient.modified = timezone.now() ingredient.user_mod = request.user.username ingredient.save() recipes = set(IngredientRecipe.objects.filter(ingredient=ingredient).order_by('-recipe__name')) context = { 'ingredient': ingredient, 'recipes': recipes, } return render(request, 'ingredient.html', context) else: context = { 'latest_ingredients': Ingredient.objects.order_by('-created')[:10], 'error_messages': (_('Insufficient privileges'),), } return render(request, 'ingredients.html', context)
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)