-
Notifications
You must be signed in to change notification settings - Fork 27
/
manager.py
87 lines (69 loc) · 2.07 KB
/
manager.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
# -*- coding:utf-8 -*-
# Run a test server.
import os
import unittest
from flask_migrate import Migrate, MigrateCommand
from flask_script import Manager
from werkzeug.serving import run_simple
from werkzeug.wsgi import DispatcherMiddleware
from app import create_app, db
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]
if os.environ.get('FLATCOKE') == 'production':
app = DispatcherMiddleware(create_app(os.environ.get("FLATCOKE")), {
'/api': create_app('api')
})
else:
app = create_app(os.environ.get("FLATCOKE") or 'development')
manager = Manager(app)
migrate = Migrate(app, db)
manager.add_command('db', MigrateCommand)
@manager.command
def hello():
"""say hello for testing"""
print('hello')
@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.command
def seed():
from app.users.models import User, Address
from faker import Faker
fake = Faker()
users = [User(username=fake.user_name(), name=fake.name(),
email=fake.email(), password='qwer1234') for i in range(20)]
db.session.add_all(users)
db.session.commit()
addresses = []
for user in users:
address = Address()
address.user = user
address.address = fake.address()
addresses.append(address)
db.session.add_all(addresses)
db.session.commit()
@manager.command
def test():
"""Run unit tests."""
tests = unittest.TestLoader().discover('app.tests', pattern='*.py')
unittest.TextTestRunner(verbosity=1).run(tests)
@manager.command
def dev():
# TODO add TestingConfig
pass
if __name__ == '__main__':
if os.environ.get('FLATCOKE') == 'production':
run_simple('0.0.0.0', 5000, app,
use_reloader=False, use_debugger=False,
)
manager.run()