Ejemplo n.º 1
0
def seed_db():
    from models.Budget import Budget
    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.username = f"tester{i}"
        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):
        budget = Budget()
        budget.name = faker.catch_phrase()
        budget.user_id = random.choice(users).id
        db.session.add(budget)

    db.session.commit()
    print("Tables seeded")
Ejemplo n.º 2
0
def budget_create():
    budget_fields = budget_schema.load(request.json)
    user_id = get_jwt_identity()

    user = User.query.get(user_id)

    if not user:
        return abort(401, description="Invalid user")

    new_budget = Budget()
    new_budget.name = budget_fields["name"]

    user.budgets.append(new_budget)
    db.session.commit()

    return jsonify(budget_schema.dump(new_budget))