-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.py
145 lines (111 loc) · 3.86 KB
/
cli.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
'''
db_manager.py
Stuff for standing up the admin database. CLI provided by Python-Click.
'''
import click
from flask_security.utils import encrypt_password
from core import application as app
from core import application_db as db
from core.admin import user_datastore
from core.models import Role
from core.api.db_interface import get_all_fishfries
from datetime import datetime
from pathlib import Path
import json
# ---------------------------------------------------------
# helpers
def create_user(email, password, role):
"""Create a new user in the database
Arguments:
email {[type]} -- [description]
password {[type]} -- [description]
role {[type]} -- [description]
Returns:
An instance of the User model
"""
new_user = user_datastore.create_user(
email=email,
password=encrypt_password(password),
roles=[role]
)
db.session.commit()
return new_user
def create_role(role):
"""[summary]
Arguments:
role {string} -- [description]
Returns:
instance of Role model
"""
new_role = Role(name=role)
db.session.add(new_role)
db.session.commit()
return new_role
# ---------------------------------------------------------
# commands
@click.group()
def manage_db():
pass
@click.command()
@click.option('--role', prompt=True)
def new_role(role):
create_role(role)
@click.command()
@click.option('--email', prompt=True)
@click.option('--password', prompt=True, hide_input=True, confirmation_prompt=True)
@click.option('--role', prompt=True, type=click.Choice(['admin', 'contributor']))
def new_user(email, password, role):
create_user(email, password, role)
@click.command()
@click.confirmation_option(help='Are you sure you want to create a fresh database? This will overwrite any existing database specified in "/core/config.py"')
def bootstrap_db():
'''This stands up a completely fresh user database for the admin-side of the application, with a test admin and test contributor. Use for dev/testing.
NOTE: This uses the database path specified in /core.config.py. It will overwrite the existing application database tables unless you've pointed the 'core/config.py' file to point to another sqlite database.
'''
with app.app_context():
#---------------------------------------#
# FRESH DB
click.echo("Dropping tables...")
db.drop_all()
click.echo("Creating fresh tables...")
db.create_all()
#---------------------------------------#
# CREATE ROLES in ROLES TABLE
click.echo("Creating roles...")
role_admin = create_role('admin')
role_contributor = create_role('contributor')
#---------------------------------------#
# CREATE USERS in USERS TABLE
test_users = [
{
"email": "[email protected]",
"password": "adm1nt3st",
"role": role_admin,
}, {
"email": "[email protected]",
"password": "contr1bt3st",
"role": role_contributor,
}
]
for user in test_users:
new_user = create_user(
email=user['email'],
password=user['password'],
role=user['role'],
)
click.echo("Created user {0}".format(new_user.email))
return
@click.command()
@click.option('--path')
def dump_geojson(path):
fries = get_all_fishfries()
out_file = Path(path) / "fishfry_{0}.geojson".format(datetime.now().strftime('%Y%d%mT%H%M%S'))
with open(str(out_file), 'w') as fp:
json.dump(fries, fp)
click.echo("dumped fishfries to {0}".format(str(out_file)))
manage_db.add_command(new_user)
manage_db.add_command(new_role)
manage_db.add_command(bootstrap_db)
manage_db.add_command(dump_geojson)
if __name__ == '__main__':
manage_db()