Example #1
0
def create_app(env_name='DEVELOPMENT'):
    # Flask application
    app = Flask(__name__)
    app.config.from_object(__name__)
    app.config.update(PROJECT_ROOT=PROJECT_ROOT)
    app.config.update(PROJECT_DIR=PROJECT_DIR)

    # Flask environment
    env = Environments(app, default_env=from_env_name(env_name))
    env.from_object('{0}.settings'.format(__name__))
    app.extensions['env'] = env

    # Create database connection object
    db.app = app  # RuntimeError: application not registered on db
    # instance and no application bound to current context
    db.init_app(app)
    ma.init_app(app)

    app.extensions['ma'] = ma
    app.extensions['db'] = db

    hasher = Hashids(salt=str(app.config['SECRET_KEY']), min_length=10)
    app.extensions['hasher'] = hasher

    # Flask security
    user_datastore = SQLAlchemyUserDatastore(db, User, Role)
    security = Security(app, user_datastore)
    app.extensions['user_datastore'] = user_datastore
    app.extensions['security'] = security

    from .api.application import v1 as application_v1

    application_v1.setup(app)

    # Templates
    @app.template_filter('datetimeformat')
    def datetimeformat(value, format='%H:%M / %d-%m-%Y'):
        return value.strftime(format)

    # Context
    @app.context_processor
    def inject_application_config():
        return {
            'app_config': application_config()
        }

    @app.errorhandler(TypeError)
    def handle_type_error(error):
        response = Response(status=400)
        return response

    @app.errorhandler(ApiException)
    def handle_type_error(error):
        return error.to_response()

    return app
Example #2
0
from flask import abort
from flask.ext.environments import Environments
from tools.sessions import CachedFlask, setup_sessioned_app
from tools.login_manager import TokenSessionLoginManager, setup_login_manager

import logging

logger = logging.getLogger(__name__)

has_loaded = False
if not has_loaded:
    root_module = __name__.split('.')[0]

    # Setup App
    app = CachedFlask(root_module)
    env = Environments(app, default_env='DEVELOPMENT')
    env.from_object('config')

    print "Running in environment: %s" % env.env

    session_key = app.config.get('SESSION_SECRET_KEY', '')
    if not env.env.lower().startswith('development'):
        # Setup Sessions and Caching
        session_cache_config = app.config.get('SESSION_CACHE_CONFIG', None)
        app_cache_config = app.config.get('APP_CACHE_CONFIG', None)

        setup_sessioned_app(app=app,
                            session_key=session_key,
                            session_cache_config=session_cache_config,
                            app_cache_config=app_cache_config)