Beispiel #1
0
def create_app(config):
    app = Flask(__name__)

    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///'
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    # app.config['SQLALCHEMY_ECHO'] = True
    # app.config['DEBUG'] = True
    if config == 'Test':
        app.config['TEST'] = True
    app.secret_key = 'only-testing'

    from webgrid_ta.model import db
    db.init_app(app)
    webgrid.init_db(db)
    default_registry.locales = app.config.get('DEFAULT_LOCALE', 'en')
    configure_jinja_environment(app.jinja_env, translation_manager)
    Bootstrap(app)
    webgrid.init_app(app)

    from .views import main
    app.register_blueprint(main)

    @app.before_request
    def set_language():
        default_registry.locales = str(flask.request.accept_languages)
        configure_jinja_environment(app.jinja_env, translation_manager)

    return app
Beispiel #2
0
 def init_app(self, app):
     bp = Blueprint(
         'webgrid',
         __name__,
         static_folder='static',
         static_url_path=app.static_url_path + '/webgrid'
     )
     app.register_blueprint(bp)
     configure_jinja_environment(app.jinja_env, translation_manager)
Beispiel #3
0
    def test_configure_jinja_environment_can_install(self):
        jinja_environment = mock.Mock()
        manager = mock.Mock()

        jinja.configure_jinja_environment(jinja_environment, manager)

        jinja_environment.add_extension.assert_called_with('jinja2.ext.i18n')
        jinja_environment.install_gettext_translations.assert_called_with(
            manager.translations)
        jinja_environment.globals.update.assert_called_with(
            find_mo_filename=manager._mo_finder)
Beispiel #4
0
    def test_configure_jinja_environment_cannot_install(self, caplog):
        jinja_environment = mock.Mock()
        delattr(jinja_environment, 'install_gettext_translations')
        manager = mock.Mock()

        jinja.configure_jinja_environment(jinja_environment, manager)

        jinja_environment.add_extension.assert_called_with('jinja2.ext.i18n')
        jinja_environment.globals.update.assert_called_with(
            find_mo_filename=manager._mo_finder)

        assert 1 == len(caplog.records)
        assert 'install_gettext_translations' in caplog.records[0].message
Beispiel #5
0
    def __init__(self, grid):
        self.grid = grid
        self.manager = grid.manager
        if self.manager:
            self.jinja_env = self.manager.jinja_environment
        else:
            # if the grid is unmanaged for any reason (e.g. just not in a request/response
            # cycle and used only for render), fall back to a default jinja environment
            self.jinja_env = jinja.Environment(
                loader=jinja.PackageLoader('webgrid', 'templates'),
                autoescape=True
            )
        self.jinja_env.filters['wg_safe'] = jinja.filters.do_mark_safe

        configure_jinja_environment(self.jinja_env, translation_manager)
Beispiel #6
0
    def init(self):
        self.manager = self.grid.manager
        if self.manager:
            self.jinja_env = self.manager.jinja_environment
        else:
            # if the grid is unmanaged for any reason (e.g. just not in a request/response
            # cycle and used only for render), fall back to a default jinja environment
            self.jinja_env = jinja.Environment(loader=jinja.PackageLoader(
                'webgrid', 'templates'),
                                               finalize=lambda x: x
                                               if x is not None else '',
                                               autoescape=True)
        self.jinja_env.filters['wg_safe'] = jinja.filters.do_mark_safe
        self.jinja_env.filters['wg_attributes'] = render_html_attributes
        self.jinja_env.filters['wg_gettext'] = _

        configure_jinja_environment(self.jinja_env, translation_manager)
Beispiel #7
0
 def set_language():
     default_registry.locales = str(flask.request.accept_languages)
     configure_jinja_environment(app.jinja_env, translation_manager)