-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanage.py
101 lines (80 loc) · 2.61 KB
/
manage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""Main entry-point for instadam backend server.
Commands:
start Start the server
initdb Initialize the database
cleartable Clear all the table content
cleardb Clear the database
Usage:
manage.py start [--mode]
manage.py initdb [--mode]
manage.py cleardb [--mode]
manage.py cleartable [--mode]
Options:
--mode Start the api on specific mode, one of
production, development, testing
[default : production]
"""
import click
from instadam.app import create_app, db
from instadam.models.user import PrivilegesEnum, User
@click.group()
def cli():
pass
@cli.command()
def deploy():
app = create_app('production')
with app.app_context():
meta = db.metadata
for table in reversed(meta.sorted_tables):
print('Clear table %s' % table)
db.session.execute(table.delete())
db.session.commit()
db.create_all()
admin = User(
username='admin',
email='[email protected]',
privileges=PrivilegesEnum.ADMIN)
admin.set_password('AdminPassword0')
db.session.add(admin)
db.session.commit()
app.run(host='0.0.0.0', port=8080)
@cli.command()
@click.option('--mode', default='development', help='production/development')
def start(mode):
app = create_app(mode)
if mode == 'development':
with app.app_context():
db.create_all() # init in-memory Sqlite
app.run(host='0.0.0.0', port=8080)
@cli.command()
@click.option('--mode', default='development', help='production/development')
def initdb(mode):
app = create_app(mode)
with app.app_context():
db.create_all()
admin = User(
username='admin',
email='[email protected]',
privileges=PrivilegesEnum.ADMIN)
admin.set_password('AdminPassword0')
db.session.add(admin)
db.session.commit()
@cli.command()
@click.option('--mode', default='development', help='production/development')
def cleartable(mode):
app = create_app(mode)
with app.app_context():
meta = db.metadata
for table in reversed(meta.sorted_tables):
print('Clear table %s' % table)
db.session.execute(table.delete())
db.session.commit()
@cli.command()
@click.option('--mode', default='development', help='production/development')
def cleardb(mode):
app = create_app(mode)
with app.app_context():
db.reflect()
db.drop_all()
if __name__ == '__main__':
cli() # Execute the function specified by the user.