Exemple #1
0
def setup_jinja(app):
    config = Config.getInstance()
    # Unicode hack
    app.jinja_env.add_extension(EnsureUnicodeExtension)
    app.add_template_filter(EnsureUnicodeExtension.ensure_unicode)
    # Global functions
    app.add_template_global(url_for)
    app.add_template_global(url_rule_to_js)
    app.add_template_global(IndicoConfigWrapper(config), 'indico_config')
    app.add_template_global(config.getSystemIconURL, 'system_icon')
    # 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))
    # Filters (new ones returning unicode)
    app.add_template_filter(underline)
    # i18n
    app.jinja_env.add_extension('jinja2.ext.i18n')
    app.jinja_env.install_gettext_callables(gettext, ngettext, True)
    # webassets
    app.jinja_env.add_extension('webassets.ext.jinja2.AssetsExtension')
    app.jinja_env.assets_environment = core_env
Exemple #2
0
def setup_jinja(app):
    config = Config.getInstance()
    # Unicode hack
    app.jinja_env.add_extension(EnsureUnicodeExtension)
    app.add_template_filter(EnsureUnicodeExtension.ensure_unicode)
    # 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(IndicoConfigWrapper(config), 'indico_config')
    app.add_template_global(config.getSystemIconURL, 'system_icon')
    app.add_template_global(include_css_assets)
    app.add_template_global(include_js_assets)
    app.add_template_global(include_plugin_css_assets)
    app.add_template_global(include_plugin_js_assets)
    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(format_currency)
    app.add_template_global(get_currency_name)
    app.add_template_global(url_for_login)
    app.add_template_global(url_for_logout)
    # 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))
    # Filters (new ones returning unicode)
    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(natsort)
    # Tests
    app.add_template_test(
        instanceof)  # only use this test if you really have to!
    app.add_template_test(equalto)
    # i18n
    app.jinja_env.add_extension('jinja2.ext.i18n')
    app.jinja_env.install_gettext_callables(gettext_context, ngettext_context,
                                            True)
    app.add_template_global(
        lambda: HelperMaKaCInfo.getMaKaCInfoInstance().getLang(),
        'get_default_language')
    # webassets
    app.jinja_env.add_extension('webassets.ext.jinja2.AssetsExtension')
    app.jinja_env.assets_environment = core_env
Exemple #3
0
    def get_context(self):
        if self._context is None:
            self._context = context = {}
            self._info = []

            add_to_context = partial(_add_to_context, context, self._info)
            add_to_context_multi = partial(_add_to_context_multi, context, self._info)
            add_to_context_smart = partial(_add_to_context_smart, context, self._info)
            # Common stdlib modules
            self._info.append(cformat('*** %{magenta!}stdlib%{reset} ***'))
            DATETIME_ATTRS = ('date', 'time', 'datetime', 'timedelta')
            ORM_ATTRS = ('joinedload', 'defaultload', 'contains_eager', 'lazyload', 'noload', 'subqueryload', 'undefer')
            add_to_context_multi([getattr(datetime, attr) for attr in DATETIME_ATTRS] +
                                 [getattr(sqlalchemy.orm, attr) for attr in ORM_ATTRS] +
                                 [itertools, re, sys, os],
                                 color='yellow')
            # Legacy Indico
            self._info.append(cformat('*** %{magenta!}Legacy%{reset} ***'))
            add_to_context_multi([Conference, ConferenceHolder, CategoryManager, Catalog, IndexesHolder], color='green')
            add_to_context(LocalProxy(HelperMaKaCInfo.getMaKaCInfoInstance), 'minfo', color='green')
            # Models
            self._info.append(cformat('*** %{magenta!}Models%{reset} ***'))
            models = [cls for name, cls in sorted(db.Model._decl_class_registry.items(), key=itemgetter(0))
                      if hasattr(cls, '__table__')]
            add_to_context_smart(models)
            # Tasks
            self._info.append(cformat('*** %{magenta!}Tasks%{reset} ***'))
            tasks = [task for task in sorted(celery.tasks.values()) if not task.name.startswith('celery.')]
            add_to_context_smart(tasks, get_name=lambda x: x.name.replace('.', '_'), color='blue!')
            # Plugins
            self._info.append(cformat('*** %{magenta!}Plugins%{reset} ***'))
            plugins = [type(plugin) for plugin in sorted(plugin_engine.get_active_plugins().values(),
                                                         key=attrgetter('name'))]
            add_to_context_multi(plugins, color='yellow!')
            # Utils
            self._info.append(cformat('*** %{magenta!}Misc%{reset} ***'))
            add_to_context(celery, 'celery', doc='celery app', color='blue!')
            add_to_context(DBMgr.getInstance(), 'dbi', doc='zodb db interface', color='cyan!')
            add_to_context(db, 'db', doc='sqlalchemy db interface', color='cyan!')
            add_to_context(transaction, doc='transaction module', color='cyan!')
            add_to_context(now_utc, 'now_utc', doc='get current utc time', color='cyan!')
            add_to_context(IndicoConfigWrapper(Config.getInstance()), 'config', doc='indico config')
            add_to_context(current_app, 'app', doc='flask app')
            add_to_context(lambda *a, **kw: server_to_utc(datetime.datetime(*a, **kw)), 'dt',
                           doc='like datetime() but converted from localtime to utc')
            add_to_context(lambda x: ConferenceHolder().getById(x, True), 'E', doc='get event by id (Conference)')
            add_to_context(Event.get, 'EE', doc='get event by id (Event)')
            # Stuff from plugins
            signals.plugin.shell_context.send(add_to_context=add_to_context, add_to_context_multi=add_to_context_multi)

        return self._context
Exemple #4
0
    def get_context(self):
        if self._context is None:
            self._context = context = {}

            for attr in ('date', 'time', 'datetime', 'timedelta'):
                _add_to_context(context,
                                getattr(datetime, attr),
                                doc='stdlib datetime.{}'.format(attr),
                                color='yellow')

            _add_to_context(context, Conference)
            _add_to_context(context, ConferenceHolder)
            _add_to_context(context, CategoryManager)
            _add_to_context(context, AvatarHolder)
            _add_to_context(context, GroupHolder)
            _add_to_context(context, PluginsHolder)
            _add_to_context(context, Catalog)
            _add_to_context(context, IndexesHolder)
            _add_to_context(context, IndicoConfigWrapper(Config.getInstance()),
                            'config')
            _add_to_context(context,
                            LocalProxy(HelperMaKaCInfo.getMaKaCInfoInstance),
                            'minfo')
            _add_to_context(context, current_app, 'app')

            for name, cls in sorted(db.Model._decl_class_registry.iteritems(),
                                    key=itemgetter(0)):
                if hasattr(cls, '__table__'):
                    _add_to_context(context, cls, color='cyan')

            _add_to_context(context,
                            DBMgr.getInstance(),
                            'dbi',
                            doc='zodb db interface',
                            color='cyan!')
            _add_to_context(context,
                            db,
                            'db',
                            doc='sqlalchemy db interface',
                            color='cyan!')
            _add_to_context(context,
                            transaction,
                            doc='transaction module',
                            color='cyan!')

        return self._context
Exemple #5
0
def _make_shell_context():
    context = {}
    info = [cformat('%{white!}Available objects')]
    add_to_context = partial(_add_to_context, context, info)
    add_to_context_multi = partial(_add_to_context_multi, context, info)
    add_to_context_smart = partial(_add_to_context_smart, context, info)
    # Common stdlib modules
    info.append(cformat('*** %{magenta!}stdlib%{reset} ***'))
    DATETIME_ATTRS = ('date', 'time', 'datetime', 'timedelta')
    ORM_ATTRS = ('joinedload', 'defaultload', 'contains_eager', 'lazyload', 'noload', 'subqueryload', 'undefer',
                 'undefer_group', 'load_only')
    add_to_context_multi([getattr(datetime, attr) for attr in DATETIME_ATTRS] +
                         [getattr(sqlalchemy.orm, attr) for attr in ORM_ATTRS] +
                         [itertools, re, sys, os],
                         color='yellow')
    # Models
    info.append(cformat('*** %{magenta!}Models%{reset} ***'))
    models = [cls for name, cls in sorted(db.Model._decl_class_registry.items(), key=itemgetter(0))
              if hasattr(cls, '__table__')]
    add_to_context_smart(models)
    # Tasks
    info.append(cformat('*** %{magenta!}Tasks%{reset} ***'))
    tasks = [task for task in sorted(celery.tasks.values()) if not task.name.startswith('celery.')]
    add_to_context_smart(tasks, get_name=lambda x: x.name.replace('.', '_'), color='blue!')
    # Plugins
    info.append(cformat('*** %{magenta!}Plugins%{reset} ***'))
    plugins = [type(plugin) for plugin in sorted(plugin_engine.get_active_plugins().values(),
                                                 key=attrgetter('name'))]
    add_to_context_multi(plugins, color='yellow!')
    # Utils
    info.append(cformat('*** %{magenta!}Misc%{reset} ***'))
    add_to_context(celery, 'celery', doc='celery app', color='blue!')
    add_to_context(db, 'db', doc='sqlalchemy db interface', color='cyan!')
    add_to_context(now_utc, 'now_utc', doc='get current utc time', color='cyan!')
    add_to_context(IndicoConfigWrapper(Config.getInstance()), 'config', doc='indico config')
    add_to_context(current_app, 'app', doc='flask app')
    add_to_context(lambda *a, **kw: server_to_utc(datetime.datetime(*a, **kw)), 'dt',
                   doc='like datetime() but converted from localtime to utc')
    add_to_context(Event.get, 'EE', doc='get event by id')
    # Stuff from plugins
    signals.plugin.shell_context.send(add_to_context=add_to_context, add_to_context_multi=add_to_context_multi)
    return context, info
Exemple #6
0
    def get_context(self):
        if self._context is None:
            self._context = context = {}
            self._info = []

            add_to_context = partial(_add_to_context, context, self._info)

            for attr in ('date', 'time', 'datetime', 'timedelta'):
                add_to_context(getattr(datetime, attr),
                               doc='stdlib datetime.{}'.format(attr),
                               color='yellow')

            add_to_context(Conference)
            add_to_context(ConferenceHolder)
            add_to_context(CategoryManager)
            add_to_context(AvatarHolder)
            add_to_context(Catalog)
            add_to_context(IndexesHolder)
            add_to_context(IndicoConfigWrapper(Config.getInstance()), 'config')
            add_to_context(LocalProxy(HelperMaKaCInfo.getMaKaCInfoInstance),
                           'minfo')
            add_to_context(current_app, 'app')

            for name, cls in sorted(db.Model._decl_class_registry.iteritems(),
                                    key=itemgetter(0)):
                if hasattr(cls, '__table__'):
                    add_to_context(cls, color='cyan')

            add_to_context(DBMgr.getInstance(),
                           'dbi',
                           doc='zodb db interface',
                           color='cyan!')
            add_to_context(db,
                           'db',
                           doc='sqlalchemy db interface',
                           color='cyan!')
            add_to_context(transaction,
                           doc='transaction module',
                           color='cyan!')
            signals.plugin.shell_context.send(add_to_context=add_to_context)

        return self._context
Exemple #7
0
def setup_jinja(app):
    config = Config.getInstance()
    # 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(IndicoConfigWrapper(config), 'indico_config')
    app.add_template_global(config.getSystemIconURL, 'system_icon')
    app.add_template_global(include_css_assets)
    app.add_template_global(include_js_assets)
    app.add_template_global(include_plugin_css_assets)
    app.add_template_global(include_plugin_js_assets)
    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(lambda: 'custom_js' in core_env, 'has_custom_js')
    app.add_template_global(lambda: 'custom_sass' in core_env,
                            'has_custom_sass')
    app.add_template_global(get_request_stats)
    # Global variables
    app.add_template_global(LocalProxy(get_current_locale), 'current_locale')
    # 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(natsort)
    app.add_template_filter(groupby)
    app.add_template_filter(any)
    app.add_template_filter(strip_tags)
    app.add_template_filter(alpha_enum)
    app.add_template_filter(crc32)
    app.add_template_filter(bool)
    # 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)
    # webassets
    app.jinja_env.add_extension('webassets.ext.jinja2.AssetsExtension')
    app.jinja_env.assets_environment = core_env
Exemple #8
0
def setup_jinja(app):
    config = Config.getInstance()
    # 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(IndicoConfigWrapper(config), 'indico_config')
    app.add_template_global(config.getSystemIconURL, 'system_icon')
    app.add_template_global(include_css_assets)
    app.add_template_global(include_js_assets)
    app.add_template_global(include_plugin_css_assets)
    app.add_template_global(include_plugin_js_assets)
    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_login)
    app.add_template_global(url_for_logout)
    app.add_template_global(lambda: unicode(uuid.uuid4()), 'uuid')
    app.add_template_global(icon_from_mimetype)
    # 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))
    # Filters (new ones returning unicode)
    app.add_template_filter(date_time_util.format_human_timedelta)
    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(natsort)
    app.add_template_filter(groupby)
    app.add_template_filter(any)
    app.add_template_filter(strip_tags)
    app.add_template_filter(alpha_enum)
    # 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)
    # webassets
    app.jinja_env.add_extension('webassets.ext.jinja2.AssetsExtension')
    app.jinja_env.assets_environment = core_env