Skip to content

Commit

Permalink
Merge pull request #25 from realpython/flask-cli
Browse files Browse the repository at this point in the history
updated dependencies, removed flask script, added flask cli
  • Loading branch information
mjhea0 committed Jan 30, 2018
2 parents 62403ed + d4c00d2 commit 28af16a
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .python-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.6.3
3.6.4
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
The MIT License (MIT)
Copyright (c) 2017 Michael Herman
Copyright (c) 2018 Michael Herman

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:

Expand Down
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,19 @@ $ python manage.py create_data

### Run the Application

With debug mode:

```sh
$ python manage.py runserver
$ export FLASK_DEBUG=1 && python manage.py run
```

Access the application at the address [http://localhost:5000/](http://localhost:5000/)
Without debug mode:

> Want to specify a different port?
```sh
$ export FLASK_DEBUG=0 && python manage.py run
```

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

### Testing

Expand Down
67 changes: 32 additions & 35 deletions manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@


import unittest
import coverage

from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
import coverage
from flask.cli import FlaskGroup

from project.server import create_app, db
from project.server.models import User


app = create_app()
cli = FlaskGroup(create_app=create_app)

# code coverage
COV = coverage.coverage(
branch=True,
Expand All @@ -22,15 +25,33 @@
)
COV.start()

app = create_app()
migrate = Migrate(app, db)
manager = Manager(app)
@cli.command()
def create_db():
db.drop_all()
db.create_all()
db.session.commit()


@cli.command()
def drop_db():
"""Drops the db tables."""
db.drop_all()


@cli.command()
def create_admin():
"""Creates the admin user."""
db.session.add(User(email='ad@min.com', password='admin', admin=True))
db.session.commit()


# migrations
manager.add_command('db', MigrateCommand)
@cli.command()
def create_data():
"""Creates sample data."""
pass


@manager.command
@cli.command()
def test():
"""Runs the unit tests without test coverage."""
tests = unittest.TestLoader().discover('project/tests', pattern='test*.py')
Expand All @@ -40,7 +61,7 @@ def test():
return 1


@manager.command
@cli.command()
def cov():
"""Runs the unit tests with coverage."""
tests = unittest.TestLoader().discover('project/tests')
Expand All @@ -56,30 +77,6 @@ def cov():
return 1


@manager.command
def create_db():
"""Creates the db tables."""
db.create_all()


@manager.command
def drop_db():
"""Drops the db tables."""
db.drop_all()


@manager.command
def create_admin():
"""Creates the admin user."""
db.session.add(User(email='ad@min.com', password='admin', admin=True))
db.session.commit()


@manager.command
def create_data():
"""Creates sample data."""
pass


if __name__ == '__main__':
manager.run()
cli()
5 changes: 4 additions & 1 deletion project/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
migrate = Migrate()


def create_app():
def create_app(script_info=None):

# instantiate the app
app = Flask(
Expand Down Expand Up @@ -77,4 +77,7 @@ def page_not_found(error):
def server_error_page(error):
return render_template('errors/500.html'), 500

# shell context for flask cli
app.shell_context_processor({'app': app, 'db': db})

return app
4 changes: 0 additions & 4 deletions project/server/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
class BaseConfig(object):
"""Base configuration."""
SECRET_KEY = 'my_precious'
DEBUG = False
BCRYPT_LOG_ROUNDS = 13
WTF_CSRF_ENABLED = True
DEBUG_TB_ENABLED = False
Expand All @@ -17,7 +16,6 @@ class BaseConfig(object):

class DevelopmentConfig(BaseConfig):
"""Development configuration."""
DEBUG = True
BCRYPT_LOG_ROUNDS = 4
WTF_CSRF_ENABLED = False
SQLALCHEMY_DATABASE_URI = 'sqlite:///{0}'.format(
Expand All @@ -27,7 +25,6 @@ class DevelopmentConfig(BaseConfig):

class TestingConfig(BaseConfig):
"""Testing configuration."""
DEBUG = True
TESTING = True
BCRYPT_LOG_ROUNDS = 4
WTF_CSRF_ENABLED = False
Expand All @@ -39,6 +36,5 @@ class TestingConfig(BaseConfig):
class ProductionConfig(BaseConfig):
"""Production configuration."""
SECRET_KEY = 'my_precious'
DEBUG = False
SQLALCHEMY_DATABASE_URI = 'postgresql://localhost/example'
DEBUG_TB_ENABLED = False
3 changes: 0 additions & 3 deletions project/tests/test__config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def create_app(self):

def test_app_is_development(self):
self.assertFalse(current_app.config['TESTING'])
self.assertTrue(app.config['DEBUG'] is True)
self.assertTrue(app.config['WTF_CSRF_ENABLED'] is False)
self.assertTrue(app.config['DEBUG_TB_ENABLED'] is True)
self.assertFalse(current_app is None)
Expand All @@ -33,7 +32,6 @@ def create_app(self):

def test_app_is_testing(self):
self.assertTrue(current_app.config['TESTING'])
self.assertTrue(app.config['DEBUG'] is True)
self.assertTrue(app.config['BCRYPT_LOG_ROUNDS'] == 4)
self.assertTrue(app.config['WTF_CSRF_ENABLED'] is False)

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

def test_app_is_production(self):
self.assertFalse(current_app.config['TESTING'])
self.assertTrue(app.config['DEBUG'] is False)
self.assertTrue(app.config['DEBUG_TB_ENABLED'] is False)
self.assertTrue(app.config['WTF_CSRF_ENABLED'] is True)
self.assertTrue(app.config['BCRYPT_LOG_ROUNDS'] == 13)
Expand Down
5 changes: 2 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ Flask==0.12.2
Flask-Bcrypt==0.7.1
Flask-Bootstrap==3.3.7.1
Flask-DebugToolbar==0.10.1
Flask-Login==0.4.0
Flask-Login==0.4.1
Flask-Migrate==2.1.1
Flask-Script==2.0.6
Flask-SQLAlchemy==2.3.2
Flask-Testing==0.6.2
Flask-Testing==0.7.1
Flask-WTF==0.14.2

0 comments on commit 28af16a

Please sign in to comment.