Example #1
0
def create_recipe():
    categories = [str(c.name) for c in Category.load_unique_categories()]
    ingredients = [str(i.name.encode('utf-8')) for i in Ingredient.load_unique_ingredients()]

    if request.method == 'GET':
        recipe = Recipe()
        
        # TESTING FORM REFILL
        # recipe.name = 'name'
        # recipe.servings = 'servings'
        # recipe.preparation_time = 'prep_time'
        # recipe.nutritional_info = 'nut_info'
        # recipe.categories_string = 'cat1, cat2'
        # recipe.ingredients = []
        # ingredient = Ingredient()
        # ingredient.name = 'ing1-name'
        # ingredient.quantity = 'ing1-quantity'
        # ingredient.unit = 'ing1-unit'
        # ingredient.comment = 'ing1-comment'
        # recipe.ingredients.append(ingredient)
        # ingredient = Ingredient()
        # ingredient.name = 'ing2-name'
        # ingredient.quantity = 'ing2-quantity'
        # ingredient.unit = 'ing2-unit'
        # ingredient.comment = 'ing2-comment'
        # recipe.ingredients.append(ingredient)
        # recipe.steps = []
        # step = Step()
        # step.number = 1
        # step.instructions = 'inst1'
        # recipe.steps.append(step)
        # step = Step()
        # step.number = 2
        # step.instructions = 'inst2'
        # recipe.steps.append(step)

        return render_template('create_recipe.html', categories=categories, ingredients=ingredients, recipe=recipe)

    elif request.method == 'POST':
        f = request.form

        recipe = Recipe()
        recipe.name = f['recipe_name'].strip()
        recipe.servings = f['recipe_servings'].strip()
        recipe.preparation_time = f['recipe_preparation_time'].strip()
        recipe.nutritional_info = f['recipe_nutritional_info'].strip()
        recipe.creator_id = g.current_user.id

        # file
        recipe.upload_file = request.files['recipe_image']

        recipe.category_names = [cat_name.strip().title()[0:29] for cat_name in f['recipe_categories'].split(',') if cat_name.strip()]
        recipe.categories_string = ', '.join(recipe.category_names)

        recipe.ingredients = []
        ingredient_names = f.getlist('recipe_ingredients[name][]')[0:-1]
        ingredient_quantities = f.getlist('recipe_ingredients[quantity][]')[0:-1]
        ingredient_units = f.getlist('recipe_ingredients[unit][]')[0:-1]
        ingredient_comments = f.getlist('recipe_ingredients[comment][]')[0:-1]
        lengths = [len(ingredient_names), len(ingredient_quantities), len(ingredient_units), len(ingredient_comments)]
        ingredient_count = min(lengths)
        for i in xrange(ingredient_count):
            if ingredient_names[i].strip():
                ingredient = Ingredient()
                ingredient.name = first_lower(ingredient_names[i].strip())
                ingredient.quantity = ingredient_quantities[i].strip()
                ingredient.unit = ingredient_units[i].strip()
                ingredient.comment = ingredient_comments[i].strip()
                recipe.ingredients.append(ingredient)

        recipe.steps = []
        step_descriptions = f.getlist('recipe_steps[]')[0:-1]
        step_number = 1
        for description in step_descriptions:
            if description.strip():
                step = Step()
                step.instructions = description.strip()
                step.number = step_number
                recipe.steps.append(step)
                step_number += 1

        if recipe.valid():
            if recipe.save(categories, ingredients):
                return redirect(url_for('recent_recipes'))
            else:
                return render_template('create_recipe.html', categories=categories, ingredients=ingredients, recipe=recipe, error="An error has occured while saving the recipe: " + recipe.error_message)
        else:
            return render_template('create_recipe.html', categories=categories, ingredients=ingredients, recipe=recipe, error="An error has occured while saving the recipe: " + recipe.error_message)