77 lines
2.6 KiB
Python
77 lines
2.6 KiB
Python
import os
|
|
import sys
|
|
import redis
|
|
from app import create_app, db
|
|
from app.models import User, ZlLanguage, Agent
|
|
|
|
def seed_production():
|
|
"""
|
|
Production Seeding Script
|
|
1. Initialize Languages
|
|
2. Create Admin User
|
|
3. Verify Redis
|
|
"""
|
|
env = os.getenv('FLASK_ENV', 'production')
|
|
print(f"Starting seeding for environment: {env}")
|
|
app = create_app(env)
|
|
|
|
with app.app_context():
|
|
# 1. Seed Languages
|
|
languages = [
|
|
{'code': 'zh-CN', 'name': '简体中文'},
|
|
{'code': 'zh-TW', 'name': '繁體中文'},
|
|
{'code': 'en', 'name': 'English'},
|
|
{'code': 'th', 'name': 'ไทย'},
|
|
]
|
|
print("Seeding languages...")
|
|
for lang in languages:
|
|
exists = ZlLanguage.query.filter_by(code=lang['code']).first()
|
|
if not exists:
|
|
print(f" + Adding language: {lang['name']}")
|
|
db.session.add(ZlLanguage(code=lang['code'], name=lang['name'], is_active=True))
|
|
else:
|
|
print(f" . Language exists: {lang['name']}")
|
|
|
|
# 2. Create Admin User
|
|
admin_username = 'admin'
|
|
admin_email = 'admin@example.com'
|
|
admin_password = 'admin' # Default initial password, should be changed
|
|
|
|
print(f"Checking admin user '{admin_username}'...")
|
|
user = User.query.filter_by(username=admin_username).first()
|
|
if not user:
|
|
print(f" + Creating admin user: {admin_username} / {admin_password}")
|
|
user = User(
|
|
username=admin_username,
|
|
email=admin_email,
|
|
role='admin',
|
|
real_name='Administrator',
|
|
status='active'
|
|
)
|
|
user.set_password(admin_password)
|
|
db.session.add(user)
|
|
else:
|
|
print(f" . Admin user already exists. Updating password to default 'admin' if needed.")
|
|
# Ensure the password is reset to a known state for initial deployment
|
|
# user.set_password(admin_password)
|
|
# db.session.add(user)
|
|
|
|
db.session.commit()
|
|
print("Database seeding completed.")
|
|
|
|
# 3. Verify Redis
|
|
print("Verifying Redis connection...")
|
|
redis_url = app.config.get('REDIS_URL')
|
|
if redis_url:
|
|
try:
|
|
r = redis.from_url(redis_url)
|
|
r.ping()
|
|
print("✅ Redis connection successful.")
|
|
except Exception as e:
|
|
print(f"❌ Redis connection failed: {e}")
|
|
else:
|
|
print("⚠️ No REDIS_URL configured.")
|
|
|
|
if __name__ == '__main__':
|
|
seed_production()
|