#!/usr/bin/env python3 """ Create an admin user for the application """ import os import sys import getpass from flask import Flask from werkzeug.security import generate_password_hash # Add the parent directory to sys.path sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) from app import create_app from app.core.extensions import db from app.core.auth import User def create_admin_user(email=None, password=None): """Create an admin user in the database""" app = create_app() with app.app_context(): # Check if users already exist if User.query.count() > 0: print("Users already exist in the database.") choice = input("Do you want to create another admin user? (y/n): ") if choice.lower() != "y": print("Operation cancelled.") return # Prompt for email if not provided if not email: email = input("Enter admin email: ") # Check if user with this email already exists existing_user = User.query.filter_by(email=email).first() if existing_user: print(f"User with email {email} already exists!") return # Prompt for password if not provided if not password: password = getpass.getpass("Enter admin password: ") confirm_password = getpass.getpass("Confirm password: ") if password != confirm_password: print("Passwords do not match!") return # Create the admin user admin = User(email=email, is_admin=True) admin.set_password(password) db.session.add(admin) db.session.commit() print(f"Admin user created successfully: {email}") if __name__ == "__main__": import argparse parser = argparse.ArgumentParser(description="Create an admin user") parser.add_argument("--email", help="Admin user email") parser.add_argument("--password", help="Admin user password") args = parser.parse_args() create_admin_user(args.email, args.password)