Ejemplo n.º 1
0
def configure_app(app):
    config = IndicoConfig(
        app.config['INDICO'])  # needed since we don't have an app ctx yet
    app.config['DEBUG'] = config.DEBUG
    app.config['SECRET_KEY'] = config.SECRET_KEY
    app.config['LOGGER_NAME'] = 'flask.app'
    app.config['LOGGER_HANDLER_POLICY'] = 'never'
    if not app.config['SECRET_KEY'] or len(app.config['SECRET_KEY']) < 16:
        raise ValueError(
            'SECRET_KEY must be set to a random secret of at least 16 characters. '
            'You can generate one using os.urandom(32) in Python shell.')
    if config.MAX_UPLOAD_FILES_TOTAL_SIZE > 0:
        app.config[
            'MAX_CONTENT_LENGTH'] = config.MAX_UPLOAD_FILES_TOTAL_SIZE * 1024 * 1024
    app.config['PROPAGATE_EXCEPTIONS'] = True
    app.config['TRAP_HTTP_EXCEPTIONS'] = False
    app.config['TRAP_BAD_REQUEST_ERRORS'] = config.DEBUG
    app.config['SESSION_COOKIE_NAME'] = 'indico_session'
    app.config['PERMANENT_SESSION_LIFETIME'] = config.SESSION_LIFETIME
    app.config['RATELIMIT_STORAGE_URL'] = config.REDIS_CACHE_URL or 'memory://'
    configure_cache(app, config)
    configure_multipass(app, config)
    app.config['PLUGINENGINE_NAMESPACE'] = 'indico.plugins'
    app.config['PLUGINENGINE_PLUGINS'] = config.PLUGINS
    base = url_parse(config.BASE_URL)
    app.config['PREFERRED_URL_SCHEME'] = base.scheme
    app.config['SERVER_NAME'] = base.netloc
    if base.path:
        app.config['APPLICATION_ROOT'] = base.path
    configure_xsendfile(app, config.STATIC_FILE_METHOD)
    if config.USE_PROXY:
        app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1, x_host=1)
    configure_webpack(app)
    configure_emails(app, config)
Ejemplo n.º 2
0
def configure_app(app, set_path=False):
    config = IndicoConfig(
        app.config['INDICO'])  # needed since we don't have an app ctx yet
    app.config['DEBUG'] = config.DEBUG
    app.config['SECRET_KEY'] = config.SECRET_KEY
    if not app.config['SECRET_KEY'] or len(app.config['SECRET_KEY']) < 16:
        raise ValueError(
            'SECRET_KEY must be set to a random secret of at least 16 characters. '
            'You can generate one using os.urandom(32) in Python shell.')
    if config.MAX_UPLOAD_FILES_TOTAL_SIZE > 0:
        app.config[
            'MAX_CONTENT_LENGTH'] = config.MAX_UPLOAD_FILES_TOTAL_SIZE * 1024 * 1024
    app.config['PROPAGATE_EXCEPTIONS'] = True
    app.config['SESSION_COOKIE_NAME'] = 'indico_session'
    app.config['PERMANENT_SESSION_LIFETIME'] = config.SESSION_LIFETIME
    app.config['INDICO_SESSION_PERMANENT'] = config.SESSION_LIFETIME > 0
    app.config['INDICO_HTDOCS'] = os.path.join(app.root_path, 'htdocs')
    configure_multipass(app, config)
    app.config['PLUGINENGINE_NAMESPACE'] = 'indico.plugins'
    app.config['PLUGINENGINE_PLUGINS'] = config.PLUGINS
    if set_path:
        base = url_parse(config.BASE_URL)
        app.config['PREFERRED_URL_SCHEME'] = base.scheme
        app.config['SERVER_NAME'] = base.netloc
        if base.path:
            app.config['APPLICATION_ROOT'] = base.path
    configure_xsendfile(app, config.STATIC_FILE_METHOD)
    if config.USE_PROXY:
        app.wsgi_app = ProxyFix(app.wsgi_app)
Ejemplo n.º 3
0
def setup_jinja(app):
    app.jinja_env.policies['ext.i18n.trimmed'] = True
    # Unicode hack
    app.jinja_env.add_extension(EnsureUnicodeExtension)
    app.add_template_filter(EnsureUnicodeExtension.ensure_unicode)
    # Useful (Python) builtins
    app.add_template_global(dict)
    # Global functions
    app.add_template_global(url_for)
    app.add_template_global(url_for_plugin)
    app.add_template_global(url_rule_to_js)
    app.add_template_global(IndicoConfig(exc=Exception), 'indico_config')
    app.add_template_global(call_template_hook, 'template_hook')
    app.add_template_global(is_single_line_field, '_is_single_line_field')
    app.add_template_global(render_field, '_render_field')
    app.add_template_global(iter_form_fields, '_iter_form_fields')
    app.add_template_global(format_currency)
    app.add_template_global(get_currency_name)
    app.add_template_global(url_for_index)
    app.add_template_global(url_for_login)
    app.add_template_global(url_for_logout)
    app.add_template_global(lambda: unicode(uuid.uuid4()), 'uuid')
    app.add_template_global(icon_from_mimetype)
    app.add_template_global(render_sidemenu)
    app.add_template_global(slugify)
    app.add_template_global(lambda: date_time_util.now_utc(False), 'now')
    app.add_template_global(render_session_bar)
    app.add_template_global(get_request_stats)
    # Global variables
    app.add_template_global(LocalProxy(get_current_locale), 'current_locale')
    app.add_template_global(
        LocalProxy(lambda: current_plugin.manifest
                   if current_plugin else None), 'plugin_webpack')
    # Useful constants
    app.add_template_global('^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$',
                            name='time_regex_hhmm')  # for input[type=time]
    # Filters (indico functions returning UTF8)
    app.add_template_filter(
        EnsureUnicodeExtension.wrap_func(date_time_util.format_date))
    app.add_template_filter(
        EnsureUnicodeExtension.wrap_func(date_time_util.format_time))
    app.add_template_filter(
        EnsureUnicodeExtension.wrap_func(date_time_util.format_datetime))
    app.add_template_filter(
        EnsureUnicodeExtension.wrap_func(date_time_util.format_human_date))
    app.add_template_filter(
        EnsureUnicodeExtension.wrap_func(date_time_util.format_timedelta))
    app.add_template_filter(
        EnsureUnicodeExtension.wrap_func(date_time_util.format_number))
    # Filters (new ones returning unicode)
    app.add_template_filter(date_time_util.format_human_timedelta)
    app.add_template_filter(date_time_util.format_pretty_date)
    app.add_template_filter(date_time_util.format_pretty_datetime)
    app.add_template_filter(lambda d: Markup(html_params(**d)), 'html_params')
    app.add_template_filter(underline)
    app.add_template_filter(markdown)
    app.add_template_filter(dedent)
    app.add_template_filter(html_to_plaintext)
    app.add_template_filter(natsort)
    app.add_template_filter(groupby)
    app.add_template_filter(any)
    app.add_template_filter(alpha_enum)
    app.add_template_filter(crc32)
    app.add_template_filter(bool)
    app.add_template_filter(lambda s: Markup(sanitize_html(s or '')),
                            'sanitize_html')
    app.add_template_filter(RichMarkup, 'rich_markup')
    # Tests
    app.add_template_test(
        instanceof)  # only use this test if you really have to!
    app.add_template_test(
        subclassof)  # only use this test if you really have to!
    # i18n
    app.jinja_env.add_extension('jinja2.ext.i18n')
    app.jinja_env.install_gettext_callables(gettext_context, ngettext_context,
                                            True)