Ejemplo n.º 1
0
def edit_recipe(recipe_id):
    if not ObjectId.is_valid(recipe_id):
        return render_template('recipe_error.html',
                               message="No recipe found",
                               message_class='warning',
                               btn_class='red darken-4')
    recipe = recipe_model.get_edit_data(ObjectId(recipe_id),
                                        session['user']['username'])
    if recipe is False:
        return render_template('recipe_error.html',
                               message="You can't edit someone recipe",
                               message_class='error',
                               btn_class='')
    init_form = RecipeForm(recipe, request.form)
    if request.method == 'POST' and init_form.validate():
        data = RecipeForm(request.form)
        recipe_model.update(data, ObjectId(recipe_id))
        flash('Recipe has been edited', 'success')
        return redirect(url_for('view_recipe', recipe_id=recipe_id))

    return render_template('recipe_edit.html',
                           form=init_form,
                           form_action=url_for('edit_recipe',
                                               recipe_id=recipe_id),
                           recipe_id=recipe_id,
                           title='Edit recipe',
                           body_class='edit_recipe')
Ejemplo n.º 2
0
def new_recipe():
    form = RecipeForm(request.form)
    if request.method == 'POST' and form.validate():
        added_id = recipe_model.add(form, session['user']['username'])
        flash('Recipe has been added', 'success')
        return redirect(url_for('view_recipe', recipe_id=added_id))
    return render_template('recipe_edit.html',
                           form=form,
                           form_action=url_for('new_recipe'),
                           title='Add recipe',
                           body_class='edit_recipe')
 def test_recipe(self):
     data = [
         ('title', 'Test recipe'),
         ('introduction', None),
         ('method-0', 'Method'),
         ('ingredients-0', 'Ingredient'),
         ('categories-0', None),
         ('cuisines-0', None),
         ('allegrens-0', None)
     ]
     form = RecipeForm(MultiDict(data))
     self.assertTrue(form.validate())
     self.assertEqual(form.title.data, 'Test recipe')
Ejemplo n.º 4
0
def edit_recipe(recipe_id):
    form = RecipeForm(request.form)
    recipe = Recipe.query.filter_by(id=recipe_id).first_or_404()
    if request.method == 'POST' and form.validate():
        recipe.title = form.title.data
        recipe.ingredients = form.ingredients.data
        recipe.time_needed = form.time_needed.data
        recipe.steps = form.steps.data
        recipe.status = form.status.data
        db.session.commit()
        flash('Recipe edited successfully', 'success')
        return redirect(url_for('my_recipes'))
    # I need to set the values for ingredients and steps, because they're from textarea, and that
    # doesn't support value for field.
    form.ingredients.data = recipe.ingredients
    form.steps.data = recipe.steps
    return render_template('recipes/edit.html', form=form, recipe=recipe)
Ejemplo n.º 5
0
def new_recipe():
    form = RecipeForm(request.form)
    if request.method == 'GET':
        return render_template('recipes/new.html', form=form)
    if form.validate():
        # https://stackoverflow.com/questions/33429510/wtforms-selectfield-not-properly-coercing-for-booleans f**k
        if form.status.data == '':
            form.status.data = False
        recipe = Recipe(title=form.title.data,
                        ingredients=form.ingredients.data,
                        time_needed=form.time_needed.data,
                        steps=form.steps.data,
                        status=form.status.data,
                        user_id=current_user.id)
        db.session.add(recipe)
        db.session.commit()
        flash('Recipe added successfully', 'success')
        return redirect(url_for('show_recipe', recipe_id=recipe.id))

    flash('There are some problems here', 'danger')
    return render_template('recipes/new.html', form=form)
Ejemplo n.º 6
0
def add_recipe():
    form = RecipeForm()
    if request.method == 'POST' and form.validate():
        # Add recipe to database and create ingredient links
        recipe = Recipe(name=form.name.data, type=form.type.data,
                        time=form.time.data, category=form.category.data,
                        serves=form.serves.data, method=form.method.data)
        db.session.add(recipe)
        # Create links between recipe and all ingredients required
        for ingredient in session['ingredients']:
            find_ingredient = Ingredient.query.filter_by(name=ingredient[0]).first()
            ing_recipe_link = RI_Link(r_id=recipe.id, i_id=find_ingredient.id,
                                      quantity=ingredient[1], q_type=ingredient[2])
            recipe.ingredients.append(ing_recipe_link)
            find_ingredient.recipes.append(ing_recipe_link)
        # Add calorie value for the recipe
        calories = 0
        for ingredient in session['ingredients']:
            find_ing = Ingredient.query.filter_by(name=ingredient[0]).first()
            if ingredient[2] == 'g':
                calories += int(ingredient[1])*int(find_ing.calories_per_g)
            elif ingredient[2] == 'mL':
                calories += int(ingredient[1])*int(find_ing.calories_per_ml)
            elif ingredient[2] == 'tbs':
                calories += int(ingredient[1])*int(find_ing.calories_per_tbs)
            elif ingredient[2] == 'tsp':
                calories += int(ingredient[1])*int(find_ing.calories_per_tsp)
            elif ingredient[2] == 'each':
                calories += int(ingredient[1])*int(find_ing.calories_per_each)
        recipe.calories = int(calories/(int(recipe.serves)))
        db.session.commit()
        return redirect(url_for('index'))
    session['ingredients'] = []
    session.modified = True
    form = RecipeForm()
    return render_template('add-recipe.html', title= 'Add Recipe', form = form)