-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_users.py
More file actions
104 lines (86 loc) · 4.18 KB
/
setup_users.py
File metadata and controls
104 lines (86 loc) · 4.18 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
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
#!/usr/bin/env python3
import os
import sys
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Add the current directory to the Python path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
# Import the Flask app and its components
from app import app, mongo, db
from passlib.hash import scrypt
import datetime
def setup_users():
"""Create admin and johndoe users"""
with app.app_context():
try:
# Create admin user if it doesn't exist
admin_user = mongo.db.customers.find_one({'username': 'admin', 'is_admin': True})
if not admin_user:
admin_hashed_password = scrypt.hash('mongodb123')
admin_user_doc = {
"username": "admin",
"password": admin_hashed_password,
"is_admin": True,
"created_at": datetime.datetime.now(datetime.timezone.utc)
}
admin_result = mongo.db.customers.insert_one(admin_user_doc)
print(f"Admin user created with ID: {admin_result.inserted_id}")
else:
print("Admin user already exists")
# Create johndoe user if it doesn't exist
johndoe_user = mongo.db.customers.find_one({'username': 'johndoe'})
if not johndoe_user:
johndoe_hashed_password = scrypt.hash('password123')
johndoe_user_doc = {
"username": "johndoe",
"password": johndoe_hashed_password,
"email": "johndoe@example.com",
"created_at": datetime.datetime.now(datetime.timezone.utc),
"is_admin": False
}
johndoe_result = mongo.db.customers.insert_one(johndoe_user_doc)
print(f"Johndoe user created with ID: {johndoe_result.inserted_id}")
# Create accounts for johndoe
checking_account = {
"customer_id": johndoe_result.inserted_id,
"account_type": "Checking",
"balance": 5000.00,
"created_at": datetime.datetime.now(datetime.timezone.utc)
}
savings_account = {
"customer_id": johndoe_result.inserted_id,
"account_type": "Savings",
"balance": 10000.00,
"created_at": datetime.datetime.now(datetime.timezone.utc)
}
account_results = mongo.db.accounts.insert_many([checking_account, savings_account])
print(f"Created {len(account_results.inserted_ids)} accounts for johndoe")
# Create some sample transactions
transactions = [
{
"account_id": account_results.inserted_ids[0], # Checking account
"amount": 500.00,
"type": "deposit",
"description": "Initial deposit",
"timestamp": datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(days=7)
},
{
"account_id": account_results.inserted_ids[1], # Savings account
"amount": 1000.00,
"type": "deposit",
"description": "Initial deposit",
"timestamp": datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(days=5)
}
]
transaction_results = mongo.db.transactions.insert_many(transactions)
print(f"Created {len(transaction_results.inserted_ids)} sample transactions")
else:
print("Johndoe user already exists")
print("User setup completed successfully!")
except Exception as e:
print(f"Error setting up users: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
setup_users()