Skip to content

Commit 28af16a

Browse files
authored
Merge pull request #25 from realpython/flask-cli
updated dependencies, removed flask script, added flask cli
2 parents 62403ed + d4c00d2 commit 28af16a

File tree

8 files changed

+48
-54
lines changed

8 files changed

+48
-54
lines changed

.python-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.6.3
1+
3.6.4

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
The MIT License (MIT)
2-
Copyright (c) 2017 Michael Herman
2+
Copyright (c) 2018 Michael Herman
33

44
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
55

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,19 @@ $ python manage.py create_data
3737

3838
### Run the Application
3939

40+
With debug mode:
41+
4042
```sh
41-
$ python manage.py runserver
43+
$ export FLASK_DEBUG=1 && python manage.py run
4244
```
4345

44-
Access the application at the address [http://localhost:5000/](http://localhost:5000/)
46+
Without debug mode:
4547

46-
> Want to specify a different port?
48+
```sh
49+
$ export FLASK_DEBUG=0 && python manage.py run
50+
```
4751

48-
> ```sh
49-
> $ python manage.py runserver -h 0.0.0.0 -p 8080
50-
> ```
52+
Access the application at the address [http://localhost:5000/](http://localhost:5000/)
5153

5254
### Testing
5355

manage.py

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22

33

44
import unittest
5-
import coverage
65

7-
from flask_script import Manager
8-
from flask_migrate import Migrate, MigrateCommand
6+
import coverage
7+
from flask.cli import FlaskGroup
98

109
from project.server import create_app, db
1110
from project.server.models import User
1211

12+
13+
app = create_app()
14+
cli = FlaskGroup(create_app=create_app)
15+
1316
# code coverage
1417
COV = coverage.coverage(
1518
branch=True,
@@ -22,15 +25,33 @@
2225
)
2326
COV.start()
2427

25-
app = create_app()
26-
migrate = Migrate(app, db)
27-
manager = Manager(app)
28+
@cli.command()
29+
def create_db():
30+
db.drop_all()
31+
db.create_all()
32+
db.session.commit()
33+
34+
35+
@cli.command()
36+
def drop_db():
37+
"""Drops the db tables."""
38+
db.drop_all()
39+
40+
41+
@cli.command()
42+
def create_admin():
43+
"""Creates the admin user."""
44+
db.session.add(User(email='[email protected]', password='admin', admin=True))
45+
db.session.commit()
46+
2847

29-
# migrations
30-
manager.add_command('db', MigrateCommand)
48+
@cli.command()
49+
def create_data():
50+
"""Creates sample data."""
51+
pass
3152

3253

33-
@manager.command
54+
@cli.command()
3455
def test():
3556
"""Runs the unit tests without test coverage."""
3657
tests = unittest.TestLoader().discover('project/tests', pattern='test*.py')
@@ -40,7 +61,7 @@ def test():
4061
return 1
4162

4263

43-
@manager.command
64+
@cli.command()
4465
def cov():
4566
"""Runs the unit tests with coverage."""
4667
tests = unittest.TestLoader().discover('project/tests')
@@ -56,30 +77,6 @@ def cov():
5677
return 1
5778

5879

59-
@manager.command
60-
def create_db():
61-
"""Creates the db tables."""
62-
db.create_all()
63-
64-
65-
@manager.command
66-
def drop_db():
67-
"""Drops the db tables."""
68-
db.drop_all()
69-
70-
71-
@manager.command
72-
def create_admin():
73-
"""Creates the admin user."""
74-
db.session.add(User(email='[email protected]', password='admin', admin=True))
75-
db.session.commit()
76-
77-
78-
@manager.command
79-
def create_data():
80-
"""Creates sample data."""
81-
pass
82-
8380

8481
if __name__ == '__main__':
85-
manager.run()
82+
cli()

project/server/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
migrate = Migrate()
2222

2323

24-
def create_app():
24+
def create_app(script_info=None):
2525

2626
# instantiate the app
2727
app = Flask(
@@ -77,4 +77,7 @@ def page_not_found(error):
7777
def server_error_page(error):
7878
return render_template('errors/500.html'), 500
7979

80+
# shell context for flask cli
81+
app.shell_context_processor({'app': app, 'db': db})
82+
8083
return app

project/server/config.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
class BaseConfig(object):
88
"""Base configuration."""
99
SECRET_KEY = 'my_precious'
10-
DEBUG = False
1110
BCRYPT_LOG_ROUNDS = 13
1211
WTF_CSRF_ENABLED = True
1312
DEBUG_TB_ENABLED = False
@@ -17,7 +16,6 @@ class BaseConfig(object):
1716

1817
class DevelopmentConfig(BaseConfig):
1918
"""Development configuration."""
20-
DEBUG = True
2119
BCRYPT_LOG_ROUNDS = 4
2220
WTF_CSRF_ENABLED = False
2321
SQLALCHEMY_DATABASE_URI = 'sqlite:///{0}'.format(
@@ -27,7 +25,6 @@ class DevelopmentConfig(BaseConfig):
2725

2826
class TestingConfig(BaseConfig):
2927
"""Testing configuration."""
30-
DEBUG = True
3128
TESTING = True
3229
BCRYPT_LOG_ROUNDS = 4
3330
WTF_CSRF_ENABLED = False
@@ -39,6 +36,5 @@ class TestingConfig(BaseConfig):
3936
class ProductionConfig(BaseConfig):
4037
"""Production configuration."""
4138
SECRET_KEY = 'my_precious'
42-
DEBUG = False
4339
SQLALCHEMY_DATABASE_URI = 'postgresql://localhost/example'
4440
DEBUG_TB_ENABLED = False

project/tests/test__config.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ def create_app(self):
1919

2020
def test_app_is_development(self):
2121
self.assertFalse(current_app.config['TESTING'])
22-
self.assertTrue(app.config['DEBUG'] is True)
2322
self.assertTrue(app.config['WTF_CSRF_ENABLED'] is False)
2423
self.assertTrue(app.config['DEBUG_TB_ENABLED'] is True)
2524
self.assertFalse(current_app is None)
@@ -33,7 +32,6 @@ def create_app(self):
3332

3433
def test_app_is_testing(self):
3534
self.assertTrue(current_app.config['TESTING'])
36-
self.assertTrue(app.config['DEBUG'] is True)
3735
self.assertTrue(app.config['BCRYPT_LOG_ROUNDS'] == 4)
3836
self.assertTrue(app.config['WTF_CSRF_ENABLED'] is False)
3937

@@ -46,7 +44,6 @@ def create_app(self):
4644

4745
def test_app_is_production(self):
4846
self.assertFalse(current_app.config['TESTING'])
49-
self.assertTrue(app.config['DEBUG'] is False)
5047
self.assertTrue(app.config['DEBUG_TB_ENABLED'] is False)
5148
self.assertTrue(app.config['WTF_CSRF_ENABLED'] is True)
5249
self.assertTrue(app.config['BCRYPT_LOG_ROUNDS'] == 13)

requirements.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ Flask==0.12.2
33
Flask-Bcrypt==0.7.1
44
Flask-Bootstrap==3.3.7.1
55
Flask-DebugToolbar==0.10.1
6-
Flask-Login==0.4.0
6+
Flask-Login==0.4.1
77
Flask-Migrate==2.1.1
8-
Flask-Script==2.0.6
98
Flask-SQLAlchemy==2.3.2
10-
Flask-Testing==0.6.2
9+
Flask-Testing==0.7.1
1110
Flask-WTF==0.14.2

0 commit comments

Comments
 (0)