def test_get_default_none(self): from django.conf import global_settings from django_jinja.backend import Jinja2 with self.settings(TEMPLATES=global_settings.TEMPLATES): with self.assertRaisesRegexp(ImproperlyConfigured, r'No Jinja2 backend is configured'): Jinja2.get_default()
def test_get_default_multiple(self): from django_jinja.backend import Jinja2 with self.modify_settings(TEMPLATES={ 'append': [{"BACKEND": "django_jinja.backend.Jinja2", "NAME": "jinja2dup", "APP_DIRS": True, "OPTIONS": { "context_processors": [ "django.contrib.auth.context_processors.auth", "django.template.context_processors.debug", "django.template.context_processors.i18n", "django.template.context_processors.media", "django.template.context_processors.static", "django.template.context_processors.tz", "django.contrib.messages.context_processors.messages", ], "constants": { "foo": "bar", }, "extensions": settings.JINJA2_EXTENSIONS }}], }): with self.assertRaisesRegexp(ImproperlyConfigured, r'Several Jinja2 backends are configured'): Jinja2.get_default()
def current_pl_template(self, request, context=None): """Return a template of the PL with the session exercise context. If given, will use context instead.""" session_exercise = self.session_exercise() try: pl = session_exercise.pl if pl: return session_exercise.get_pl(request, context) else: dic = dict(session_exercise.context if not context else context) dic['user_settings__'] = session_exercise.session_activity.user.profile dic['user__'] = session_exercise.session_activity.user first_pls = session_exercise.session_activity.activity.indexed_pl() if first_pls: dic['first_pl__'] = first_pls[0].id env = Jinja2.get_default() for key in dic: if type(dic[key]) is str: dic[key] = env.from_string(dic[key]).render(context=dic, request=request) return get_template("activity/activity_type/pltp/pltp.html").render(dic, request) except Exception as e: # pragma: no cover error_msg = str(e) if request.user.profile.can_load(): error_msg += "<br><br>" + htmlprint.html_exc() return get_template("playexo/error.html").render({"error_msg": error_msg})
def test_get_default_multiple(self): setting = { "append": [{ "BACKEND": "django_jinja.backend.Jinja2", "NAME": "jinja2dup", "APP_DIRS": True, "OPTIONS": { "match_extension": ".jinjadup", } }] } with self.modify_settings(TEMPLATES=setting): with self.assertRaisesRegexp(ImproperlyConfigured, r'Several Jinja2 backends'): Jinja2.get_default()
def test_get_match_extension(self): if django.VERSION[:2] < (1, 8): self.assertEquals(getattr(settings, "DEFAULT_JINJA2_TEMPLATE_EXTENSION", ".jinja"), get_match_extension()) else: from django_jinja.backend import Jinja2 self.assertEquals(Jinja2.get_default().match_extension, get_match_extension())
def test_get_default_multiple(self): from django_jinja.backend import Jinja2 setting = { "append": [ { "BACKEND": "django_jinja.backend.Jinja2", "NAME": "jinja2dup", "APP_DIRS": True, "OPTIONS": {"match_extension": ".jinjadup"}, } ] } with self.modify_settings(TEMPLATES=setting): with self.assertRaisesRegexp(ImproperlyConfigured, r"Several Jinja2 backends"): Jinja2.get_default()
def test_get_match_extension(self): if django.VERSION[:2] < (1, 8): self.assertEquals( getattr(settings, 'DEFAULT_JINJA2_TEMPLATE_EXTENSION', '.jinja'), get_match_extension()) else: from django_jinja.backend import Jinja2 self.assertEquals(Jinja2.get_default().match_extension, get_match_extension())
def render(self, template, context, request): env = Jinja2.get_default() for k, v in context.items(): if isinstance(v, str): context[k] = env.from_string(v).render(context=context, request=request) return get_template(template).render( { "__components": Component.from_context(context), **context }, request)
def test_get_default(self): from django_jinja.backend import Jinja2 Jinja2.get_default.cache_clear() self.assertEqual(Jinja2.get_default(), self.env)
def test_get_match_extension(self): self.assertEquals(Jinja2.get_default().match_extension, get_match_extension())
def render_feedback(feedback): """Returns the given markdown string as an html string """ env = Jinja2.get_default() return env.from_string("{% with fh=f|markdown %}{{fh|safe}}{% endwith %}" ).render(context={'f': feedback})
import os from django.test import TestCase from django.test.client import RequestFactory from django.test.utils import override_settings from django_jinja.backend import Jinja2 from mock import patch from lib import l10n_utils ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'test_files') TEMPLATE_DIRS = (os.path.join(ROOT, 'templates'),) jinja_env = Jinja2.get_default() @patch.object(jinja_env.env.loader, 'searchpath', TEMPLATE_DIRS) @override_settings(ROOT=ROOT) @override_settings(DEV=False) class TestRender(TestCase): urls = 'lib.l10n_utils.tests.test_files.urls' def _test(self, path, template, locale, accept_lang, status, destination=None, active_locales=None, add_active_locales=None): request = RequestFactory().get(path) request.META['HTTP_ACCEPT_LANGUAGE'] = accept_lang request.locale = locale ctx = {} if active_locales: ctx['active_locales'] = active_locales
from nautobot_plugin_nornir.utils import get_dispatcher from nautobot_golden_config.models import GoldenConfigSetting, GoldenConfig from nautobot_golden_config.utilities.helper import ( get_job_filter, get_repository_working_dir, verify_global_settings, render_jinja_template, ) from nautobot_golden_config.utilities.graphql import graph_ql_query from nautobot_golden_config.nornir_plays.processor import ProcessGoldenConfig InventoryPluginRegister.register("nautobot-inventory", NautobotORMInventory) LOGGER = logging.getLogger(__name__) jinja_settings = Jinja2.get_default() jinja_env = jinja_settings.env def run_template( # pylint: disable=too-many-arguments task: Task, logger, global_settings, nautobot_job, jinja_root_path) -> Result: """Render Jinja Template. Only one template is supported, so the expectation is that that template includes all other templates. Args: task (Task): Nornir task individual object logger (NornirLogger): Logger to log messages to. global_settings (GoldenConfigSetting): The settings for GoldenConfigPlugin. nautobot_job (Result): The the output from the Nautobot Job instance being run.
def test_get_default_none(self): with self.settings(TEMPLATES=global_settings.TEMPLATES): with self.assertRaisesRegex(ImproperlyConfigured, r'No Jinja2 backend is configured'): Jinja2.get_default()
def test_get_default(self): Jinja2.get_default.cache_clear() self.assertEqual(Jinja2.get_default(), self.env)
from django_jinja.backend import Jinja2 from mock import call, Mock, patch from pyquery import PyQuery as pq from jinja2 import Markup from bedrock.base.urlresolvers import reverse from bedrock.firefox import views as fx_views from bedrock.firefox.firefox_details import FirefoxDesktop from bedrock.mozorg.tests import TestCase TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), 'test_data') PROD_DETAILS_DIR = os.path.join(TEST_DATA_DIR, 'product_details_json') GOOD_PLATS = {'Windows': {}, 'OS X': {}, 'Linux': {}} jinja_env = Jinja2.get_default().env class TestInstallerHelp(TestCase): def setUp(self): self.button_mock = Mock() self.patcher = patch.dict(jinja_env.globals, download_firefox=self.button_mock) self.patcher.start() self.view_name = 'firefox.installer-help' with self.activate('en-US'): self.url = reverse(self.view_name) def tearDown(self): self.patcher.stop()
from django.test import TestCase from django_jinja.backend import Jinja2 engine = Jinja2.get_default() class TestDjangoJinjaMarkdown(TestCase): def test_markdown_filter(self): self.assertEqual(engine.from_string('{{ "**bold dude**"|markdown }}').render(), '<p><strong>bold dude</strong></p>') def test_markdown_function(self): self.assertEqual(engine.from_string('{{ markdown("***angry walter***") }}').render(), '<p><strong><em>angry walter</em></strong></p>') def test_markdown_tag(self): self.assertEqual(engine.from_string('{% markdown %}*fancy donnie*{% endmarkdown %}').render(), '<p><em>fancy donnie</em></p>')