Remove env parameter from app factory function

master
Steven Loria 11 years ago
parent 7a0aa255af
commit 4aea7377a1
  1. 2
      {{cookiecutter.repo_name}}/manage.py
  2. 4
      {{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/app.py
  3. 2
      {{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/main.py
  4. 1
      {{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/modules/public.py
  5. 2
      {{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/settings.py
  6. 2
      {{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/tests/test_models.py

@ -10,7 +10,7 @@ from {{cookiecutter.repo_name}}.models import db
env = os.environ.get("{{cookiecutter.repo_name | upper }}_ENV", 'prod') env = os.environ.get("{{cookiecutter.repo_name | upper }}_ENV", 'prod')
app = create_app("{{cookiecutter.repo_name}}.settings.{0}Config" app = create_app("{{cookiecutter.repo_name}}.settings.{0}Config"
.format(env.capitalize()), env) .format(env.capitalize()))
manager = Manager(app) manager = Manager(app)
TEST_CMD = "nosetests" TEST_CMD = "nosetests"

@ -10,16 +10,14 @@ from {{cookiecutter.repo_name}}.models import db
assets_env = Environment() assets_env = Environment()
def create_app(config_object, env): def create_app(config_object):
'''An application factory, as explained here: '''An application factory, as explained here:
http://flask.pocoo.org/docs/patterns/appfactories/ http://flask.pocoo.org/docs/patterns/appfactories/
:param config_object: The configuration object to use. :param config_object: The configuration object to use.
:param env: A string, the current environment. Either "dev" or "prod"
''' '''
app = Flask(__name__) app = Flask(__name__)
app.config.from_object(config_object) app.config.from_object(config_object)
app.config['ENV'] = env
# Initialize SQLAlchemy # Initialize SQLAlchemy
db.init_app(app) db.init_app(app)
# Register asset bundles # Register asset bundles

@ -12,4 +12,4 @@ if __name__ == '__main__':
# Get the environment setting from the system environment variable # Get the environment setting from the system environment variable
env = os.environ.get("{{cookiecutter.repo_name | upper}}_ENV", "prod") env = os.environ.get("{{cookiecutter.repo_name | upper}}_ENV", "prod")
app = create_app("{{cookiecutter.repo_name}}.settings.{env}Config" app = create_app("{{cookiecutter.repo_name}}.settings.{env}Config"
.format(env=env.capitalize()), env) .format(env=env.capitalize()))

@ -47,7 +47,6 @@ def register():
flash("Thank you for registering. You can now log in.", 'success') flash("Thank you for registering. You can now log in.", 'success')
return redirect(url_for('public.home')) return redirect(url_for('public.home'))
except IntegrityError as err: except IntegrityError as err:
print(err)
flash("That username and/or email already exists. Try again.", 'warning') flash("That username and/or email already exists. Try again.", 'warning')
else: else:
flash_errors(form) flash_errors(form)

@ -7,11 +7,13 @@ class Config(object):
PROJECT_ROOT = os.path.abspath(os.path.join(APP_DIR, os.pardir)) PROJECT_ROOT = os.path.abspath(os.path.join(APP_DIR, os.pardir))
class ProdConfig(Config): class ProdConfig(Config):
ENV = 'prod'
DEBUG = False DEBUG = False
SQLALCHEMY_DATABASE_URI = 'postgresql://localhost/example' SQLALCHEMY_DATABASE_URI = 'postgresql://localhost/example'
SQLALCHEMY_ECHO = False SQLALCHEMY_ECHO = False
class DevConfig(Config): class DevConfig(Config):
ENV = 'dev'
DEBUG = True DEBUG = True
DB_NAME = "dev.db" DB_NAME = "dev.db"
# Put the db file in project root # Put the db file in project root

@ -13,7 +13,7 @@ class TestUser(TestCase):
SQLALCHEMY_DATABASE_URI = 'sqlite://' SQLALCHEMY_DATABASE_URI = 'sqlite://'
def create_app(self): def create_app(self):
app = create_app(self, 'testing') app = create_app(self)
with app.app_context(): with app.app_context():
db.create_all() db.create_all()
return app return app

Loading…
Cancel
Save