Extend fake shell command for adding fake posts into DB

This commit is contained in:
Xevion
2022-03-27 10:20:53 -05:00
parent ced2f975ae
commit 4f585448d7
+12 -1
View File
@@ -1,3 +1,4 @@
import random
from datetime import datetime from datetime import datetime
import click import click
@@ -67,8 +68,9 @@ def create_app():
@click.argument("count") @click.argument("count")
def create_fake_users(count: int): def create_fake_users(count: int):
fake = Faker() fake = Faker()
count = int(count)
users = {} users = {}
for _ in range(int(count)): for _ in range(count):
profile: dict = fake.simple_profile() profile: dict = fake.simple_profile()
users[profile['username']] = profile users[profile['username']] = profile
@@ -82,6 +84,15 @@ def create_app():
print(f'Committing {len(users)} users into DB.') print(f'Committing {len(users)} users into DB.')
db.session.commit() db.session.commit()
post_count: int = 0
for author in random.choices(User.query.all(), k=count // 4):
new_post = Post(author=author.id, text=fake.paragraph(nb_sentences=2))
db.session.add(new_post)
post_count += 1
print(f'Committing {post_count} posts into the DB.')
db.session.commit()
@app.cli.command("create_all") @app.cli.command("create_all")
def db_create_all() -> None: def db_create_all() -> None:
db.create_all(app=app) db.create_all(app=app)