-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_admin.py
More file actions
52 lines (43 loc) · 1.54 KB
/
create_admin.py
File metadata and controls
52 lines (43 loc) · 1.54 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
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from backend.models.role import Role
from backend.models.staff import Staff
from backend.security import get_password_hash
DATABASE_URL = "postgresql://postgres:mysecretpassword@localhost/postgres"
engine = create_engine(DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
def create_admin_user():
"""
Creates the default admin user if it doesn't exist.
"""
db = SessionLocal()
try:
# Check if admin user already exists
admin_user = db.query(Staff).filter(Staff.email == "admin@example.com").first()
if admin_user:
print("Admin user already exists.")
return
# Get the Admin role
admin_role = db.query(Role).filter(Role.name == "Admin").first()
if not admin_role:
print("Admin role not found. Please run create_samples.py first.")
return
# Create the admin user
hashed_password = get_password_hash("admin")
new_admin = Staff(
first_name="Admin",
last_name="User",
email="admin@example.com",
contact_number="0000000000",
hashed_password=hashed_password,
role_id=admin_role.id
)
db.add(new_admin)
db.commit()
print("Successfully created admin user.")
print("Email: admin@example.com")
print("Password: admin")
finally:
db.close()
if __name__ == "__main__":
create_admin_user()