init
This commit is contained in:
35
run.py
Normal file
35
run.py
Normal file
@@ -0,0 +1,35 @@
|
||||
import os
|
||||
import click
|
||||
from app import create_app, db
|
||||
from app.models import User
|
||||
|
||||
app = create_app(os.getenv('FLASK_CONFIG') or 'default')
|
||||
|
||||
@app.shell_context_processor
|
||||
def make_shell_context():
|
||||
return dict(db=db, User=User)
|
||||
|
||||
@app.cli.command("create-user")
|
||||
@click.argument("username")
|
||||
@click.argument("email")
|
||||
@click.argument("password")
|
||||
@click.option("--admin", is_flag=True, help="Flag to create an admin user.")
|
||||
def create_user(username, email, password, admin):
|
||||
"""Creates a new user."""
|
||||
if User.query.filter_by(email=email).first():
|
||||
print(f"Error: Email {email} already exists.")
|
||||
return
|
||||
if User.query.filter_by(username=username).first():
|
||||
print(f"Error: Username {username} already exists.")
|
||||
return
|
||||
|
||||
user = User(username=username, email=email, status='active')
|
||||
user.set_password(password)
|
||||
if admin:
|
||||
user.role = 'admin'
|
||||
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
print(f"User {username} created successfully.")
|
||||
if admin:
|
||||
print("Role: Admin")
|
||||
Reference in New Issue
Block a user