flask-api-example/fake_data.py

49 lines
1.1 KiB
Python
Raw Normal View History

2018-01-31 23:17:52 +01:00
from werkzeug.security import generate_password_hash
2018-02-05 23:59:51 +01:00
from models import db, User, Notice, Comment
2018-01-31 23:17:52 +01:00
from faker import Factory
from random import randint
# Spanish
fake = Factory.create('es_ES')
# Reload tables
db.drop_all()
db.create_all()
# Make 100 fake users
for num in range(100):
profile = fake.simple_profile()
username = profile['username']
mail = profile['mail']
password = generate_password_hash('123')
# Save in database
my_user = User(username=username, mail=mail, password=password)
db.session.add(my_user)
print('Users created')
# Make 1000 fake news
for num in range(1000):
title = fake.sentence()
link = fake.uri()
user_id = randint(1, 100)
# Save in database
2018-02-05 23:59:51 +01:00
my_notice = Notice(title=title, link=link, user_id=user_id)
db.session.add(my_notice)
2018-01-31 23:17:52 +01:00
print('News created')
# Make 10000 fake comments
for num in range(10000):
text = fake.text()
2018-02-05 23:59:51 +01:00
notice_id = randint(1, 1000)
2018-01-31 23:17:52 +01:00
user_id = randint(1, 100)
# Save in database
2018-02-05 23:59:51 +01:00
my_comment = Comment(text=text, notice_id=notice_id, user_id=user_id)
2018-01-31 23:17:52 +01:00
db.session.add(my_comment)
print('Comments created')
# Save
db.session.commit()