-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
executable file
·154 lines (123 loc) · 4.02 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
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
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python
import os
from app import create_app, db
from app.models import (
User,
Role,
Agency,
Permission,
IncidentReport,
EditableHTML
)
from flask.ext.script import Manager, Shell
from flask.ext.migrate import Migrate, MigrateCommand
from flask.ext.rq import get_worker
from app.parse_csv import parse_to_db
# Import settings from .env file. Must define FLASK_CONFIG
if os.path.exists('.env'):
print('Importing environment from .env file')
for line in open('.env'):
var = line.strip().split('=')
if len(var) == 2:
os.environ[var[0]] = var[1]
app = create_app(os.getenv('FLASK_CONFIG') or 'default')
manager = Manager(app)
migrate = Migrate(app, db)
def make_shell_context():
return dict(app=app, db=db, User=User, Role=Role)
manager.add_command('shell', Shell(make_context=make_shell_context))
manager.add_command('db', MigrateCommand)
@manager.command
def test():
"""Run the unit tests."""
import unittest
tests = unittest.TestLoader().discover('tests')
unittest.TextTestRunner(verbosity=2).run(tests)
@manager.command
def recreate_db():
"""
Recreates a local database. You probably should not use this on
production.
"""
db.drop_all()
db.create_all()
db.session.commit()
@manager.option('-nu',
'--number-users',
default=10,
type=int,
help='Number of users to create',
dest='number_users')
@manager.option('-nr',
'--number-reports',
default=100,
type=int,
help='Number of reports to create',
dest='number_reports')
def add_fake_data(number_users, number_reports):
"""
Adds fake data to the database.
"""
User.generate_fake(count=number_users)
IncidentReport.generate_fake(count=number_reports)
@manager.command
def setup_dev():
"""Runs the set-up needed for local development."""
setup_general()
# Create a default admin user
admin = User(email='[email protected]',
phone_number='+12345678910',
password='password',
first_name='Admin',
last_name='User',
role=Role.query.filter_by(permissions=Permission.ADMINISTER)
.first(),
confirmed=True)
# Create a default agency worker user
worker = User(email='[email protected]',
phone_number='+11098764321',
password='password',
first_name='AgencyWorker',
last_name='User',
role=Role.query
.filter_by(permissions=Permission.AGENCY_WORKER)
.first(),
confirmed=True)
worker.agencies = [Agency.get_agency_by_name('SEPTA')]
# Create a default general user
general = User(email='[email protected]',
phone_number='+15434549876',
password='password',
first_name='General',
last_name='User',
role=Role.query.filter_by(permissions=Permission.GENERAL)
.first(),
confirmed=True)
db.session.add(admin)
db.session.add(worker)
db.session.add(general)
db.session.commit()
@manager.option('-f',
'--filename',
default='poll244.csv',
type=str,
help='Filename of csv to parse',
dest='filename')
def parse_csv(filename):
"""Parses the given csv file into the database."""
parse_to_db(db, filename)
@manager.command
def setup_prod():
"""Runs the set-up needed for production."""
setup_general()
def setup_general():
"""Runs the set-up needed for both local development and production."""
Role.insert_roles()
Agency.insert_agencies()
EditableHTML.add_default_faq()
@manager.command
def run_worker():
"""Initializes a slim rq task queue."""
get_worker().work()
if __name__ == '__main__':
manager.run()