def jinja2_factory(app): j2_config = { 'template_path': TEMPLATE_DIR, 'translations_path': LOCALE_DIR, 'environment_args': { 'auto_reload': False, 'cache_size': 400, 'extensions': [ 'jinja2.ext.autoescape', 'jinja2.ext.do', 'jinja2.ext.with_', 'jinja2.ext.i18n', ], 'trim_blocks': True, }, 'filters': FILTERS, 'globals': { 'uri_for': webapp2.uri_for, 'now': datetime.datetime.now(), 'getattr': getattr, 'len': len, 'enumerate': enumerate, 'name2rgb': name2rgb, } } return jinja2.Jinja2(app, config=j2_config)
def push_notification2(self, to_email, subject, template, notifcation_type, event, user, only_once=False, **template_val): if only_once and self._already_notified(notifcation_type, event, user): return if self._handler is None: email_body = jinja2.get_jinja2( factory=jinja2.Jinja2(webapp2.get_app()), app=webapp2.get_app()).render_template(template, **template_val) email_url = webapp2.uri_for('taskqueue-send-email') else: email_body = self._handler.jinja2.render_template( template, **template_val) email_url = self._handler.uri_for('taskqueue-send-email') taskqueue.add(url=email_url, params={ 'to': to_email, 'subject': subject, 'body': email_body }) notification = Notification() notification.type = notifcation_type notification.event = event.key notification.user = user.key notification.put()
def test_set_jinja2(self): app = webapp2.WSGIApplication() self.assertEqual(len(app.registry), 0) jinja2.set_jinja2(jinja2.Jinja2(app), app=app) self.assertEqual(len(app.registry), 1) j = jinja2.get_jinja2(app=app) self.assertTrue(isinstance(j, jinja2.Jinja2))
def jinja2_factory(app, loaders=None): """Set configuration environment for Jinja2. Args: app -- (WSGIApplication) loaders -- (list) Jinja2 template loaders Return: (Jinja2) A Jinja2 instance. """ if loaders is None: loaders = template_loaders config = { 'environment_args': { 'extensions': EXTENSIONS, 'loader': loaders }, 'globals': { 'uri_for': uri_for, 'datetime': datetime }, 'filters': {} } j = jinja2.Jinja2(app, config=config) return j
def jinja2_factory(self, app): j = jinja2.Jinja2(app, config={'environment_args': {'cache_size': 0}}) j.environment.globals.update({ # Set global variables. 'link': self.link }) return j
def jinja2_factory(app): j = jinja2.Jinja2(app) j.environment.globals.update({ 'uri_for': webapp2.uri_for, 'quote_plus': quote_plus }) return j
def j2_factory(app): """ The factory function passed to get_jinja2. Args: app: the WSGIApplication """ return jinja2.Jinja2(app, BaseHandler.get_jinja2_config())
def jinja2_factory(app): "True ninja method for attaching additional globals/filters to jinja" j = jinja2.Jinja2(app) j.environment.globals.update({ 'uri_for': webapp2.uri_for, }) return j
def test_get_template_attribute(self): app = webapp2.WSGIApplication(config={ 'webapp2_extras.jinja2': { 'template_path': template_path, } }) j = jinja2.Jinja2(app) hello = j.get_template_attribute('hello.html', 'hello') self.assertEqual(hello('World'), 'Hello, World!')
def jinja2_factory(app): j = jinja2.Jinja2(app) j.environment.filters.update({'printjson': my_filters.printjson}) j.environment.tests.update({}) j.environment.globals.update({ # Set global variables. 'uri_for': webapp2.uri_for, }) return j
def jinja2_factory(app): logging.info("template path: %s", os.path.join(os.path.dirname(__file__), '../../templates')) j = jinja2.Jinja2(app, {'template_path': os.path.join(os.path.dirname(__file__), '../../templates')}) j.environment.globals.update({ # Set global variables. 'uri_for': webapp2.uri_for # ... }) return j
def jinja2_factory(app): j = jinja2.Jinja2(app) j.environment.filters.update({ #'naturaldelta':naturaldelta, }) j.environment.globals.update({ # 'Post': Post, #'ndb': ndb, # could be used for ndb.OR in templates }) return j
def jinja2_factory(app): "True ninja method for attaching additional globals/filters to jinja" config = {'template_path': app.config.get('templates_path', 'template'),} j = jinja2.Jinja2(app, config=config) j.environment.globals.update({ # 'config': app.config, 'uri_for': webapp2.uri_for, }) return j
def jinja2_factory(app): j = jinja2.Jinja2(app) j.environment.filters.update({'format_currency': format_currency}) j.environment.globals.update({ 'csrf_token': util.generate_csrf_token, 'getattr': getattr, 'str': str, 'uri_for': webapp2.uri_for }) return j
def jinja2_factory(app): from webapp2_extras import jinja2 jinja_config = dict(jinja2.default_config) jinja_config['template_path'] = config.TEMPLATES_DIR j = jinja2.Jinja2(app, jinja_config) j.environment.filters.update({}) j.environment.globals.update({ 'uri_for': webapp2.uri_for, }) return j
def jinja2_factory(app): j = jinja2.Jinja2(app) j.environment.filters.update({}) j.environment.tests.update({}) # j.package_path = 'views/templates' j.environment.globals.update({ # Set global variables. 'uri_for': webapp2.uri_for, # ... }) return j
def handle_response_error(request, response, exception): # If the exception is a HTTPException, use its error code else a generic 500 error code status_code = exception.code if isinstance(exception, webapp2.HTTPException) else 500 if settings.DEBUG or status_code >= 500: logging.exception(exception) j = jinja2.Jinja2(app=_APP, config={'template_path': TEMPLATE_PATH}) t = j.render_template(str(status_code) + '.html') response.write(t) response.set_status(status_code)
def jinja2_factory(app): template_dir = os.path.abspath( os.path.join(os.path.dirname(__file__), 'templates')) config = {'template_path': template_dir} jinja = jinja2.Jinja2(app, config=config) jinja.environment.globals.update({ 'sha256': hashlib.sha256, 'quote': urllib.quote, 'len': len, 'enumerate': enumerate, 'range': range, }) return jinja
def jinja2(self): """Returns a Jinja2 renderer cached in the app registry.""" jinja_config = { 'environment_args': { 'auto_reload': False, 'extensions': [ 'jinja2.ext.autoescape', 'jinja2.ext.with_', 'jinja2htmlcompress.SelectiveHTMLCompress', ], }, } return wa2_jinja2.Jinja2(app=self.app, config=jinja_config)
def jinja2_factory(self, app): j = jinja2.Jinja2(app) j.environment.globals.update({ 'APP_ENV': app.config['app_env'], 'uri_for': webapp2.uri_for, 'get_flashes': get_flashes }) j.environment.filters.update({ 'get_entity': utils.jinja2.filters.get_entity, 'tojson': utils.jinja2.filters.tojson, 'rebase_datetime': utils.jinja2.filters.rebase_datetime, 'bool_display': bool_display }) return j
def test_render_template_globals_filters(self): app = webapp2.WSGIApplication(config={ 'webapp2_extras.jinja2': { 'template_path': template_path, 'globals': dict(foo='fooglobal'), 'filters': dict(foo=lambda x: x + '-foofilter'), }, }) req = webapp2.Request.blank('/') app.set_globals(app=app, request=req) j = jinja2.Jinja2(app) message = 'fooglobal-foofilter' res = j.render_template('template3.html', message=message) self.assertEqual(res, message)
def jinja2_factory(self, app): j = jinja2.Jinja2(app) j.environment.filters.update({ # Set filters. }) j.environment.globals.update({ # Set global variables. 'uri_for': self.uri_for, 'static_url': self.static_url }) j.environment.tests.update({ # Set tests. # ... }) return j
def test_render_template_force_compiled(self): app = webapp2.WSGIApplication(config={ 'webapp2_extras.jinja2': { 'template_path': template_path, 'compiled_path': compiled_path, 'force_compiled': True, } }) req = webapp2.Request.blank('/') app.set_globals(app=app, request=req) j = jinja2.Jinja2(app) message = 'Hello, World!' res = j.render_template('template1.html', message=message) self.assertEqual(res, message)
def jinja2(self): ''' Returns a Jinja2 renderer cached in the app registry. Taken from http://webapp-improved.appspot.com/_modules/webapp2_extras/jinja2.html#Jinja2 since the get_jinja2 factory doesn't allow passing a config dict. ''' app = self.app or webapp2.get_app() key = 'webapp2_extras.jinja2.Jinja2' jinja = app.registry.get(key) if not jinja: jinja = app.registry[key] = jinja_helper.Jinja2(app) jinja.environment.filters['urlencode'] = utils.urlencode jinja.environment.tests['mobile'] = utils.is_mobile return jinja
def jinja2(self): """ Initializes (if not yet) and returns a cached Jinja2 instance. """ # Line Comment # The quick brown fox jumps over the lazy dog # THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG return jinja2.Jinja2(app=self.app, config={ 'environment_args': { 'trim_blocks': True }, 'globals': { 'uri_for': webapp2.uri_for } })
def push_notification(self, to_email, subject, template, **template_val): if self._handler is None: email_body = jinja2.get_jinja2( factory=jinja2.Jinja2(webapp2.get_app()), app=webapp2.get_app()).render_template(template, **template_val) email_url = webapp2.uri_for('taskqueue-send-email') else: email_body = self._handler.jinja2.render_template( template, **template_val) email_url = self._handler.uri_for('taskqueue-send-email') taskqueue.add(url=email_url, params={ 'to': to_email, 'subject': subject, 'body': email_body })
def jinja2_factory(app): j = jinja2.Jinja2(app) j.environment.filters.update({ # Set filters. # ... }) j.environment.globals.update({ # Set global variables. 'csrf_token': generate_csrf_token, 'uri_for': webapp2.uri_for, 'getattr': getattr, }) j.environment.tests.update({ # Set test. # ... }) return j
def jinja2_factory(app): j = jinja2.Jinja2(app) j.environment.filters.update({ # Set filters. 'bleach': bleach_clean, 'appliance_update': appliance_update }) j.environment.globals.update({ # Set global variables. 'csrf_token': generate_csrf_token, 'uri_for': webapp2.uri_for, 'getattr': getattr, 'str': str }) j.environment.tests.update({ # Set tests. # ... }) return j
def jinja2_factory(self, app): """ Adds on additional template filters, from templatetags/jinja_tags.py Reference: [1] http://webapp-improved.appspot.com/todo.html?highlight=jinja2%20filter#jinja2-factory [2] http://webapp-improved.appspot.com/api/webapp2_extras/jinja2.html?highlight=jinja2%20filter#webapp2_extras.jinja2.Jinja2 """ j = jinja2.Jinja2(app) j.environment.filters.update({ 'active': tags.active, 'naturaltime': tags.naturaltime, 'timeuntil': tags.timeuntil, 'timesince': tags.timesince, 'datetimeformat': tags.datetimeformat, 'sourcecode': tags.sourcecode, 'currency': tags.currency, }) return j
def jinja2_factory(app): j = jinja2.Jinja2(app) j.environment.filters.update({ # Set filters. 'epoch': epoch, 'timendate': timendate, 'base64encode': base64encode, }) j.environment.globals.update({ # Set global variables. 'csrf_token': generate_csrf_token, 'uri_for': webapp2.uri_for, 'getattr': getattr, 'str': str }) j.environment.tests.update({ # Set tests. # ... }) return j