当前位置: 首页>>代码示例>>Python>>正文


Python Category.load_unique_categories方法代码示例

本文整理汇总了Python中models.category.Category.load_unique_categories方法的典型用法代码示例。如果您正苦于以下问题:Python Category.load_unique_categories方法的具体用法?Python Category.load_unique_categories怎么用?Python Category.load_unique_categories使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在models.category.Category的用法示例。


在下文中一共展示了Category.load_unique_categories方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: create_recipe

# 需要导入模块: from models.category import Category [as 别名]
# 或者: from models.category.Category import load_unique_categories [as 别名]
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)      
开发者ID:teffland,项目名称:recipes,代码行数:90,代码来源:views.py


注:本文中的models.category.Category.load_unique_categories方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。