def autopages(dirname="autopages"): """ Create URL-patterns for templates in template directory. If we have: autopages/ |- about.html `- company/ ` products.html The return value is: patterns('', url('^about/$', direct_to_template, {'template': 'autopages/about.html'}), url('^company/products/$', direct_to_template, {'template': 'autopages/company/products.html'}), ) """ templates = [] try: loader.find_template("") except loader.TemplateDoesNotExist: pass for l in loader.template_source_loaders: logger.debug('Getting templates from loader "%s".' % l) if hasattr(l, "get_template_sources"): directories = l.get_template_sources(dirname) logger.debug('"%s" directories: %s.' % (l, directories)) for directory in directories: dirtemplates = get_templates(directory) msg = 'Directory "%s" templates: %s.' % (directory, dirtemplates) logger.debug(msg) templates.extend(dirtemplates) else: msg = 'Loader "%s" has no attribute `template_source_loaders`.' % l logger.debug(msg) urls = [] for template in templates: pattern = urlpattern_from_path(template, dirname) logger.debug('Adding URL-pattern "%s" for template "%s".' % (pattern, template)) urls.append(pattern) return patterns("", *urls)
def test_default_args(self): path = 'dir/about.html' dirname = 'a' urlpattern = urlpattern_from_path(path, dirname) self.assertEquals( {'template': os.path.join(dirname, path)}, urlpattern.default_args)
def test_page_path(self): path = 'dir/about.html' urlpattern = urlpattern_from_path(path) regex = re.compile(r'^dir/about/$', re.UNICODE) self.assertEquals(regex, urlpattern.regex)