-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_admin.py
More file actions
72 lines (60 loc) · 2.33 KB
/
setup_admin.py
File metadata and controls
72 lines (60 loc) · 2.33 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
"""
Setup script to create the first super admin
Run this once to create the initial super admin account
"""
import sys
import os
from sqlalchemy.orm import Session
# Add the app directory to the path
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from app.db.base import SessionLocal
from app.db.admin_models import Admin, AdminRole
from app.schemas.admin import AdminCreate
from app.services.admin_auth_service import AdminAuthService
def create_super_admin():
"""Create the first super admin"""
db: Session = SessionLocal()
try:
# Check if super admin already exists
existing_super_admin = db.query(Admin).filter(Admin.is_super_admin == True).first()
if existing_super_admin:
print("Super admin already exists!")
print(f"Username: {existing_super_admin.admin_username}")
print(f"Email: {existing_super_admin.admin_email}")
return
# Get admin details
print("Creating Super Admin Account")
print("-" * 30)
admin_username = input("Enter admin username: ").strip()
admin_email = input("Enter admin email: ").strip()
full_name = input("Enter full name: ").strip()
password = input("Enter password: ").strip()
if not all([admin_username, admin_email, full_name, password]):
print("All fields are required!")
return
# Create admin
admin_data = AdminCreate(
admin_username=admin_username,
admin_email=admin_email,
full_name=full_name,
password=password,
role=AdminRole.SUPER_ADMIN
)
admin = AdminAuthService.create_admin(db, admin_data)
admin.is_super_admin = True
db.commit()
print("\n" + "=" * 40)
print("Super Admin Created Successfully!")
print("=" * 40)
print(f"Username: {admin.admin_username}")
print(f"Email: {admin.admin_email}")
print(f"Full Name: {admin.full_name}")
print(f"Role: {admin.role.value}")
print("\nYou can now login at: http://localhost:8000/admin/login")
except Exception as e:
print(f"Error creating super admin: {e}")
db.rollback()
finally:
db.close()
if __name__ == "__main__":
create_super_admin()