/
manage.py
executable file
·104 lines (82 loc) · 2.98 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
#!/usr/bin/env python
import os
from app import create_app, db
from app.models import User, Role, ZIPCode, Address, DonorLevel, Resource, \
ResourceReview, Tag, AffiliationTag, ResourceCategoryTag
from flask.ext.script import Manager, Shell
from flask.ext.migrate import Migrate, MigrateCommand
# 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, ZIPCode=ZIPCode,
Address=Address, DonorLevel=DonorLevel, Tag=Tag,
ResourceCategoryTag=ResourceCategoryTag,
AffiliationTag=AffiliationTag)
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('-n',
'--fake-count',
default=10,
type=int,
help='Number of each model type to create',
dest='count')
def add_fake_data(count):
"""
Adds fake data to the database.
"""
User.generate_fake(count=count)
ZIPCode.generate_fake()
Resource.generate_fake()
ResourceReview.generate_fake(count=count)
Address.generate_fake()
AffiliationTag.generate_default()
# Set a random zip for each user without one.
User.set_random_zip_codes(User.query.filter_by(zip_code=None).all(),
ZIPCode.query.all())
# Set a random affiliation tag for each user.
User.set_random_affiliation_tags(User.query.all(),
AffiliationTag.query.all())
@manager.command
def setup_dev():
"""Runs the set-up needed for local development."""
setup_general()
admin_email = 'wvr@gmail.com'
if User.query.filter_by(email=admin_email).first() is None:
User.create_confirmed_admin('Default',
'Admin',
admin_email,
'password',
ZIPCode.create_zip_code('19104'))
@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()
DonorLevel.insert_donor_levels()
if __name__ == '__main__':
manager.run()