forked from hngprojects/hng_boilerplate_python_fastapi_web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseed.py
More file actions
47 lines (29 loc) · 1.5 KB
/
seed.py
File metadata and controls
47 lines (29 loc) · 1.5 KB
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
#!/usr/bin/env python3
""" Populates the database with seed data
"""
from api.v1.models import *
from api.v1.models.base import Base
from api.v1.services.user import user_service
from api.db.database import create_database, get_db
# create_database()
db = next(get_db())
user_1 = User(email="test@mail", username="testuser", password=user_service.hash_password("testpass"), first_name="John", last_name="Doe")
user_2 = User(email="test1@mail", username="testuser1", password=user_service.hash_password("testpass1"), first_name="Jane", last_name="Boyle")
user_3 = User(email="test2@mail", username="testuser2", password=user_service.hash_password("testpass2"), first_name="Bob", last_name="Dwayne")
db.add_all([user_1, user_2, user_3])
org_1 = Organization(name= "Python Org", description="An organization for python develoers")
org_2 = Organization(name="Django Org", description="An organization of django devs")
org_3 = Organization(name="FastAPI Devs", description="An organization of Fast API devs")
db.add_all([org_1, org_2, org_3])
org_1.users.extend([user_1, user_2, user_3])
org_2.users.extend([user_1, user_3])
org_3.users.extend([user_2, user_1])
db.commit()
product_1 = Product(name="bed", price=400000, org_id=org_1.id)
product_2 = Product(name="shoe", price=150000, org_id=org_2.id)
profile_1 = Profile(bio='My name is John Doe', phone_number='09022112233')
user_1.profile = profile_1
db.add_all([product_1, product_2])
db.commit()
users = db.query(Organization).first().users
print("Seed data succesfully")