def create_recipe():
    if request.method == 'POST':
        name = request.form.get('name')
        ingredients = request.form.get('ingredients')

    stored_ingredients = get_ingredients()
    return render_template('recipe/create-recipe/create-recipe.html',
                           data={'stored_ingredients': stored_ingredients})
Exemple #2
0
def index():
    try:
        ingredients = get_ingredients().json['ingredients']
        recipes = get_recipes().json['recipes']
    except Exception as error:
        print('error', error)
        ingredients = []
        recipes = []
    data = {'ingredients': ingredients, 'recipes': recipes}
    return render_template('dashboard/dashboard.html', title='Dashboard', data=data)
def create_recipe():
    if request.json:
        try:
            db = SQLAlchemy(current_app)
            obj = request.json

            name = obj['name']
            steps = obj['steps']
            ingredient_ids = obj['ingredients']

            ingredients = list(
                map(lambda x: IngredientModel.query.filter_by(id=x).first(),
                    ingredient_ids))

            recipe = RecipeModel(name=name,
                                 steps=steps,
                                 ingredients=ingredients)

            with current_app.app_context():
                db.session.merge(recipe)
                db.session.commit()

            res = json.dumps({
                'message': 'Receita cadastrada!',
                'error': False
            })
            return Response(res, mimetype='application/json', status=200)

        except Exception as error:
            res = json.dumps({'message': str(error), 'error': True})
            return Response(res, mimetype='application/json', status=200)
    else:
        try:
            stored_ingredients = get_ingredients().json['ingredients']
        except Exception as error:
            print(error)
            stored_ingredients = []
        return render_template('recipe/create-recipe/create-recipe.html',
                               data={'stored_ingredients': stored_ingredients})