updates
This commit is contained in:
165
Backend/seeds_data/seed_users.py
Normal file
165
Backend/seeds_data/seed_users.py
Normal file
@@ -0,0 +1,165 @@
|
||||
import sys
|
||||
import os
|
||||
from pathlib import Path
|
||||
sys.path.insert(0, str(Path(__file__).parent))
|
||||
from sqlalchemy.orm import Session
|
||||
from src.config.database import SessionLocal
|
||||
from src.models.role import Role
|
||||
from src.models.user import User
|
||||
import bcrypt
|
||||
from datetime import datetime
|
||||
|
||||
def get_db():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
return db
|
||||
finally:
|
||||
pass
|
||||
|
||||
def seed_users(db: Session):
|
||||
print('=' * 80)
|
||||
print('SEEDING USERS')
|
||||
print('=' * 80)
|
||||
|
||||
# Get roles
|
||||
admin_role = db.query(Role).filter(Role.name == 'admin').first()
|
||||
staff_role = db.query(Role).filter(Role.name == 'staff').first()
|
||||
customer_role = db.query(Role).filter(Role.name == 'customer').first()
|
||||
|
||||
if not admin_role or not staff_role or not customer_role:
|
||||
print(' ❌ Roles not found! Please seed roles first.')
|
||||
return
|
||||
|
||||
users_data = [
|
||||
{
|
||||
'email': 'gnxsoft@gnxsoft.com',
|
||||
'password': 'gnxsoft123',
|
||||
'full_name': 'GNXSoft Admin',
|
||||
'phone': '+1 (555) 111-2222',
|
||||
'role': 'admin',
|
||||
'currency': 'EUR',
|
||||
'is_active': True
|
||||
},
|
||||
{
|
||||
'email': 'admin@gnxsoft.com',
|
||||
'password': 'admin123',
|
||||
'full_name': 'Administrator',
|
||||
'phone': '+1 (555) 222-3333',
|
||||
'role': 'admin',
|
||||
'currency': 'EUR',
|
||||
'is_active': True
|
||||
},
|
||||
{
|
||||
'email': 'staff@gnxsoft.com',
|
||||
'password': 'staff123',
|
||||
'full_name': 'Staff Member',
|
||||
'phone': '+1 (555) 333-4444',
|
||||
'role': 'staff',
|
||||
'currency': 'EUR',
|
||||
'is_active': True
|
||||
},
|
||||
{
|
||||
'email': 'customer@gnxsoft.com',
|
||||
'password': 'customer123',
|
||||
'full_name': 'Customer User',
|
||||
'phone': '+1 (555) 444-5555',
|
||||
'role': 'customer',
|
||||
'currency': 'EUR',
|
||||
'is_active': True
|
||||
},
|
||||
{
|
||||
'email': 'john.doe@gnxsoft.com',
|
||||
'password': 'customer123',
|
||||
'full_name': 'John Doe',
|
||||
'phone': '+1 (555) 555-6666',
|
||||
'role': 'customer',
|
||||
'currency': 'USD',
|
||||
'is_active': True
|
||||
},
|
||||
{
|
||||
'email': 'jane.smith@gnxsoft.com',
|
||||
'password': 'customer123',
|
||||
'full_name': 'Jane Smith',
|
||||
'phone': '+1 (555) 666-7777',
|
||||
'role': 'customer',
|
||||
'currency': 'EUR',
|
||||
'is_active': True
|
||||
},
|
||||
{
|
||||
'email': 'robert.wilson@gnxsoft.com',
|
||||
'password': 'customer123',
|
||||
'full_name': 'Robert Wilson',
|
||||
'phone': '+1 (555) 777-8888',
|
||||
'role': 'customer',
|
||||
'currency': 'GBP',
|
||||
'is_active': True
|
||||
},
|
||||
{
|
||||
'email': 'maria.garcia@gnxsoft.com',
|
||||
'password': 'customer123',
|
||||
'full_name': 'Maria Garcia',
|
||||
'phone': '+1 (555) 888-9999',
|
||||
'role': 'customer',
|
||||
'currency': 'EUR',
|
||||
'is_active': True
|
||||
}
|
||||
]
|
||||
|
||||
role_map = {
|
||||
'admin': admin_role.id,
|
||||
'staff': staff_role.id,
|
||||
'customer': customer_role.id
|
||||
}
|
||||
|
||||
created_count = 0
|
||||
skipped_count = 0
|
||||
|
||||
for user_data in users_data:
|
||||
existing = db.query(User).filter(User.email == user_data['email']).first()
|
||||
if existing:
|
||||
print(f' ⚠️ User "{user_data["email"]}" already exists, skipping...')
|
||||
skipped_count += 1
|
||||
continue
|
||||
|
||||
password = user_data.pop('password')
|
||||
role_name = user_data.pop('role')
|
||||
role_id = role_map[role_name]
|
||||
|
||||
password_bytes = password.encode('utf-8')
|
||||
salt = bcrypt.gensalt()
|
||||
hashed_password = bcrypt.hashpw(password_bytes, salt).decode('utf-8')
|
||||
|
||||
user = User(
|
||||
email=user_data['email'],
|
||||
password=hashed_password,
|
||||
full_name=user_data['full_name'],
|
||||
phone=user_data.get('phone'),
|
||||
role_id=role_id,
|
||||
currency=user_data.get('currency', 'EUR'),
|
||||
is_active=user_data.get('is_active', True)
|
||||
)
|
||||
db.add(user)
|
||||
print(f' ✓ Created user: {user_data["email"]} ({role_name}) - Password: {password}')
|
||||
created_count += 1
|
||||
|
||||
db.commit()
|
||||
print(f'\n✓ Users seeded successfully!')
|
||||
print(f' - Created: {created_count} user(s)')
|
||||
print(f' - Skipped: {skipped_count} user(s) (already exist)')
|
||||
print('=' * 80)
|
||||
|
||||
def main():
|
||||
db = get_db()
|
||||
try:
|
||||
seed_users(db)
|
||||
except Exception as e:
|
||||
print(f'\n❌ Error: {e}')
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
db.rollback()
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user