lenticular_cloud2/lenticular_cloud/cli.py

96 lines
3.1 KiB
Python
Raw Normal View History

2022-02-06 22:57:01 +00:00
import argparse
2022-06-17 11:38:49 +00:00
from .model import db, User
2022-02-06 22:57:01 +00:00
from .app import create_app
2022-02-11 15:10:40 +00:00
from werkzeug.middleware.proxy_fix import ProxyFix
2022-04-08 19:28:22 +00:00
from flask_migrate import upgrade
2022-04-08 19:29:23 +00:00
from pathlib import Path
2022-06-18 11:05:18 +00:00
from flask import Flask
2022-02-11 15:10:40 +00:00
import logging
import os
2022-02-06 22:57:01 +00:00
2022-06-18 11:05:18 +00:00
def entry_point() -> None:
2022-02-11 15:10:40 +00:00
logging.basicConfig(level=os.environ.get("LOGLEVEL", "INFO"))
2022-02-06 22:57:01 +00:00
parser = argparse.ArgumentParser(description='lenticular_cloud cli')
subparsers = parser.add_subparsers()
parser_user = subparsers.add_parser('user')
parser_user.set_defaults(func=cli_user)
parser_signup = subparsers.add_parser('signup')
2022-06-17 11:38:49 +00:00
parser_signup.add_argument('--signup_id', type=str)
2022-02-06 22:57:01 +00:00
parser_signup.set_defaults(func=cli_signup)
2022-02-11 15:10:40 +00:00
parser_run = subparsers.add_parser('run')
parser_run.set_defaults(func=cli_run)
2022-04-08 19:28:22 +00:00
parser_db_upgrade = subparsers.add_parser('db_upgrade')
parser_db_upgrade.set_defaults(func=cli_db_upgrade)
2022-02-06 22:57:01 +00:00
'''
parser_upcoming = subparsers.add_parser('upcoming')
parser_upcoming.set_defaults(func=cli_upcoming)
parser_upcoming.add_argument('-a', '--all', help='shows all single order`', nargs='?', default=False, const=True,
type=bool,
required=False)
parser_upcoming.add_argument('-n', '--no-import', dest='noimport', help='do not a automatic import', nargs='?',
default=False, type=bool, required=False)
parser_upcoming.add_argument('-F', '--format', help='format can be `d`|`m`|`y`', default='d', required=False)
# parser_select.add_argument('-F', '--format', help='format can be `d`|`m`|`y`', default='d', required=False)
# parser_select.add_argument('-f', '--from', help='from date in the format `yyyy-mm-dd`', required=False)
# parser_select.add_argument('-t', '--to', help='to date in the format `yyyy-mm-dd`', required=False)
'''
args = parser.parse_args()
if 'func' not in args:
parser.print_help()
return
app = create_app()
2022-02-11 15:10:40 +00:00
if args.func == cli_run:
cli_run(app,args)
return
2022-02-06 22:57:01 +00:00
with app.app_context():
args.func(args)
2022-06-18 11:05:18 +00:00
def cli_user(args) -> None:
2022-06-17 11:38:49 +00:00
for user in User.query.all():
print(f'{user.id} - Enabled: {user.enabled} - Name:`{user.username}`')
2022-02-06 22:57:01 +00:00
pass
2022-06-18 11:05:18 +00:00
def cli_signup(args) -> None:
2022-06-17 11:38:49 +00:00
2022-02-06 22:57:01 +00:00
if args.signup_id is not None:
2022-06-17 11:38:49 +00:00
user = User.query.get(args.signup_id)
if user == None:
print("user not found")
return
user.enabled = True
2022-02-06 22:57:01 +00:00
db.session.commit()
else:
# list
2022-06-17 11:38:49 +00:00
print('disabled users:')
for user in User.query.filter_by(enabled=False).all():
print(f'<Signup id={user.id}, username={user.username}>')
2022-02-06 22:57:01 +00:00
2022-06-18 11:05:18 +00:00
def cli_run(app: Flask, args) -> None:
print("running in debug mode")
2022-02-11 15:10:40 +00:00
logging.basicConfig(level=logging.DEBUG)
2022-06-18 11:05:18 +00:00
#app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1)
app.run(debug=False, host='127.0.0.1', port=5000)
2022-02-11 15:10:40 +00:00
2022-06-18 11:05:18 +00:00
def cli_db_upgrade(args) -> None:
2022-04-08 19:29:23 +00:00
app = create_app()
migration_dir = Path(app.root_path) / 'migrations'
upgrade( str(migration_dir) )
2022-04-08 19:28:22 +00:00
2022-02-06 22:57:01 +00:00
if __name__ == "__main__":
entry_point()