for Flask

My tool for creating a database, administrator account and server startup. code:

from sys import argv
from getpass import getpass
from app_name import create_app, db, bcrypt
from app_name.config import Config, ConfigProduction, ConfigTesting
from app_name.auth.models import User

app = create_app(Config) 

def create_db():

def create_admin():
    username = input('Enter admin name: ')
    email = input('Enter admin email: ')
    password = getpass('Enter admin password: ')
    confirm_password = getpass('Retype admin password: ')
    # Simple validation here:
    if '' in (username, email, password, confirm_password): raise ValueError('Empty value occurred')
    if password != confirm_password: raise ValueError("Passwords don't match")
    if User.query.filter_by(username=username).first(): raise ValueError('User already exists in db')

    hashed = bcrypt.generate_password_hash(password).decode('utf-8')
    admin = User(username=username, email=email, password=hashed, admin=True, confirmed=True)

if __name__ == '__main__':
    if len(argv) == 2:
        _, command = argv
        if command == 'run':
        if command == 'create_db': create_db()
        if command == 'create_admin': create_admin()

        print('Invalid command.')

Leave a Reply

Your email address will not be published. Required fields are marked *