예제 #1
0
def seed_db():
    from models.Recipe import Recipe
    from models.User import User
    from main import bcrypt
    from faker import Faker
    import random

    faker = Faker()
    users = []

    for i in range(5):
        user = User()
        user.email =  f"test{i}@test.com"
        user.password = bcrypt.generate_password_hash("123456").decode("utf-8")
        db.session.add(user)
        users.append(user)
    
    db.session.commit()

    for i in range(10):
        recipe = Recipe()
        recipe.recipe_name = faker.catch_phrase()
        recipe.description = faker.catch_phrase()
        recipe.user_id = random.choice(users).user_id
        db.session.add(recipe)
        print(f"{i + 1} recipe record(s) created")

    db.session.commit()
    print("Tables seeded")
def recipe_create(user=None):
    #Create a new recipe
    recipe_fields = recipe_schema.load(request.json)

    new_recipe = Recipe()
    new_recipe.recipe_name = recipe_fields["recipe_name"]
    new_recipe.description = recipe_fields["description"]

    user.recipes.append(new_recipe)

    db.session.commit()
    
    return jsonify(recipe_schema.dump(new_recipe))