-
Notifications
You must be signed in to change notification settings - Fork 2
/
seed.py
278 lines (233 loc) · 9.35 KB
/
seed.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
import datetime
from fastapi import FastAPI
from fastapi.testclient import TestClient
from sqlalchemy.orm import Session
from db_models.cafe_shift_model import CafeShift_DB
from db_models.ad_model import BookAd_DB
from db_models.council_model import Council_DB
from db_models.event_model import Event_DB
from db_models.news_model import News_DB
from db_models.permission_model import Permission_DB
from db_models.post_model import Post_DB
from api_schemas.user_schemas import UserCreate
from db_models.song_category_model import SongCategory_DB
from db_models.song_model import Song_DB
from db_models.user_model import User_DB
from pydantic_extra_types.phone_numbers import PhoneNumber
def seed_users(db: Session, app: FastAPI):
# This one seeds by actually calling user register route. Other create models directly
client = TestClient(app)
boss = UserCreate(
email="[email protected]",
first_name="Boss",
last_name="AllaPostersson",
password="dabdab",
telephone_number=PhoneNumber("+46760187158"),
program="F",
)
user = UserCreate(
email="[email protected]",
first_name="User",
last_name="Userström",
password="dabdab",
telephone_number=PhoneNumber("+46706427444"),
program="F",
)
user2 = UserCreate(
email="[email protected]",
first_name="User2",
last_name="Userström2",
password="dabdab",
telephone_number=PhoneNumber("+46760187158"),
)
user3 = UserCreate(
email="[email protected]",
first_name="User3",
last_name="Userström3",
password="dabdab",
telephone_number=PhoneNumber("+46760187158"),
)
user4 = UserCreate(
email="[email protected]",
first_name="User4",
last_name="Userström4",
password="dabdab",
telephone_number=PhoneNumber("+46760187158"),
)
boss_response = client.post("/auth/register", json=boss.model_dump())
assert boss_response.status_code == 201
response = client.post("/auth/register", json=user.model_dump())
assert response.status_code == 201
user2_response = client.post("/auth/register", json=user2.model_dump())
assert user2_response.status_code == 201
user3_response = client.post("/auth/register", json=user3.model_dump())
assert user3_response.status_code == 201
user4_response = client.post("/auth/register", json=user4.model_dump())
assert user4_response.status_code == 201
client.close()
boss_id = boss_response.json()["id"]
user_id = response.json()["id"]
user2_id = user2_response.json()["id"]
user3_id = user3_response.json()["id"]
user4_id = user4_response.json()["id"]
# now fetch the created users and set is_verified to True
boss = db.query(User_DB).filter_by(id=boss_id).one()
user = db.query(User_DB).filter_by(id=user_id).one()
user2 = db.query(User_DB).filter_by(id=user2_id).one()
user3 = db.query(User_DB).filter_by(id=user3_id).one()
user4 = db.query(User_DB).filter_by(id=user4_id).one()
boss.is_verified = True
user.is_verified = True
boss.is_member = True
user2.is_verified = True
user3.is_verified = True
user4.is_verified = True
boss.is_member = True
user.is_member = True
user2.is_member = True
user3.is_member = True
user4.is_member = True
db.commit()
return boss, user
def seed_cafe_shifts(db: Session, user: User_DB):
starts_at = datetime.datetime.now(datetime.UTC)
ends_at = starts_at + datetime.timedelta(hours=1)
shift = CafeShift_DB(starts_at=starts_at, ends_at=ends_at)
shift.user = user
shift.user_id = user.id
db.add(shift)
db.commit()
return shift
def seed_councils(db: Session):
councils = [Council_DB(name="Kodmästeriet"), Council_DB(name="Sanningsministeriet")]
db.add_all(councils)
db.commit()
return councils
def seed_posts(db: Session, some_councils: list[Council_DB]):
posts = [
Post_DB(name="Buggmästare", council_id=some_councils[0].id),
Post_DB(name="Lallare", council_id=some_councils[0].id),
Post_DB(name="Mytoman", council_id=some_councils[1].id),
]
db.add_all(posts)
db.commit()
return posts
def seed_post_users(db: Session, boss: User_DB, user: User_DB, posts: list[Post_DB]):
# Give posts to users
boss.posts = posts
user.posts.append(posts[0])
db.commit()
def seed_permissions(db: Session, posts: list[Post_DB]):
perm1 = Permission_DB(action="manage", target="Permission")
perm2 = Permission_DB(action="view", target="User")
perm3 = Permission_DB(action="manage", target="Event")
perm4 = Permission_DB(action="manage", target="Post")
perm5 = Permission_DB(action="manage", target="News")
perm6 = Permission_DB(action="manage", target="Song")
perm7 = Permission_DB(action="manage", target="Gallery")
perm8 = Permission_DB(action="manage", target="Ads")
perm9 = Permission_DB(action="manage", target="Car")
perm10 = Permission_DB(action="manage", target="Cafe")
posts[0].permissions.append(perm1)
posts[0].permissions.append(perm2)
posts[1].permissions.append(perm3)
posts[0].permissions.append(perm4)
posts[1].permissions.append(perm5)
posts[0].permissions.append(perm6)
posts[0].permissions.append(perm7)
posts[0].permissions.append(perm8)
posts[0].permissions.append(perm9)
posts[1].permissions.append(perm10)
db.commit()
def seed_events(db: Session, one_council: Council_DB):
starts_at = datetime.datetime.now(datetime.UTC)
signup_start = starts_at + datetime.timedelta(hours=1)
signup_end = signup_start + datetime.timedelta(hours=1)
ends_at = signup_end + datetime.timedelta(hours=1)
event = Event_DB(
council_id=one_council.id,
starts_at=starts_at,
ends_at=ends_at,
description_en="Dis gun be litty",
description_sv="Det blir fett gäähda",
title_en="Phat party",
title_sv="Loyde gähdda",
signup_start=signup_start,
signup_end=signup_end,
)
db.add(event)
db.commit()
return event
def seed_news(db: Session, user: User_DB):
news = [
News_DB(
title_sv="Min första nyhet :))",
title_en="My first news :smilyface:",
content_sv="Oj här var det ju en massa spännande saker man kunde läsa!",
content_en="Whoops here there was a lot of content to read!",
author_id=user.id,
),
News_DB(
title_sv="En annan nyhet",
title_en="Another news",
content_sv="Lite mer content",
content_en="A bit more content",
author_id=user.id,
),
]
db.add_all(news)
db.commit()
def seed_songs_and_song_category(db: Session):
category = SongCategory_DB(name="Lofi hip hop - beats to study/relax to")
db.add(category)
song = Song_DB(
title="Never Gonna Give You Up",
author="rick astley n77",
content="Blah,blah,blah",
category_id=1,
)
db.add(song)
db.commit()
return
def seed_ads(db: Session):
users = db.query(User_DB).all()
ad = BookAd_DB(title="Endim", course="FMNAF05", author="Jonas", price=50, selling=True, user_id=users[0].id)
db.add(ad)
ad = BookAd_DB(title="Flerdim", course="FMNAF25", author="Jonas", price=190, selling=True, user_id=users[0].id)
db.add(ad)
ad = BookAd_DB(title="Linalg", course="FMNAF35", author="Jonas", price=920, selling=True, user_id=users[2].id)
db.add(ad)
ad = BookAd_DB(title="Våglära", course="VAG01", author="Tjalle", price=9430, selling=False, user_id=users[1].id)
db.add(ad)
ad = BookAd_DB(title="Java", course="JA25", author="Patrik", price=50, selling=True, user_id=users[1].id)
db.add(ad)
ad = BookAd_DB(title="EffektivC", course="EC10", author="Skeppstedt", price=90, selling=True, user_id=users[0].id)
db.add(ad)
ad = BookAd_DB(title="Matstat", course="MATS100", author="Tant", price=670, selling=False, user_id=users[1].id)
db.add(ad)
ad = BookAd_DB(title="Termo", course="VARM20", author="Thomas", price=90, selling=True, user_id=users[1].id)
db.add(ad)
ad = BookAd_DB(title="Kemi", course="BOOM12", author="Bengt", price=10, selling=False, user_id=users[3].id)
db.add(ad)
ad = BookAd_DB(title="Endim", course="FMNAF05", author="Jonas", price=1, selling=True, user_id=users[4].id)
db.add(ad)
ad = BookAd_DB(title="Endim", course="FMNAF05", author="Jonas", price=10, selling=True, user_id=users[2].id)
db.add(ad)
db.commit()
def seed_if_empty(app: FastAPI, db: Session):
# If there's no user, assumed DB is empty and seed it.
if db.query(User_DB).count() > 0:
return
print("Time to seed.")
councils = seed_councils(db)
posts = seed_posts(db, councils)
seed_permissions(db, posts)
boss, user = seed_users(db, app)
seed_cafe_shifts(db, user)
seed_post_users(db, boss, user, posts)
seed_events(db, councils[1])
seed_news(db, boss)
seed_songs_and_song_category(db)
seed_songs_and_song_category(db)
seed_ads(db)
print("Done seeding!")