Skip to content
This repository was archived by the owner on Jan 15, 2022. It is now read-only.

Commit 7a39b2f

Browse files
committed
Switch from Flask-Script to Click
1 parent 304f43d commit 7a39b2f

File tree

6 files changed

+29
-32
lines changed

6 files changed

+29
-32
lines changed

Procfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
web: gunicorn dribdat.app:create_app\(\) -b 0.0.0.0:$PORT -w 3 --log-file=-
1+
web: gunicorn dribdat.app:init_app\(\) -b 0.0.0.0:$PORT -w 3 --log-file=-
22
init: python manage.py db init
33
migrate: python manage.py db migrate
44
upgrade: python manage.py db upgrade

autoapp.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
"""Create an application instance."""
33
from flask.helpers import get_debug_flag
44

5-
from dribdat.app import create_app
5+
from dribdat.app import init_app
66
from dribdat.settings import DevConfig, ProdConfig
77

88
CONFIG = DevConfig if get_debug_flag() else ProdConfig
99

10-
app = create_app(CONFIG)
10+
app = init_app(CONFIG)

dribdat/app.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from dribdat.utils import timesince
1818
from flask_misaka import Misaka
1919

20-
def create_app(config_object=ProdConfig):
20+
def init_app(config_object=ProdConfig):
2121
"""An application factory, as explained here: http://flask.pocoo.org/docs/patterns/appfactories/.
2222
2323
:param config_object: The configuration object to use.

manage.py

+20-23
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,43 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3+
34
import os
4-
from flask_script import Manager, Shell, Server
5-
from flask_script.commands import Clean, ShowUrls
5+
6+
from flask import Flask
7+
from flask.cli import FlaskGroup
68
from flask_migrate import MigrateCommand
79

8-
from dribdat.app import create_app
10+
from dribdat.app import init_app
911
from dribdat.user.models import User
1012
from dribdat.settings import DevConfig, ProdConfig
1113
from dribdat.database import db
1214

13-
if os.environ.get("DRIBDAT_ENV") == 'prod':
14-
app = create_app(ProdConfig)
15-
else:
16-
app = create_app(DevConfig)
17-
1815
HERE = os.path.abspath(os.path.dirname(__file__))
1916
TEST_PATH = os.path.join(HERE, 'tests')
2017

21-
manager = Manager(app)
22-
23-
24-
def _make_context():
25-
"""Return context dict for a shell session so you can access
26-
app, db, and the User model by default.
27-
"""
18+
def shell_context():
19+
"""Return context dict for a shell session"""
2820
return {'app': app, 'db': db, 'User': User}
2921

22+
def create_app(script_info=None):
23+
"""Initialise the app object"""
24+
if os.environ.get("DRIBDAT_ENV") == 'prod':
25+
app = init_app(ProdConfig)
26+
else:
27+
app = init_app(DevConfig)
28+
app.shell_context_processor(shell_context)
29+
return app
30+
31+
cli = FlaskGroup(create_app=create_app)
3032

31-
@manager.command
33+
@cli.command
3234
def test():
3335
"""Run the tests."""
3436
import pytest
3537
exit_code = pytest.main([TEST_PATH, '--verbose'])
3638
return exit_code
3739

38-
39-
manager.add_command('server', Server(port=5000))
40-
manager.add_command('shell', Shell(make_context=_make_context))
41-
manager.add_command('db', MigrateCommand)
42-
manager.add_command("urls", ShowUrls())
43-
manager.add_command("clean", Clean())
40+
cli.add_command('db', MigrateCommand)
4441

4542
if __name__ == '__main__':
46-
manager.run()
43+
cli()

tests/conftest.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import pytest
55
from webtest import TestApp
66

7-
from dribdat.app import create_app
7+
from dribdat.app import init_app
88
from dribdat.database import db as _db
99
from dribdat.settings import TestConfig
1010

@@ -14,7 +14,7 @@
1414
@pytest.yield_fixture(scope='function')
1515
def app():
1616
"""An application for the tests."""
17-
_app = create_app(TestConfig)
17+
_app = init_app(TestConfig)
1818
ctx = _app.test_request_context()
1919
ctx.push()
2020

tests/test_config.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# -*- coding: utf-8 -*-
22
"""Test configs."""
3-
from dribdat.app import create_app
3+
from dribdat.app import init_app
44
from dribdat.settings import DevConfig, ProdConfig
55

66

77
def test_production_config():
88
"""Production config."""
9-
app = create_app(ProdConfig)
9+
app = init_app(ProdConfig)
1010
assert app.config['ENV'] == 'prod'
1111
assert app.config['DEBUG'] is False
1212
assert app.config['DEBUG_TB_ENABLED'] is False
@@ -15,7 +15,7 @@ def test_production_config():
1515

1616
def test_dev_config():
1717
"""Development config."""
18-
app = create_app(DevConfig)
18+
app = init_app(DevConfig)
1919
assert app.config['ENV'] == 'dev'
2020
assert app.config['DEBUG'] is True
2121
assert app.config['ASSETS_DEBUG'] is True

0 commit comments

Comments
 (0)