from decimal import Decimal import click from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from werkzeug.serving import run_simple from .app import app, settings from .model import Base, UserModel, GeographyModel, StoreModel, AlertModel engine = create_engine(settings.get("database_engine_dsn")) Session = sessionmaker(bind=engine) @click.group() def cli(): pass @click.command() def serve(host="0.0.0.0", port=8080, debug=True, use_reloader=True): run_simple(host, port, app, use_debugger=debug, use_reloader=use_reloader) @click.command() def initdb(): click.echo("Creating database") Base.metadata.create_all(bind=engine) click.echo("Database created") @click.command() def dropdb(): click.echo("Are you sure you would like to drop the database?: [Y/N]") response = input() if response.lower() == "y": Base.metadata.drop_all(bind=engine) click.echo("Database dropped") else: click.echo("Database drop aborted") @click.command() @click.argument("email", type=str) @click.argument("passwd", type=str) def adduser(email, passwd): user = UserModel(email=email, password=passwd, admin=False) session = Session() session.add(user) session.commit() click.echo(f"New user {user.email} has been created") session.close() @click.command() def testdata(): session = Session() user = UserModel(email="test@kellogg.com", password="Welcome1") geography = GeographyModel(id=500_000, name="TEST", user=user) store = StoreModel( id=9_000_000_000, name="SUPER TEST STORE 4000", number="4000", address="123 FAKE STREET", city="PORTAGE", zip="49024", state="MI", lat=Decimal("42.2607409"), long=Decimal("-85.6121787"), tdlinx="0123456", geography=geography, ) alert = AlertModel(store=store, promo_name="BATMAN CHEEZIT") session.add_all([user, geography, store, alert]) session.commit() click.echo("Test Data created") session.close() cli.add_command(serve) cli.add_command(initdb) cli.add_command(dropdb) cli.add_command(adduser) cli.add_command(testdata) if __name__ == "__main__": cli()