Introduce config file for application

This commit is contained in:
Rebecca Law
2015-11-24 09:40:14 +00:00
parent e14780f7b5
commit b83d2f0d8a
6 changed files with 46 additions and 27 deletions

1
.gitignore vendored
View File

@@ -63,3 +63,4 @@ assets/stylesheets/govuk_template/.sass-cache/
cache/
static/stylesheets/govuk-template*
static/css*
static/css_all.css

10
app.py
View File

@@ -1,25 +1,17 @@
import os
from flask import Flask, render_template
from flask.ext import assets
from flask.ext.script import Manager, Server
from webassets.filter import get_filter
from app import create_app
application = create_app()
application = create_app(os.getenv('NOTIFICATIONS_ADMIN_ENVIRONMENT') or 'development')
manager = Manager(application)
port = int(os.environ.get('PORT', 6012))
manager.add_command("runserver", Server(host='0.0.0.0', port=port))
# debug mode - switch to False for production
application.config['ASSETS_DEBUG'] = True
application.config['DEBUG'] = True
env = assets.Environment(application)
# debug mode - switch to True for production
env.config['cache'] = False
env.config['manifest'] = False
# Tell flask-assets where to look for our sass files.
env.load_path = [
os.path.join(os.path.dirname(__file__), 'app/assets/stylesheets'),

View File

@@ -1,12 +1,33 @@
import os
from flask import Flask
from config import configs
from flask._compat import string_types
def create_app():
def create_app(config_name):
application = Flask(__name__)
# application.config['NOTIFY_API_ENVIRONMENT'] = config_name
# application.config.from_object(configs[config_name])
application.config['NOTIFY_API_ENVIRONMENT'] = config_name
application.config.from_object(configs[config_name])
init_app(application)
from app.main import main as main_blueprint
application.register_blueprint(main_blueprint)
return application
def init_app(app):
for key, value in app.config.items():
if key in os.environ:
app.config[key] = convert_to_boolean(os.environ[key])
def convert_to_boolean(value):
if isinstance(value, string_types):
if value.lower() in ['t', 'true', 'on', 'yes', '1']:
return True
elif value.lower() in ['f', 'false', 'off', 'no', '0']:
return False
return value

View File

@@ -8,11 +8,6 @@ def index():
return 'Hello from notifications-admin'
@main.route("/")
def idx():
return render_template('index.html')
@main.route("/govuk")
def govuk():
return render_template('govuk_template.html')

View File

@@ -1,10 +0,0 @@
<html>
<head>
{% assets "css_all" %}
<style type="text/css" src="{{ ASSET_URL }}"></style>
{% endassets %}
</head>
<body>
<h1>hi</h1>
</body>
</html>

20
config.py Normal file
View File

@@ -0,0 +1,20 @@
class Config(object):
DEBUG = False
ASSETS_DEBUG = False
cache = False
manifest = True
class Development(Config):
DEBUG = True
class Test(Config):
DEBUG = False
configs = {
'development': Development,
'TEST': Test
}