Skip to content

Commit 501b55e

Browse files
committed
Add an initialise script for setup data
1 parent 905aedd commit 501b55e

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

compose/local/django/start

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,11 @@ set -o pipefail
55
set -o nounset
66

77

8+
echo "Migrating"
89
python manage.py migrate
10+
11+
echo "INIT Data"
12+
python manage.py runscript initialise
13+
14+
echo "Launching the application....."
915
exec uvicorn config.asgi:application --host 0.0.0.0 --reload --reload-include '*.html'

scripts/initialise.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import random
2+
3+
from django.conf import settings
4+
from django.contrib.auth import get_user_model
5+
6+
from pi23_gswdt.bank.models import Account, AccountType, AccountTypeChoices, Customer
7+
8+
User = get_user_model()
9+
10+
11+
def create_superuser():
12+
try:
13+
global_superuser = User.objects.create_superuser(
14+
name="Jatin Goel",
15+
username="jatin",
16+
email="jatingoel1037@gmail.com",
17+
password="qwerty123",
18+
is_superuser=True,
19+
is_staff=True,
20+
)
21+
22+
print("Created user: ", global_superuser)
23+
except Exception as excp:
24+
print(f"Failed to create user, error: [{excp}]")
25+
26+
27+
def create_customers():
28+
customers = [
29+
{"cust_id": 123456781, "name": "Jatin 1", "email": "jatin@1.com"},
30+
{"cust_id": 123456782, "name": "Jatin 2", "email": "jatin@2.com"},
31+
{"cust_id": 123456783, "name": "Jatin 3", "email": "jatin@3.com"},
32+
{"cust_id": 123456784, "name": "Jatin 4", "email": "jatin@4.com"},
33+
{"cust_id": 123456785, "name": "Jatin 5", "email": "jatin@5.com"},
34+
]
35+
36+
obj_set = []
37+
38+
for customer in customers:
39+
obj_set.append(Customer(**customer))
40+
41+
try:
42+
Customer.objects.bulk_create(obj_set)
43+
except Exception as excp:
44+
print(f"Customer creation failed. Error: [{excp}]")
45+
return
46+
47+
print("Customer creation successful !!!!")
48+
49+
50+
def create_account_types():
51+
obj_set = []
52+
53+
for account_type in AccountTypeChoices.values:
54+
obj_set.append(AccountType(account_type=account_type))
55+
56+
try:
57+
AccountType.objects.bulk_create(obj_set)
58+
except Exception as excp:
59+
print(f"Account Type creation failed. Error: [{excp}]")
60+
return
61+
62+
print("Account Type creation successful !!!!")
63+
64+
65+
def create_accounts():
66+
obj_set = []
67+
68+
for customer in Customer.objects.all():
69+
for _ in range(2):
70+
obj_set.append(
71+
Account(
72+
customer=customer,
73+
account_type=random.choice(AccountType.objects.all()),
74+
account_number=random.randint(10**15, 10**16 - 1),
75+
balance=random.randint(100, 10**4),
76+
)
77+
)
78+
79+
try:
80+
Account.objects.bulk_create(obj_set)
81+
except Exception as excp:
82+
print(f"Account creation failed. Error: [{excp}]")
83+
return
84+
85+
print("Account creation successful !!!!")
86+
87+
88+
def run(*args):
89+
create_superuser()
90+
create_customers()
91+
create_account_types()
92+
create_accounts()

0 commit comments

Comments
 (0)