Пример #1
0
def add_recipe():
    form = AddRecipeForm()
    if form.validate_on_submit():
        #TODO create formtodict function ? then recipe = Recipe(**d)
        recipe = Recipe(
            name=form.name.data,
            steps=form.steps.data,
            servings=form.servings.data,
            upcoming_servings=form.servings.data,
            cooking_temperature=form.cooking_temperature.data,
            cooking_time=form.cooking_time.data,
            prep_time=form.prep_time.data,
        )
        ingredients = [
            ing for ing in form.ingredients.data
            if ing["ing_name"] and ing["quantity"] and ing["unit"]
        ]
        # This key name conversion is needed because I cannot have a 'name'
        # attribute in IngredientForm, because I'm using it in a FieldList,
        # and WTForms already adds a name field in FieldList
        [ing.update({"name": ing.pop("ing_name")}) for ing in ingredients]

        recipe.add_ingredients(ingredients)
        recipe.add_tags()
        db.session.add(recipe)
        db.session.commit()
        flash(f"Recipe '{form.name.data}' added")
        return redirect(url_for("main.index"))
    return render_template("add_recipe.html", title="New recipe", form=form)
Пример #2
0
def test_recipe_todict(set_db):
    ingredients = [{
        "name": "eau",
        "quantity": 1,
        "unit": "L"
    }, {
        "name": "sel",
        "quantity": 10,
        "unit": "g"
    }]

    recipe_creation_dict = {
        'name': 'eau salée',
        'id': 1,
        'steps': "test steps",
        'upcoming': True,
        'upcoming_servings': 5,
        'servings': 2,
        'cooking_temperature': 150,
        'prep_time': 20,
        'cooking_time': 40,
    }

    recipe_dict = recipe_creation_dict.copy()
    recipe_dict["ingredients"] = ingredients

    recipe = Recipe(**recipe_creation_dict)
    recipe.add_ingredients(ingredients)
    recipe.add_tags()

    db.session.add(recipe)
    db.session.commit()

    assert recipe.to_dict() == recipe_dict
Пример #3
0
def test_recipe_add_tags(set_db):
    #TODO warning here when we add "eau" for the second time,
    # when adding the quantity in add_ingredients. Try to fix that ?
    ingredients_eausel = [{
        "name": "eau",
        "quantity": 1,
        "unit": "L"
    }, {
        "name": "sel",
        "quantity": 10,
        "unit": "g"
    }]

    ingredients_eaupoivre = [{
        "name": "eau",
        "quantity": 2,
        "unit": "mL"
    }, {
        "name": "poivre",
        "quantity": 10,
        "unit": "g"
    }]

    recipe_eausel = Recipe(name="eau salée")
    recipe_eausel.add_ingredients(ingredients_eausel)
    recipe_eausel.add_tags()

    db.session.add(recipe_eausel)
    db.session.commit()

    recipe_eaupoivre = Recipe(name="eau poivrée")
    recipe_eaupoivre.add_ingredients(ingredients_eaupoivre)
    recipe_eaupoivre.add_tags()

    db.session.add(recipe_eaupoivre)
    db.session.commit()

    expected_tags = {
        unidecode(ingredient["name"]).lower()
        for ingredient in chain(ingredients_eausel, ingredients_eaupoivre)
    }
    expected_tags.update(
        unidecode(elem).lower() for elem in recipe_eausel.name.split())
    expected_tags.update(
        unidecode(elem).lower() for elem in recipe_eaupoivre.name.split())
    assert expected_tags == set(tag.name for tag in Tag.query)
    assert len(Tag.query.filter_by(name="eau").first().recipes) == 2
    assert len(Tag.query.filter_by(name="sel").first().recipes) == 1
    assert len(Tag.query.filter_by(name="salee").first().recipes) == 1