-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake.py
121 lines (106 loc) · 3.19 KB
/
fake.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
from random import randint
from sqlalchemy.exc import IntegrityError
from faker import Faker
from app import db, create_app
from app.models import User, Post, Comment, Tweet
def users(count=100):
fake = Faker()
while count > 0:
user = User(
email=fake.email(),
username=fake.user_name(),
password='password',
confirmed=True,
name=fake.name(),
location=fake.city(),
about_me=fake.text(),
member_since=fake.past_date()
)
db.session.add(user)
try:
db.session.commit()
count -= 1
except IntegrityError:
db.session.rollback()
def posts(count=100):
fake = Faker()
user_count = User.query.count()
for _ in range(count):
user = User.query.offset(randint(0, user_count - 1)).first()
post = Post(
title=fake.sentence(),
body=fake.text(),
created=fake.past_date(),
updated=fake.past_date(),
author=user
)
db.session.add(post)
db.session.commit()
def comments(count=100):
fake = Faker()
user_count = User.query.count()
for post in Post.query.all():
for _ in range(count):
user = User.query.offset(randint(0, user_count - 1)).first()
comment = Comment(
body=fake.sentence(),
post=post,
author=user
)
db.session.add(comment)
for tweet in Tweet.query.all():
for _ in range(count):
user = User.query.offset(randint(0, user_count - 1)).first()
comment = Comment(
body=fake.sentence(),
tweet=tweet,
author=user
)
db.session.add(comment)
db.session.commit()
def tweets(count=100):
fake = Faker()
user_count = User.query.count()
for _ in range(count):
user = User.query.offset(randint(0, user_count - 1)).first()
tweet = Tweet(
body=fake.text(),
created=fake.past_date(),
author=user
)
db.session.add(tweet)
db.session.commit()
def replies():
fake = Faker()
post = Post.query.get(1)
tweet = Tweet.query.get(1)
user_count = User.query.count()
for c in post.comments.all():
user = User.query.offset(randint(0, user_count - 1)).first()
comment = Comment(
body=fake.sentence(),
post=post,
author=user,
parent=c
)
db.session.add(comment)
for c in tweet.comments.all():
user = User.query.offset(randint(0, user_count - 1)).first()
comment = Comment(
body=fake.sentence(),
tweet=tweet,
author=user,
parent=c
)
db.session.add(comment)
db.session.commit()
def run():
app = create_app('default')
with app.app_context():
# users(100)
# posts(100)
comments(100)
# tweets(100)
# replies()
if __name__ == "__main__":
run()