-
Notifications
You must be signed in to change notification settings - Fork 21
/
manage.py
executable file
·162 lines (136 loc) · 4.25 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
155
156
157
158
159
160
161
162
#!/usr/bin/env python
import os
from app import create_app, db
from app.models import User, Role, Post, Follow, FirmType, FirmTier, Firm, \
Company, Relationship, Geo, UserType
from flask_script import Manager, Shell
from flask_migrate import Migrate, MigrateCommand
application = create_app(os.getenv('FLASK_CONFIG', 'default'))
manager = Manager(application)
migrate = Migrate(application, db)
def make_shell_context():
"""
Automatically import application, db, and model objects into interactive
shell.
"""
return dict(application=application, db=db, User=User, Geo=Geo, Role=Role,
Follow=Follow, FirmType=FirmType, FirmTier=FirmTier, Firm=Firm,
Company=Company, Relationship=Relationship, UserType=UserType)
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 db_rebuild():
"""
Destroy and rebuild database with fake data.
"""
# destroy and rebuild tables
db.reflect()
db.drop_all()
db.create_all()
# insert roles as defined in model
Role.insert_roles()
# insert geos and usertypes as defined in model
Geo.insert_geos()
UserType.insert_user_types()
# insert firm types/tiers as defined in model
FirmType.insert_firm_types()
FirmTier.insert_firm_tiers()
# insert fake admin/test users
from random import seed
import forgery_py
seed()
test_user_1 = User(
email='[email protected]',
username='salimham',
password='password',
confirmed=True,
name='Salim Hamed',
location='Seattle, WA',
about_me=forgery_py.lorem_ipsum.sentence(),
member_since=forgery_py.date.date(True)
)
test_user_2 = User(
email='[email protected]',
username='bryan',
password='password',
confirmed=True,
name='Bryan Davis',
location='Seattle, WA',
about_me=forgery_py.lorem_ipsum.sentence(),
member_since=forgery_py.date.date(True)
)
test_user_3 = User(
email='[email protected]',
username='joe',
password='password',
confirmed=True,
name='Joe Smith',
location='San Francisco, CA',
about_me=forgery_py.lorem_ipsum.sentence(),
member_since=forgery_py.date.date(True)
)
test_user_4 = User(
email='[email protected]',
username='bill',
password='password',
confirmed=True,
name='Bill Gates',
location='Bellevue, WA',
about_me=forgery_py.lorem_ipsum.sentence(),
member_since=forgery_py.date.date(True)
)
admin_user = User(
email='[email protected]',
username='admin',
password='password',
confirmed=True,
name='Bill Gates',
location='Seattle, WA',
about_me=forgery_py.lorem_ipsum.sentence(),
member_since=forgery_py.date.date(True)
)
db.session.add_all([test_user_1, test_user_2, test_user_3, test_user_4,
admin_user])
db.session.commit()
# insert fake user data
User.generate_fake(60)
# insert fake post data
Post.generate_fake(400)
# insert fake followers
Follow.generate_fake(2000)
# insert fake firms
Firm.generate_fake(5000)
# insert fake companies
Company.generate_fake(10000)
# insert fake relationships
Relationship.generate_fake(60000)
# print results
inspector = db.inspect(db.engine)
print 'The following tables were created.'
print '-'*17
for table in inspector.get_table_names():
print table
@manager.command
def deploy():
"""Run deployment tasks."""
from flask.ext.migrate import upgrade
# migrate database to latest revision
upgrade()
# create user roles
Role.insert_roles()
# insert geos and usertypes as defined in model
Geo.insert_geos()
UserType.insert_user_types()
# insert firm types/tiers as defined in model
FirmType.insert_firm_types()
FirmTier.insert_firm_tiers()
if __name__ == '__main__':
manager.run()