-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanage.py
56 lines (45 loc) · 1.63 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
import os
import shutil
from flask_script import Manager, prompt_bool
from flask_migrate import MigrateCommand
from colandr import create_app, db
from colandr.models import User
from colandr.config import configs
manager = Manager(create_app)
manager.add_option(
'-c', '--config', dest='config_name', type=str,
choices=sorted(configs.keys()),
default=os.getenv('COLANDR_FLASK_CONFIG', 'default'))
manager.add_command('db', MigrateCommand)
@manager.command
def reset():
"""
Drop and then create all tables in the database, clear out all uploaded
fulltext files and ranking models on disk, and create an admin user.
"""
if prompt_bool("Are you sure you want to reset ALL app data?") is False:
return
db.drop_all()
db.create_all()
for dirkey in ('FULLTEXT_UPLOADS_DIR', 'RANKING_MODELS_DIR'):
shutil.rmtree(manager.app.config[dirkey], ignore_errors=True)
os.makedirs(manager.app.config[dirkey], exist_ok=True)
@manager.option('-n', '--name', dest='name', required=True)
@manager.option('-e', '--email', dest='email', required=True)
@manager.option('-p', '--password', dest='password', required=True)
def add_admin(name, email, password):
"""
Add an admin account to the database, with both `is_admin` and `is_confirmed`
values already set to True.
"""
user = User(name, email, password)
user.is_confirmed = True
user.is_admin = True
db.session.add(user)
try:
db.session.commit()
except Exception as e:
print('an error occurred when adding admin: {}'.format(e))
db.session.rollback()
if __name__ == '__main__':
manager.run()