def set_blueprints(app, blueprints): """ Registers blueprints with the app. """ # Register blueprints. for blueprint in blueprints: url_prefix = None if len(blueprint) == 2: blueprint, url_prefix = blueprint blueprint_object = import_string('%s:BLUEPRINT' % blueprint, silent=True) blueprint_name, blueprint_import_name = blueprint.split('.')[-1], blueprint if not blueprint_object: options = dict(static_folder='static', template_folder='templates') blueprint_object = Blueprint(blueprint_name, blueprint_import_name, **options) blueprint_routes = import_string('%s.urls:routes' % blueprint_import_name, silent=True) if blueprint_routes: urls.set_urls(blueprint_object, blueprint_routes) # Other initializations. for fn, values in [(set_before_handlers, import_string('%s:BEFORE_REQUESTS' % blueprint, silent=True)), (set_before_app_handlers, import_string('%s:BEFORE_APP_REQUESTS' % blueprint, silent=True)), (set_after_handlers, import_string('%s:AFTER_REQUESTS' % blueprint, silent=True)), (set_after_app_handlers, import_string('%s:AFTER_APP_REQUESTS' % blueprint, silent=True)), (set_context_processors, import_string('%s:CONTEXT_PROCESSORS' % blueprint, silent=True)), (set_app_context_processors, import_string('%s:APP_CONTEXT_PROCESSORS' % blueprint, silent=True)), (set_error_handlers, import_string('%s:ERROR_HANDLERS' % blueprint, silent=True)), (set_app_error_handlers, import_string('%s:APP_ERROR_HANDLERS' % blueprint, silent=True))]: if values: fn(blueprint_object, values) # Can be mounted at specific prefix. if url_prefix: app.register_blueprint(blueprint_object, url_prefix=url_prefix) else: app.register_blueprint(blueprint_object)
def init(basic_app=False): """ Sets up flask application object `app` and returns it. If `basic_app` is true, it creates the app and the db object. Mainly useful for using models outside of the app. """ # Instantiate main app, load configs, register modules, set # url patterns and return the `app` object. app = Flask(__name__) app.config.from_object(settings) if not basic_app: # Other initializations. for fn, values in [ (set_middlewares, getattr(settings, "MIDDLEWARES", None)), (set_blueprints, getattr(settings, "BLUEPRINTS", None)), (set_before_handlers, getattr(settings, "BEFORE_REQUESTS", None)), (set_after_handlers, getattr(settings, "AFTER_REQUESTS", None)), (set_log_handlers, getattr(settings, "LOG_HANDLERS", None)), (set_context_processors, getattr(settings, "CONTEXT_PROCESSORS", None)), (set_error_handlers, getattr(settings, "ERROR_HANDLERS", None)), (set_template_filters, getattr(settings, "TEMPLATE_FILTERS", None)), ]: if values: fn(app, values) # URL rules. urls.set_urls(app) #: Wrap the `app` with `Babel` for i18n. Babel(app) Environment(app) config.cache = Cache(app) # Init SQLAlchemy wrapper. config.db = SQLAlchemy(app) return app
def init(): """ Sets up flask application object `app` and returns it. """ # Instantiate main app, load configs, register modules, set # url patterns and return the `app` object. app = Flask(__name__) app.config.from_object(settings) config.app = app # Init SQLAlchemy wrapper. config.db = SQLAlchemy(app) if app.debug: DebugToolbarExtension(app) #: Wrap the `app` with `Babel` for i18n. Babel(app) Environment(app) config.cache = Cache(app) app.jinja_env.add_extension(SlimishExtension) app.jinja_env.slim_debug = app.debug config.bcrypt = Bcrypt(app) # Other initializations. for fn, values in [(set_middlewares, getattr(settings, 'MIDDLEWARES', None)), (set_context_processors, getattr(settings, 'CONTEXT_PROCESSORS', None)), (set_template_filters, getattr(settings, 'TEMPLATE_FILTERS', None)), (set_before_handlers, getattr(settings, 'BEFORE_REQUESTS', None)), (set_after_handlers, getattr(settings, 'AFTER_REQUESTS', None)), (set_log_handlers, getattr(settings, 'LOG_HANDLERS', None)), (set_error_handlers, getattr(settings, 'ERROR_HANDLERS', None)), (set_blueprints, getattr(settings, 'BLUEPRINTS', None))]: if values: fn(app, values) # URL rules. urls.set_urls(app) return app
def init(): """ Sets up flask application object `app` and returns it. """ # Instantiate main app, load configs, register modules, set # url patterns and return the `app` object. app = Flask(__name__) app.config.from_object(settings) config.app = app # Init SQLAlchemy wrapper. config.db = SQLAlchemy(app) if app.debug: DebugToolbarExtension(app) #: Wrap the `app` with `Babel` for i18n. Babel(app) config.cache = Cache(app) app.jinja_env.add_extension(SlimishExtension) app.jinja_env.slim_debug = app.debug config.bcrypt = Bcrypt(app) # Other initializations. for fn, values in [ (set_middlewares, getattr(settings, 'MIDDLEWARES', None)), (set_context_processors, getattr(settings, 'CONTEXT_PROCESSORS', None)), (set_template_filters, getattr(settings, 'TEMPLATE_FILTERS', None)), (set_before_handlers, getattr(settings, 'BEFORE_REQUESTS', None)), (set_after_handlers, getattr(settings, 'AFTER_REQUESTS', None)), (set_log_handlers, getattr(settings, 'LOG_HANDLERS', None)), (set_error_handlers, getattr(settings, 'ERROR_HANDLERS', None)), (set_blueprints, getattr(settings, 'BLUEPRINTS', None)) ]: if values: fn(app, values) # Register all js and css files. assets = Environment(app) register_assets(app, assets) # URL rules. urls.set_urls(app) return app
def init(): """ Sets up flask application object `app` and returns it. """ # Instantiate main app, load configs, register modules, set # url patterns and return the `app` object. app = Flask(__name__) app.config.from_object(settings) config.app = app # Init SQLAlchemy wrapper. config.db = SQLAlchemy(app) if app.debug: DebugToolbarExtension(app) #: Wrap the `app` with `Babel` for i18n. Babel(app) config.cache = Cache(app) config.bcrypt = Bcrypt(app) # Other initializations. for fn, values in [(set_middlewares, getattr(settings, 'MIDDLEWARES', None)), (set_context_processors, getattr(settings, 'CONTEXT_PROCESSORS', None)), (set_template_filters, getattr(settings, 'TEMPLATE_FILTERS', None)), (set_before_handlers, getattr(settings, 'BEFORE_REQUESTS', None)), (set_after_handlers, getattr(settings, 'AFTER_REQUESTS', None)), (set_log_handlers, getattr(settings, 'LOG_HANDLERS', None)), (set_error_handlers, getattr(settings, 'ERROR_HANDLERS', None)), (set_blueprints, getattr(settings, 'BLUEPRINTS', None))]: if values: fn(app, values) # Register all js and css files. assets = Environment(app) for name, bundle in webassets.loaders.YAMLLoader('assets.yml').load_bundles().items(): assets.register(name, bundle) # URL rules. urls.set_urls(app) # Set flask-cli commands import commands return app
def set_blueprints(app, blueprints): """ Registers blueprints with the app. """ # Register blueprints. for blueprint in blueprints: url_prefix = None if len(blueprint) == 2: blueprint, url_prefix = blueprint blueprint_name, blueprint_import_name = blueprint.split(".")[-1], blueprint options = dict(static_folder="static", template_folder="templates") if not url_prefix: options["static_url_path"] = "/%s/static" % blueprint_name blueprint_object = Blueprint(blueprint_name, blueprint_import_name, **options) blueprint_routes = import_string("%s.urls:routes" % blueprint_import_name, silent=True) if blueprint_routes: urls.set_urls(blueprint_object, blueprint_routes) # Can be mounted at specific prefix. if url_prefix: app.register_blueprint(blueprint_object, url_prefix=url_prefix) else: app.register_blueprint(blueprint_object)