-
Notifications
You must be signed in to change notification settings - Fork 0
/
seed_db.py
285 lines (221 loc) · 11.1 KB
/
seed_db.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import datetime
import random
import sys
from operator import or_
import faker
from sqlalchemy.sql import ClauseElement
from sqlalchemy.sql.expression import func
from articles.models import Article
from blog_api.factory import db, bcrypt
from categories.models import Category
from comments.models import Comment
from likes.models import Like
from roles.models import Role, UserRole
from subscriptions.models import UserSubscription
from tags.models import Tag
from users.models import User
faker = faker.Faker()
tags = []
categories = []
def get_or_create(session, model, defaults=None, **kwargs):
instance = session.query(model).filter_by(**kwargs).first()
if instance:
return instance, False
else:
params = dict((k, v) for k, v in kwargs.iteritems() if not isinstance(v, ClauseElement))
params.update(defaults or {})
instance = model(**params)
session.add(instance)
return instance, True
def encrypt_password(password):
return bcrypt.generate_password_hash(password).decode('utf-8')
def seed_admin():
role, created = get_or_create(db.session, Role, defaults={'description': 'for admin only'},
name='ROLE_ADMIN')
admins = User.query.filter(User.users_roles.any(role_id=role.id)).all()
admins = User.query.filter(User.roles.any(id=role.id)).all()
admin_count = User.query.filter(User.roles.any(id=role.id)).count()
user, created = get_or_create(db.session, User, defaults={'first_name': 'adminFN',
'last_name': 'adminFN',
'email': 'admin@flaskblogapi.app',
'password': bcrypt.generate_password_hash('password')},
**{'username': 'admin'})
# db.session.add(user)
db.session.commit()
if len(user.roles) == 0:
# user.users_roles.append(UserRole(user_id=user.id, role_id=role.id))
user.roles.append(role)
db.session.commit()
# User.query.filter(User.roles.any(name='ROLE_ADMIN')).all()
# User.query.join(User.roles).filter_by(name='ROLE_ADMIN').all()
def seed_authors():
role, created = get_or_create(db.session, Role, defaults={'description': 'for authors only'},
name='ROLE_AUTHOR')
authors_count = db.session.query(User.id).filter(User.roles.any(id=role.id)).count()
authors_count = User.query.filter(User.roles.any(id=role.id)).count()
authors_to_seed = 5
authors_to_seed -= authors_count
for i in range(0, authors_to_seed):
profile = faker.profile(fields='username,mail,name')
username = profile['username']
first_name = profile['name'].split()[0]
last_name = profile['name'].split()[1]
email = profile['mail']
password = bcrypt.generate_password_hash('password')
user = User(username=username, first_name=first_name, last_name=last_name, email=email,
password=password, roles=[role])
db.session.add(user)
db.session.commit()
# db.session.add(UserRole(user_id=user.id, role_id=role.id))
db.session.commit()
def seed_users():
role, created = get_or_create(db.session, Role, defaults={'description': 'for standard users'},
name='ROLE_USER')
db.session.commit()
non_user_ids = db.session.query(User.id).filter(
~User.roles.any(id=role.id)).all()
all_users_count = db.session.query(func.count(User.id)).all()[0][0]
all_users_count = db.session.query(User.id).count()
# User.query.filter(User.roles.any(UserRole.role_id.in_([1,2]))).count()
standard_users_count = db.session.query(User).filter(User.roles.any(UserRole.role_id.in_([role.id]))).count()
standard_users_count = db.session.query(User.id).filter(
User.roles.any(id=role.id)).count()
users_to_seed = 23
users_to_seed -= standard_users_count
sys.stdout.write('[+] Seeding %d users\n' % users_to_seed)
for i in range(0, users_to_seed):
profile = faker.profile(fields='username,mail,name')
username = profile['username']
first_name = profile['name'].split()[0]
last_name = profile['name'].split()[1]
email = profile['mail']
password = bcrypt.generate_password_hash('password')
user = User(username=username, first_name=first_name, last_name=last_name, email=email,
password=password)
user.roles.append(role)
db.session.add(user)
db.session.commit()
db.session.commit()
def seed_tags():
sys.stdout.write('[+] Seeding tags\n')
tag = get_or_create(db.session, Tag, defaults={'description': 'rails tutorials'}, name='Ruby On Rails')[0]
tags.append(tag)
tag = get_or_create(db.session, Tag, defaults={'description': 'untagged tutorials'}, name='Untagged')[0]
tags.append(tag)
tag = get_or_create(db.session, Tag, defaults={'description': 'spring boot tutorials'}, name='Spring Boot')[0]
tags.append(tag)
tag = get_or_create(db.session, Tag, defaults={'description': 'laravel tutorials'}, name='Laravel')[0]
tags.append(tag)
tag = get_or_create(db.session, Tag, defaults={'description': 'spring boot tutorials'}, name='Spring Boot')[0]
tags.append(tag)
tag = get_or_create(db.session, Tag, defaults={'description': 'angular tutorials'}, name='Angular')[0]
tags.append(tag)
db.session.add_all(tags)
db.session.commit()
def seed_categories():
sys.stdout.write('[+] Seeding categories\n')
category = get_or_create(db.session, Category, defaults={'description': 'uncategorized tutorials'},
name='Uncategorized')[0]
categories.append(category)
category = get_or_create(db.session, Category, defaults={'description': 'Java tutorials'}, name='Java')[0]
categories.append(category)
category = get_or_create(db.session, Category, defaults={'description': 'ruby tutorials'}, name='Ruby')[0]
categories.append(category)
category = get_or_create(db.session, Category, defaults={'description': 'python tutorials'}, name='Python')[0]
categories.append(category)
category = get_or_create(db.session, Category,
defaults={'description': 'javascript tutorials'}, name='Javascript')[0]
categories.append(category)
category = get_or_create(db.session, Category, defaults={'description': 'angular tutorials'}, name='Angular')[0]
categories.append(category)
db.session.add_all(categories)
db.session.commit()
def seed_articles():
roles = db.session.query(Role.id).filter(or_(Role.name == 'ROLE_ADMIN', Role.name == 'ROLE_AUTHOR')).all()
# Not working roles = db.session.query(Role.id).filter(Role.name == v for v in ('ROLE_ADMIN', 'ROLE_AUTHOR')).all()
roles = [role[0] for role in roles]
articles_count = db.session.query(func.count(Article.id)).all()[0][0]
articles_to_seed = 23
articles_to_seed -= articles_count
sys.stdout.write('[+] Seeding %d articles\n' % articles_to_seed)
author_admin_ids = [user[0] for user in
db.session.query(User.id).filter(User.roles.any(UserRole.role_id.in_(roles))).all()]
tag_ids = [tag[0] for tag in db.session.query(Tag.id).all()]
category_ids = [category[0] for category in db.session.query(Category.id).all()]
for i in range(articles_count, articles_to_seed):
title = faker.sentence()
description = '\n'.join(faker.sentences(2))
body = faker.text()
user_id = random.choice(author_admin_ids)
start_date = datetime.date(year=2017, month=1, day=1)
random_date = faker.date_between(start_date=start_date, end_date='+4y')
publish_on = random_date
a = Article(title=title, body=body, description=description, user_id=user_id,
publish_on=publish_on)
a.tags.append(db.session.query(Tag).order_by(func.random()).first())
for i in range(0, random.randint(1, 2)):
a.tags.append(random.choice(tags))
for i in range(0, random.randint(1, 2)):
a.categories.append(random.choice(categories))
db.session.add(a)
db.session.commit()
def seed_comments():
comments_count = db.session.query(func.count(Comment.id)).scalar()
comments_to_seed = 31
comments_to_seed -= comments_count
sys.stdout.write('[+] Seeding %d comments\n' % comments_to_seed)
comments = []
user_ids = [user[0] for user in User.query.with_entities(User.id).all()]
article_ids = [article[0] for article in Article.query.with_entities(Article.id)]
# equivalent:
# user_ids = [user[0] for user in db.session.query(User.id).all()]
# article_ids = [article[0] for article in db.session.query(Article.id).all()]
for i in range(comments_count, comments_to_seed):
user_id = random.choice(user_ids)
article_id = random.choice(article_ids)
comments.append(Comment(article_id=article_id,
user_id=user_id,
content=faker.sentence()))
db.session.add_all(comments)
db.session.commit()
def seed_likes():
likes_count = db.session.query(func.count(Like.id)).scalar()
likes_to_seed = 30
user_ids = [user[0] for user in db.session.query(User.id).all()]
article_ids = [article[0] for article in db.session.query(Article.id).all()]
for i in range(likes_count, likes_to_seed):
user_id = random.choice(user_ids)
liked_article_ids = [user[0] for user in
Like.query.filter_by(user_id=user_id).with_entities(Like.article_id).all()]
non_liked_articles = [a[0] for a in
db.session.query(Article.id).filter(Article.id.notin_(liked_article_ids)).all()]
# Article.id.notin_ same as ~Article.id.in_
if len(non_liked_articles) == 0:
sys.stdout.write('This user liked all articles')
pass
article_id = random.choice(non_liked_articles)
db.session.add(Like(user_id=user_id, article_id=article_id))
db.session.commit()
def seed_subscriptions():
subscriptions = db.session.query(func.count(UserSubscription.following_id)).scalar()
subscriptions_to_seed = 30
user_ids = [user[0] for user in db.session.query(User.id).all()]
for i in range(subscriptions, subscriptions_to_seed):
follower_id = random.choice(user_ids)
already_following_ids = [u[0] for u in UserSubscription.query.filter(
UserSubscription.follower_id == follower_id).with_entities(UserSubscription.following_id)]
already_following_ids.append(follower_id)
user_ids_not_following = set(user_ids).difference(set(already_following_ids))
following_id = random.sample(user_ids_not_following, 1)[0]
db.session.add(UserSubscription(following_id=following_id, follower_id=follower_id))
db.session.commit()
if __name__ == '__main__':
seed_admin()
seed_authors()
seed_users()
seed_tags()
seed_categories()
seed_articles()
seed_comments()
seed_likes()
seed_subscriptions()