Compare commits
2 Commits
7c567018a2
...
fc4eaaa663
| Author | SHA1 | Date | |
|---|---|---|---|
| fc4eaaa663 | |||
| 959c4e883d |
Binary file not shown.
BIN
Machine-Backend/app/instance/machines
Normal file
BIN
Machine-Backend/app/instance/machines
Normal file
Binary file not shown.
@ -1,52 +1,115 @@
|
||||
from app import create_app, db
|
||||
from app.models import User
|
||||
from app.models.models import User
|
||||
import sys
|
||||
|
||||
def seed_sample_user():
|
||||
"""Create a sample user for testing login"""
|
||||
def seed_sample_users():
|
||||
"""Create sample users for testing login with correct roles"""
|
||||
app = create_app()
|
||||
|
||||
with app.app_context():
|
||||
try:
|
||||
# Check if user already exists
|
||||
existing_user = User.query.filter_by(email='admin@vending.com').first()
|
||||
if existing_user:
|
||||
print("Sample user already exists!")
|
||||
print(f"Email: admin@vending.com")
|
||||
print(f"Password: admin123")
|
||||
return
|
||||
# Sample users with correct role names (must match database roles exactly)
|
||||
sample_users = [
|
||||
{
|
||||
'username': 'Super Admin',
|
||||
'email': 'superadmin@vending.com',
|
||||
'password': 'admin123',
|
||||
'role': 'SuperAdmin', # MUST MATCH DATABASE EXACTLY
|
||||
'contact': '+1234567890'
|
||||
},
|
||||
{
|
||||
'username': 'Management User',
|
||||
'email': 'management@vending.com',
|
||||
'password': 'management123',
|
||||
'role': 'Management',
|
||||
'contact': '+1234567891'
|
||||
},
|
||||
{
|
||||
'username': 'Admin User',
|
||||
'email': 'admin@vending.com',
|
||||
'password': 'admin123',
|
||||
'role': 'Admin',
|
||||
'contact': '+1234567892'
|
||||
},
|
||||
{
|
||||
'username': 'Client User',
|
||||
'email': 'client@vending.com',
|
||||
'password': 'client123',
|
||||
'role': 'Client',
|
||||
'contact': '+1234567893'
|
||||
},
|
||||
{
|
||||
'username': 'Refiller User',
|
||||
'email': 'refiller@vending.com',
|
||||
'password': 'refiller123',
|
||||
'role': 'Refiller',
|
||||
'contact': '+1234567894'
|
||||
},
|
||||
{
|
||||
'username': 'Servicer User',
|
||||
'email': 'servicer@vending.com',
|
||||
'password': 'servicer123',
|
||||
'role': 'Servicer',
|
||||
'contact': '+1234567895'
|
||||
}
|
||||
]
|
||||
|
||||
# Create sample admin user
|
||||
sample_user = User(
|
||||
user_id=User.generate_user_id('admin', 'admin@vending.com'),
|
||||
username='Admin User',
|
||||
email='admin@vending.com',
|
||||
contact='+1234567890',
|
||||
roles='admin',
|
||||
user_status='active'
|
||||
)
|
||||
print("=" * 70)
|
||||
print("SEEDING SAMPLE USERS")
|
||||
print("=" * 70)
|
||||
|
||||
# Set password
|
||||
sample_user.set_password('admin123')
|
||||
created_count = 0
|
||||
|
||||
# Add to database
|
||||
db.session.add(sample_user)
|
||||
for user_data in sample_users:
|
||||
# Check if user already exists
|
||||
existing_user = User.query.filter_by(email=user_data['email']).first()
|
||||
if existing_user:
|
||||
print(f"⚠️ User {user_data['email']} already exists - skipping")
|
||||
continue
|
||||
|
||||
# Create new user
|
||||
new_user = User(
|
||||
user_id=User.generate_user_id(user_data['username'], user_data['email']),
|
||||
username=user_data['username'],
|
||||
email=user_data['email'],
|
||||
contact=user_data['contact'],
|
||||
roles=user_data['role'], # CORRECT ROLE NAME
|
||||
user_status='active'
|
||||
)
|
||||
|
||||
# Set password
|
||||
new_user.set_password(user_data['password'])
|
||||
|
||||
# Add to database
|
||||
db.session.add(new_user)
|
||||
created_count += 1
|
||||
|
||||
print(f"✓ User: {user_data['username']} ({user_data['role']})")
|
||||
|
||||
# Commit all changes
|
||||
db.session.commit()
|
||||
|
||||
print("✅ Sample user created successfully!")
|
||||
print("=" * 50)
|
||||
print(f"User ID: {sample_user.user_id}")
|
||||
print(f"Username: {sample_user.username}")
|
||||
print(f"Email: admin@vending.com")
|
||||
print(f"Password: admin123")
|
||||
print(f"Role: {sample_user.roles}")
|
||||
print(f"Status: {sample_user.user_status}")
|
||||
print("=" * 50)
|
||||
print("\n" + "=" * 70)
|
||||
print(f"✓ Successfully created {created_count} sample user(s)")
|
||||
print("=" * 70)
|
||||
|
||||
# Display all created users
|
||||
print("\nCREATED USERS - USE THESE TO LOGIN:")
|
||||
print("=" * 70)
|
||||
all_users = User.query.all()
|
||||
for user in all_users:
|
||||
print(f"\nUsername: {user.username}")
|
||||
print(f"Email: {user.email}")
|
||||
print(f"Role: {user.roles}")
|
||||
print(f"Status: {user.user_status}")
|
||||
print("-" * 70)
|
||||
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
print(f"❌ Error creating sample user: {str(e)}")
|
||||
print(f"\n❌ Error creating sample users: {str(e)}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
sys.exit(1)
|
||||
|
||||
if __name__ == '__main__':
|
||||
seed_sample_user()
|
||||
seed_sample_users()
|
||||
Reference in New Issue
Block a user