3 from forum.skins import load_template_source as skins_template_loader, Template, BaseTemplateLoader
4 from django.conf import settings
6 MODULES_TEMPLATE_PREFIX = 'modules/'
7 NO_OVERRIDE_TEMPLATE_PREFIX = 'no_override/'
8 MODULES_TEMPLATE_FOLDER = 'templates'
9 MODULES_TEMPLATE_OVERRIDES_FOLDER = 'template_overrides'
11 TEMPLATE_OVERRIDE_LOOKUP_PATHS = [f for f in [
12 os.path.join(os.path.dirname(m.__file__), MODULES_TEMPLATE_OVERRIDES_FOLDER) for m in settings.MODULE_LIST
13 ] if os.path.exists(f)
16 class ModulesTemplateLoader(BaseTemplateLoader):
18 modules_re = re.compile('^%s(\w+)\/(.*)$' % MODULES_TEMPLATE_PREFIX)
20 def load_template_source(self, name, dirs=None):
23 if name.startswith(MODULES_TEMPLATE_PREFIX):
24 match = self.modules_re.search(name)
25 file_name = os.path.join(settings.MODULES_FOLDER, match.group(1), MODULES_TEMPLATE_FOLDER, match.group(2))
27 if os.path.exists(file_name):
28 template = Template(file_name)
30 elif name.startswith(NO_OVERRIDE_TEMPLATE_PREFIX):
31 return skins_template_loader.load_template_source(name[len(NO_OVERRIDE_TEMPLATE_PREFIX):], dirs)
34 for override_path in TEMPLATE_OVERRIDE_LOOKUP_PATHS:
35 file_name = os.path.join(override_path, name)
37 if os.path.exists(file_name):
38 template = Template(file_name)
44 module_templates_loader = ModulesTemplateLoader()