Exemple #1
0
def create_app(option):
    app = Flask(__name__)
    config = config_factory.get(option)
    app.config.from_object(config)
    from common import create_jinja_filters, random_pwd

    create_jinja_filters(app)
    from webapp.api import api
    from webapp.client import client
    from webapp.media import media

    app.register_blueprint(client)
    app.register_blueprint(api, url_prefix=Constants.API_V1_URL_PREFIX)
    app.register_blueprint(media, url_prefix=Constants.MEDIA_URL_PREFIX)

    csrf.init_app(app)
    compress.init_app(app)
    gravatar.init_app(app)
    db.init_app(app)
    migrate.init_app(app, db)
    admin.init_app(app)
    mail.init_app(app)
    from models import User, Role
    from webapp.forms import ExtendedRegisterForm

    user_datastore = SQLAlchemyUserDatastore(db, User, Role)
    security.init_app(app, user_datastore, register_form=ExtendedRegisterForm)
    with app.app_context():
        api_manager.init_app(app, flask_sqlalchemy_db=db)

        @app.before_request
        def before_request():
            g.constants = Constants
            g.config = app.config
            g.mode = app.config.get('MODE')

        @app.after_request
        def redirect_if_next(response_class):
            payload = request.args if request.method == 'GET' else request.form
            if 'api_next' in payload:
                if not response_class.status_code == 200:
                    flash(response_class.data)
                    return redirect(request.referrer)
                return redirect(payload.get('api_next'))
            return response_class

    patch_request_class(app, Constants.MAX_FILE_SIZE)

    from webapp.common import make_json_error

    for code in default_exceptions.iterkeys():
        app.error_handler_spec[None][code] = make_json_error

    configure_uploads(app, (user_images, ))

    return app
Exemple #2
0
def create_app(option):
    app = Flask(__name__)
    config = config_factory.get(option)
    app.config.from_object(config)

    # Initialize extensions
    from models import User, Role
    from webapp.forms import ExtendedRegisterForm

    user_datastore = SQLAlchemyUserDatastore(db, User, Role)
    security.init_app(app, user_datastore, register_form=ExtendedRegisterForm)
    csrf.init_app(app)
    db.init_app(app)
    admin.init_app(app)
    babel.init_app(app)
    migrate.init_app(app, db)
    socketio.init_app(app)
    gravatar.init_app(app)
    gmap.init_app(app)

    configure_uploads(app, (user_images,))
    patch_request_class(app, Constants.MAX_FILE_SIZE)

    # Set up API via Flask-Restless
    import webapp.api.views

    with app.app_context():
        api_manager.init_app(app, flask_sqlalchemy_db=db)

        @app.before_request
        def before_request():
            request.x_remote_addr = request.remote_addr
            if 'X-Real-Ip' in request.headers:
                request.x_remote_addr = request.headers.get('X-Real-Ip')
            g.constants = Constants
            g.config = app.config
            g.mode = app.config.get('MODE')

    # Set up client blueprint
    from webapp.client import client

    app.register_blueprint(client)

    from webapp.media import media

    app.register_blueprint(media, url_prefix=Constants.MEDIA_URL_PREFIX)

    from util.common import create_jinja_helpers

    create_jinja_helpers(app)

    from flask.ext.babel import refresh;

    refresh()
    return app
def configure_app(app, config):
    if not config:
        config = assets.AppModes.DEVELOPMENT
    config = config_factory.get(config)
    app.config.from_object(config)
    # override config with env file cfg
    prod_config = os.environ.get('OPINEW_CONFIG_FILE')
    if prod_config and os.path.exists(prod_config):
        app.config.from_envvar('OPINEW_CONFIG_FILE')  # pragma: no cover
    # limit the file size that can be uploaded
    patch_request_class(app, assets.Constants.MAX_FILE_SIZE)
    # infinite jinja template cache
    app.jinja_env.cache = {}
Exemple #4
0
def create_app():
    app = Flask(__name__)
    app_config = config_factory.get(
        os.environ.get('FLASK_ENV') or 'development')
    app.config.from_object(app_config)

    # Database initialization
    db.init_app(app)
    migrate.init_app(app, db)

    # Views
    app.register_blueprint(graph_bp)
    
    return app
Exemple #5
0
def create_app():
    app = Flask(__name__)
    config_object = config_factory.get(os.environ.get('FLASK_ENV') or 'default')
    app.config.from_object(config_object)

    db.init_app(app)
    migrate.init_app(app, db)

    login_manager.init_app(app)
    login_manager.login_view = 'auth.login'

    app.register_blueprint(auth_bp)
    app.register_blueprint(route_bp)


    return app
Exemple #6
0
def create_app(option):
    app = Flask(__name__)
    config = config_factory.get(option)
    app.config.from_object(config)

    # Initialize extensions
    from models import User, Role

    user_datastore = SQLAlchemyUserDatastore(db, User, Role)
    security.init_app(app, user_datastore)
    admin.init_app(app)
    csrf.init_app(app)
    db.init_app(app)
    migrate.init_app(app, db)
    socketio.init_app(app)
    gravatar.init_app(app)
    babel.init_app(app)
    gmap.init_app(app)

    patch_request_class(app, Constants.MAX_FILE_SIZE)

    with app.app_context():
        @app.before_request
        def before_request():
            request.x_remote_addr = request.remote_addr
            if 'X-Real-Ip' in request.headers:
                request.x_remote_addr = request.headers.get('X-Real-Ip')
            g.constants = Constants
            g.config = app.config
            g.mode = app.config.get('MODE')

        @app.after_request
        def after_request(response):
            signed_in_value = '1' if current_user and current_user.is_authenticated else '0'
            response.set_cookie('signed_in', value=signed_in_value)
            return response

    # Set up client blueprint
    from webapp.client import client

    app.register_blueprint(client)

    from util.common import create_jinja_helpers
    create_jinja_helpers(app)

    return app
Exemple #7
0
def create_app(option):
    app = FlaskOpinewExt(__name__)
    config = config_factory.get(option)
    app.config.from_object(config)

    from common import create_jinja_filters, random_pwd, verify_initialization

    create_jinja_filters(app)
    from webapp.client import client
    from webapp.media import media

    app.register_blueprint(client)
    app.register_blueprint(media, url_prefix=Constants.MEDIA_URL_PREFIX)

    compress.init_app(app)
    gravatar.init_app(app)
    resize.init_app(app)
    db.init_app(app)
    admin.init_app(app)
    mail.init_app(app)
    migrate.init_app(app, db)
    babel.init_app(app)
    from models import User, Role
    from webapp.forms import ExtendedRegisterForm

    assets.init_app(app)
    assets.register('js_all', js_assets)
    assets.register('css_all', css_assets)

    user_datastore = SQLAlchemyUserDatastore(db, User, Role)
    security.init_app(app,
                      user_datastore,
                      confirm_register_form=ExtendedRegisterForm)
    with app.app_context():
        from providers import database, payment

        if not app.testing:
            verify_initialization()

        if app.testing:
            from async import tasks
        api_manager.init_app(app, flask_sqlalchemy_db=db)

        @app.before_request
        def before_request():
            # hack to allow browsers who don't set 3rd party cookies
            x_session = request.headers.get('X-Session')
            if x_session:
                rc = dict(request.cookies)
                rc['session'] = x_session
                request.cookies = ImmutableTypeConversionDict(rc)
                # refresh session
                refreshed_csrf_token = app.session_interface.open_session(
                    app, request).get('csrf_token')
                session['csrf_token'] = refreshed_csrf_token
            user_agent = parse(request.user_agent.string)
            g.mobile = False
            if user_agent.is_mobile or user_agent.is_tablet:
                g.mobile = True
            g.constants = Constants
            g.config = app.config
            g.mode = app.config.get('MODE')
            g.response_context = []
            g.s = strings
            g.payment = payment.StripeAPI()
            g.db = database.OpinewSQLAlchemyFacade()

        @app.after_request
        def redirect_if_next(response_class):
            if request.endpoint == 'static':
                response_class.headers['Access-Control-Allow-Origin'] = '*'
            payload = request.args if request.method == 'GET' else request.form
            if 'api_next' in payload:
                if not response_class.status_code == 200:
                    flash(response_class.data)
                    return redirect(request.referrer)
                return redirect(payload.get('api_next'))
            return response_class

    # register here CSRF so that the before_request is executed after the hack above
    csrf.init_app(app)
    patch_request_class(app, Constants.MAX_FILE_SIZE)

    from webapp.common import make_json_error

    for code in default_exceptions.iterkeys():
        app.error_handler_spec[None][code] = make_json_error

    configure_uploads(app, (
        user_images,
        review_images,
        shop_images,
    ))
    admins = [email for name, email in config.ADMINS]

    if not (app.debug or app.testing):
        mail_handler = SMTPHandler(
            app.config.get('MAIL_SERVER'),
            '*****@*****.**',
            admins,
            'Your Application Failed',
            credentials=(app.config.get('MAIL_USERNAME'),
                         app.config.get('MAIL_PASSWORD')),
            secure=())
        mail_handler.setLevel(logging.ERROR)
        mail_handler.setFormatter(
            Formatter('''
Time        : %(asctime)s
Location    : %(pathname)s:%(lineno)d
Module      : %(module)s
Function    : %(funcName)s

%(message)s'''))
        app.logger.addHandler(mail_handler)

    return app
Exemple #8
0
def create_app(option):
    app = FlaskOpinewExt(__name__)
    config = config_factory.get(option)
    app.config.from_object(config)

    from common import create_jinja_filters, random_pwd, verify_initialization

    create_jinja_filters(app)
    from webapp.client import client
    from webapp.media import media

    app.register_blueprint(client)
    app.register_blueprint(media, url_prefix=Constants.MEDIA_URL_PREFIX)

    compress.init_app(app)
    gravatar.init_app(app)
    resize.init_app(app)
    db.init_app(app)
    admin.init_app(app)
    mail.init_app(app)
    migrate.init_app(app, db)
    babel.init_app(app)
    from models import User, Role
    from webapp.forms import ExtendedRegisterForm

    assets.init_app(app)
    assets.register('js_all', js_assets)
    assets.register('css_all', css_assets)

    user_datastore = SQLAlchemyUserDatastore(db, User, Role)
    security.init_app(app, user_datastore, confirm_register_form=ExtendedRegisterForm)
    with app.app_context():
        from providers import database, payment

        if not app.testing:
            verify_initialization()

        if app.testing:
            from async import tasks
        api_manager.init_app(app, flask_sqlalchemy_db=db)

        @app.before_request
        def before_request():
            # hack to allow browsers who don't set 3rd party cookies
            x_session = request.headers.get('X-Session')
            if x_session:
                rc = dict(request.cookies)
                rc['session'] = x_session
                request.cookies = ImmutableTypeConversionDict(rc)
                # refresh session
                refreshed_csrf_token = app.session_interface.open_session(app, request).get('csrf_token')
                session['csrf_token'] = refreshed_csrf_token
            user_agent = parse(request.user_agent.string)
            g.mobile = False
            if user_agent.is_mobile or user_agent.is_tablet:
                g.mobile = True
            g.constants = Constants
            g.config = app.config
            g.mode = app.config.get('MODE')
            g.response_context = []
            g.s = strings
            g.payment = payment.StripeAPI()
            g.db = database.OpinewSQLAlchemyFacade()

        @app.after_request
        def redirect_if_next(response_class):
            if request.endpoint == 'static':
                response_class.headers['Access-Control-Allow-Origin'] = '*'
            payload = request.args if request.method == 'GET' else request.form
            if 'api_next' in payload:
                if not response_class.status_code == 200:
                    flash(response_class.data)
                    return redirect(request.referrer)
                return redirect(payload.get('api_next'))
            return response_class

    # register here CSRF so that the before_request is executed after the hack above
    csrf.init_app(app)
    patch_request_class(app, Constants.MAX_FILE_SIZE)

    from webapp.common import make_json_error

    for code in default_exceptions.iterkeys():
        app.error_handler_spec[None][code] = make_json_error

    configure_uploads(app, (user_images, review_images, shop_images, ))
    admins = [email for name, email in config.ADMINS]

    if not (app.debug or app.testing):
        mail_handler = SMTPHandler(app.config.get('MAIL_SERVER'),
                                   '*****@*****.**',
                                   admins,
                                   'Your Application Failed',
                                   credentials=(app.config.get('MAIL_USERNAME'), app.config.get('MAIL_PASSWORD')),
                                   secure=())
        mail_handler.setLevel(logging.ERROR)
        mail_handler.setFormatter(Formatter('''
Time        : %(asctime)s
Location    : %(pathname)s:%(lineno)d
Module      : %(module)s
Function    : %(funcName)s

%(message)s'''))
        app.logger.addHandler(mail_handler)

    return app