def try_remove_ingredient(sandwich_id, ingredient_id): sandwich = get_by_id(sandwich_id) ingredient = Ingredient.get_by_id(ingredient_id) if sandwich and ingredient: models.SandwichIngredient.objects.get(ingredient=ingredient, sandwich=sandwich).delete()
def setUp(self): ingredient = Ingredient.create("Pão") Ingredient.set_value(ingredient, 1.25) ingredient = Ingredient.create("Hamburger") Ingredient.set_value(ingredient, 1) ingredient = Ingredient.create("Bacon") Ingredient.set_value(ingredient, 2) name = "X-Bacon" list = ["Pão", "Hamburger", "Bacon"] profit = 5.5 sandwich = Sandwich.create(name, profit, list) beverage = Beverage.create("Coca-cola", "2 Litros", 6)
def create(request): dados = { 'title': 'Criação de lanches', 'header': 'Novo lanche', 'icon': 'fas fa-hamburger', 'ingredients': Ingredient.get_all_with_price() } return render(request, 'sandwich/create.html', dados)
def edit(request, id): if id: dados = { 'title': 'Atualizar ingrediente', 'header': 'Atualizar ingrediente', 'icon': 'fas fa-bacon', 'ingredient': Ingredient.get_info_by_id(id) } return render(request, 'ingredient/create.html', dados) else: messages.error(request, "Erro no envio")
def filter(search): sandwichs = list(models.Sandwich.objects.filter(name__contains=search)) ingredients = Ingredient.filter(search) for ingredient in ingredients: itens = models.SandwichIngredient.objects.filter(ingredient=ingredient) for item in itens: if item.sandwich not in sandwichs: sandwichs.append(item.sandwich) return sandwichs
def edit(request, id): if id: dados = { 'title': 'Editar lanche', 'header': 'Editar lanche', 'icon': 'fas fa-hamburger', 'sandwich': Sandwich.get_by_id(id), 'ingredients': Ingredient.get_all_with_price() } return render(request, 'sandwich/create.html', dados) else: messages.error(request, "ID nao encontrado") return redirect('/sandwich/list')
def filter_submit(request): if request.GET: name = request.GET.get("name") if name: ingredients = Ingredient.filter(name) if len(ingredients) > 0: return return_list(request, ingredients) else: messages.error(request, 'Ingrediente nao encontrado') else: messages.error(request, 'Nome não pode estar em branco') else: messages.error(request, 'Erro durante a solicitação') return redirect('/ingredient/filter')
def create_submit(request): if request.POST: name = request.POST.get("name") price = request.POST.get("price") if name: if price: ingredient = Ingredient.get_by_name(name) if ingredient is None: create_execute(name, price) else: messages.error(request, 'Ingrediente com mesmo nome ja cadastrado') else: messages.error(request, 'Preço não pode estar em branco') else: messages.error(request, 'Nome não pode estar em branco') else: messages.error(request, 'Erro durante envio') return redirect('/ingredient/menu')
def edit_execute(id, name, price): Ingredient.update(id, name, price)
def test_get_value_ingredient_all(self): ingredients = Ingredient.get_all_with_price() self.assertEqual(ingredients[0].price.value, 2) self.assertEqual(ingredients[1].price.value, 1)
def test_get_value_ingredient(self): ingredient = Ingredient.get_info_by_id(1) self.assertEqual(ingredient.price.value, 1.25)
def test_set_value_ingredient_id_2(self): ingredient = Ingredient.get_by_id(2) ingredient_value = Ingredient.set_value(ingredient, 1.25) self.assertEqual(ingredient_value.value, 1.25)
def test_create_ingredient(self): ingredient = Ingredient.create("Alface") self.assertEqual(ingredient.id, 4)
def sandwich_recipe(sandwich, ingredient_name): ingredient = Ingredient.get_by_name(ingredient_name) if ingredient: models.SandwichIngredient.objects.create(sandwich=sandwich, ingredient=ingredient)
def test_get_ingredient_by_id(self): ingredient = Ingredient.get_by_id(1) self.assertEqual(ingredient.name, "Pão")
def test_filter_ingredient(self): ingredients = Ingredient.filter('Pã') self.assertNotEqual(len(ingredients),0)
def test_get_all_ingredient(self): ingredients = Ingredient.get_all() self.assertIsNotNone(ingredients)
def test_get_ingredient_by_name(self): ingredient = Ingredient.get_by_name('Hamburger') self.assertEqual(ingredient.id, 2)
def create_execute(name, price): ingredient = Ingredient.create(name) Ingredient.set_value(ingredient, price)
def list(request): return return_list(request, Ingredient.get_all())
def test_get_ingredient_update(self): Ingredient.update(2, "Bacon", 2.45) ingredient = Ingredient.get_info_by_id(2) ingredient.price.value = float(ingredient.price.value) self.assertEqual(ingredient.price.value, 2.45)
def test_get_ingredient_update_name(self): Ingredient.update_name(2, "Bacon") ingredient = Ingredient.get_by_id(2) self.assertEqual(ingredient.name, "Bacon")
def test_set_value_ingredient_id_1(self): ingredient = Ingredient.get_by_id(1) ingredient_value = Ingredient.set_value(ingredient, 1.50) self.assertEqual(ingredient_value.value, 1.5)