예제 #1
0
def db_seed():    
    user = User(email='*****@*****.**', first_name='Abc', last_name='Abc', password='******')
    db_session.add(user)
    db_session.commit()

    db_session.bulk_insert_mappings(Category,
        [dict(name="Mexican", description="Mexican style food (don't say!)"), dict(name="Japanese", description="Japanese style food")]
    )
    db_session.commit()
    mexican = Category.query.first()

    db_session.bulk_insert_mappings(Ingredient,
        [dict(name="Mac & Cheese"), dict(name='Milk')]
    )
    db_session.commit()
    ingredients = Ingredient.query.all()
    mac_and_cheese = ingredients[0]
    milk = ingredients[1]

    recipe = Recipe(name='Mac & Cheese', servings='1 person', preparation_time='15 min', photo_path='fakepath')
    recipe.steps.append(Step(number=1,title='Open the box',instructions='Get the box of Mac & cheese you bought and open it. Watch out that opening those boxes can be tricky.'))
    recipe.steps.append(Step(number=2,title='Follow the instructions on the box',instructions="Read whatever is written in the box and follow it. You're welcome."))
    recipe.categories.append(mexican)
    recipe.ingredients_recipes.append(IngredientRecipe(ingredient_id=mac_and_cheese.id, quantity='1', unit='box'))
    recipe.ingredients_recipes.append(IngredientRecipe(ingredient_id=milk.id, quantity='half', unit='pint'))
    user.recipes.append(recipe)
    db_session.add(user)
    db_session.commit()

    rating = Rating()
    rating.recipe = recipe
    rating.user = user
    rating.rating = 5
    db_session.add(rating)
    db_session.commit()

    saved = Saved()
    saved.recipe = recipe
    saved.user = user
    db_session.add(saved)
    db_session.commit()

    comment = Comment(text='I like my recipe.', recipe_id=recipe.id, user_id=user.id)
    db_session.add(comment)
    db_session.commit()

    db_session.close()
예제 #2
0
파일: views.py 프로젝트: teffland/recipes
def unfavorite(recipe_id=None):
    if recipe_id:
        Saved.unmark_as_favorite(g.current_user, recipe_id)
    return ''
예제 #3
0
파일: recipe.py 프로젝트: teffland/recipes
    def load_recipes(cls, current_user, limit=8, page=1, order_by="created_at DESC", join=[], where=[]):
        attributes = ["id", "name", "servings", "preparation_time", "photo_file", "created_at", "creator_id"]
        if page == 1:
            offset = ""
        else:
            offset = "OFFSET %s" % adapt(str(limit * (page - 1))).getquoted()[1:-1]

        join_sql = ""
        if len(join) > 0:
            join_list = []
            for join_desc in join:
                if join_desc == "avg_ratings":
                    join_list.append(
                        "LEFT OUTER JOIN (SELECT recipe_id, AVG(rating) AS avg_rating FROM ratings GROUP BY recipe_id) AS avg_ratings ON recipes.id=avg_ratings.recipe_id"
                    )
                if join_desc == "saved":
                    join_list.append(
                        "INNER 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_array = where
        where_sql = ""
        if len(where_array) > 0:
            where_sql += "WHERE " + (" AND ".join(where_array))

        result = engine.execute(
            "SELECT %s FROM recipes %s %s ORDER BY %s LIMIT %s %s"
            % (",".join(attributes), join_sql, where_sql, order_by, adapt(str(limit)).getquoted(), offset)
        )

        dicts = []
        for values in result:
            attrs = {attributes[i]: value for i, value in enumerate(values)}
            dicts.append(attrs)

        recipes = cls.create_from_dicts(dicts)
        page_count = 1
        recipe_count = 0

        if len(recipes) > 0:
            # get total recipe count
            result = engine.execute("SELECT COUNT(*) FROM recipes %s %s" % (join_sql, where_sql))
            recipe_count = None
            for result_tuple in result:
                recipe_count = result_tuple[0]
            page_count = int(math.ceil(float(recipe_count) / limit))
            if page_count == 0:
                page_count = 1

            # get avg ratings
            recipe_ids = [str(recipe.id) for recipe in recipes]
            ratings = Rating.avg_rating_by_recipe(recipe_ids)

            # get favorited
            favorites = Saved.favorites(recipe_ids, current_user.id)

            # inject things on recipes
            for recipe in recipes:
                recipe.avg_rating = ratings[recipe.id]
                recipe.round_rating = round(recipe.avg_rating)
                recipe.is_user_favorite = recipe.id in favorites

        return recipes, page_count, recipe_count