def test_get_template_loader_for_path(self): path = os.path.join(self.tests_app.path, 'templates') loader = get_dmp_instance().get_template_loader_for_path( path, use_cache=False) self.assertIsInstance(loader, MakoTemplateLoader) template = loader.get_template('index.basic.html') self.assertIsInstance(template, MakoTemplateAdapter)
def compile_mako_files(self, app_config): '''Compiles the Mako templates within the apps of this system''' # go through the files in the templates, scripts, and styles directories moddir = os.path.dirname(app_config.module.__file__) for subdir_name in ('templates', 'scripts', 'styles'): subdir = os.path.join(moddir, subdir_name) if os.path.exists(subdir): for filename in os.listdir(subdir): fname, ext = os.path.splitext(filename) if ext.lower() in ( '.htm', '.html', '.jsm', '.cssm' ): # create the template object, which creates the compiled .py file renderer = get_dmp_instance().get_template_loader(app_config.name, subdir_name) renderer.get_template(filename)
def compile_mako_files(self, app_config): '''Compiles the Mako templates within the apps of this system''' # go through the files in the templates, scripts, and styles directories moddir = os.path.dirname(app_config.module.__file__) for subdir_name in ('templates', 'scripts', 'styles'): subdir = os.path.join(moddir, subdir_name) if os.path.exists(subdir): for filename in os.listdir(subdir): fname, ext = os.path.splitext(filename) if ext.lower() in ('.htm', '.html', '.jsm', '.cssm'): # create the template object, which creates the compiled .py file renderer = get_dmp_instance().get_template_loader( app_config.name, subdir_name) renderer.get_template(filename)
def template_scripts(self, config, template_name): ''' Returns a list of scripts used by the given template object AND its ancestors. This runs a ProviderRun on the given template (as if it were being displayed). This allows the WEBPACK_PROVIDERS to provide the JS files to us. For this algorithm to work, the providers must extend static_links.LinkProvider because that class creates the 'enabled' list of provider instances, and each provider instance has a url. ''' template_obj = get_dmp_instance().get_template_loader(config, create=True).get_mako_template(template_name, force=True) mako_context = create_mako_context(template_obj) inner_run = ProviderRun(mako_context['self'], factories=self.factories) inner_run.run() scripts = [] for data in inner_run.column_data: for provider in data.get('enabled', []): # if file doesn't exist, the provider won't be enabled if hasattr(provider, 'filepath'): # providers used to collect for webpack must have a .filepath property scripts.append(provider.filepath) # the absolute path to the file (see providers/static_links.py) return scripts
def setUp(self): # resets all the Mako caches because we switch between debug and prod mode # during testing, and providers load differently for each get_dmp_instance().template_loaders = {}
def test_get_template_loader(self): loader = get_dmp_instance().get_template_loader('homepage', create=False) self.assertIsInstance(loader, MakoTemplateLoader) template = loader.get_template('index.basic.html') self.assertIsInstance(template, MakoTemplateAdapter)
def test_get_template(self): template = get_dmp_instance().get_template('homepage/index.basic.html') self.assertIsInstance(template, MakoTemplateAdapter) self.assertRaises(TemplateDoesNotExist, get_dmp_instance().get_template, 'homepage/nonexistent_template.html')
def test_from_string(self): template = get_dmp_instance().from_string('${ 2 + 2 }') self.assertIsInstance(template, MakoTemplateAdapter) self.assertEqual(template.render(None), "4")
def test_is_dmp_app(self): self.assertTrue(get_dmp_instance().is_dmp_app('tests')) self.assertFalse(get_dmp_instance().is_dmp_app('nonexistent_app'))