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


Python Category.load_categories方法代码示例

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


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

示例1: load_recipe

# 需要导入模块: from models.category import Category [as 别名]
# 或者: from models.category.Category import load_categories [as 别名]
    def load_recipe(cls, recipe_id, current_user):
        attributes = [
            "id",
            "name",
            "servings",
            "preparation_time",
            "photo_file",
            "created_at",
            "creator_id",
            "nutritional_info",
            "avg_ratings.avg_rating",
            "my_ratings.rating",
            "saved_counts.saved_count",
            "saved.saved_at",
            "avg_ratings.rating_count",
        ]

        join_list = []
        join_list.append(
            "LEFT OUTER JOIN (SELECT recipe_id, AVG(rating) AS avg_rating, COUNT(rating) AS rating_count FROM ratings GROUP BY recipe_id) AS avg_ratings ON recipes.id=avg_ratings.recipe_id"
        )
        join_list.append(
            "LEFT OUTER JOIN ratings AS my_ratings ON recipes.id=my_ratings.recipe_id AND my_ratings.user_id=%s"
            % adapt(str(current_user.id)).getquoted()
        )
        join_list.append(
            "LEFT OUTER JOIN (SELECT recipe_id, COUNT(user_id) AS saved_count FROM saved GROUP BY recipe_id) AS saved_counts ON recipes.id=saved_counts.recipe_id"
        )
        join_list.append(
            "LEFT OUTER JOIN saved ON recipes.id=saved.recipe_id AND saved.user_id=%s"
            % adapt(str(current_user.id)).getquoted()
        )
        join_sql = " ".join(join_list)

        where_sql = "WHERE recipes.id=%s" % adapt(str(recipe_id)).getquoted()

        results = engine.execute("SELECT %s FROM recipes %s %s" % (",".join(attributes), join_sql, where_sql))

        recipe = None
        for result in results:
            recipe = Recipe()
            recipe.id = result[0]
            recipe.name = result[1]
            recipe.servings = result[2]
            recipe.preparation_time = result[3]
            recipe.photo_file = result[4]
            recipe.created_at = result[5]
            recipe.creator_id = result[6]
            recipe.nutritional_info = result[7]
            recipe.avg_rating = result[8]
            recipe.rating = result[9]
            recipe.favorite_count = result[10]
            recipe.is_favorite = result[11] != None
            recipe.rating_count = result[12]

            if not recipe.favorite_count:
                recipe.favorite_count = 0

            if not recipe.rating_count:
                recipe.rating_count = 0

            if not recipe.avg_rating:
                recipe.avg_rating = 0.0
            else:
                recipe.avg_rating = round(recipe.avg_rating, 1)

            user = models.user.User.load_user_by_id(recipe.creator_id)
            recipe.creator = user

            recipe.steps = Step.load_steps(recipe.id)
            recipe.ingredients_recipes = IngredientRecipe.load_ingredients(recipe.id)
            recipe.categories = Category.load_categories(recipe.id)
            recipe.category_count = len(recipe.categories)
            recipe.comments = Comment.load_comments(recipe.id)

        return recipe
开发者ID:teffland,项目名称:recipes,代码行数:78,代码来源:recipe.py


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