def get_django_filter(django_filter, load='django'): if django.VERSION < (1, 9): from django.template.base import get_library if load and not load == 'django': library = get_library(load) else: library_path = 'django.template.defaultfilters' if django.VERSION > (1, 8): from django.template.base import import_library library = import_library(library_path) else: from django.template import import_library library = import_library(library_path) else: from django.template.backends.django import get_installed_libraries from django.template.library import import_library libraries = get_installed_libraries() if load and not load == 'django': library_path = libraries.get(load) if not library_path: raise Exception( 'templatetag "{}" is not registered'.format(load)) else: library_path = 'django.template.defaultfilters' library = import_library(library_path) filter_method = library.filters.get(django_filter) if not filter_method: raise Exception( 'filter "{}" not exist on {} templatetag package'.format( django_filter, load)) return filter_method
def get_django_filter(django_filter, load='django'): if django.VERSION < (1, 9): from django.template.base import get_library if load and not load == 'django': library = get_library(load) else: library_path = 'django.template.defaultfilters' if django.VERSION > (1, 8): from django.template.base import import_library library = import_library(library_path) else: from django.template import import_library library = import_library(library_path) else: from django.template.backends.django import get_installed_libraries from django.template.library import import_library libraries = get_installed_libraries() if load and not load == 'django': library_path = libraries.get(load) if not library_path: raise Exception('templatetag "{}" is not registered'.format(load)) else: library_path = 'django.template.defaultfilters' library = import_library(library_path) filter_method = library.filters.get(django_filter) if not filter_method: raise Exception('filter "{}" not exist on {} templatetag package'.format( django_filter, load )) return filter_method
def get_module_library(library_name): """ Load the template library module with the given name. If library is not already loaded loop over all templatetags modules to locate it. {% load somelib %} and {% load someotherlib %} loops twice. Subsequent loads eg. {% load somelib %} in the same process will grab the cached module from libraries. """ lib = libraries.get(library_name, None) if not lib: templatetags_modules = get_modules() tried_modules = [] for module in templatetags_modules: taglib_module = '%s.%s' % (module, library_name) tried_modules.append(taglib_module) lib = import_library(taglib_module) if lib: libraries[library_name] = lib break if not lib: raise InvalidTemplateLibrary( "Template library {} not found, tried {}".format( library_name, ','.join(tried_modules) ) ) return lib
def compile_string(self, template_string, origin): if self.debug: from django.template.debug import DebugLexer, DebugParser lexer_class, parser_class = DebugLexer, DebugParser else: lexer_class, parser_class = Lexer, Parser lexer = lexer_class(template_string, origin) tokens = lexer.tokenize() parser = parser_class(tokens) if self.use_form_tags: # parser.add_library(import_library('template_forms.templatetags.loader_tags')) parser.add_library(import_library('template_forms.templatetags.forms')) return parser.parse()
def compile_string(self, template_string, origin): if self.debug: from django.template.debug import DebugLexer, DebugParser lexer_class, parser_class = DebugLexer, DebugParser else: lexer_class, parser_class = Lexer, Parser lexer = lexer_class(template_string, origin) tokens = lexer.tokenize() parser = parser_class(tokens) if self.use_form_tags: # parser.add_library(import_library('template_forms.templatetags.loader_tags')) parser.add_library( import_library('template_forms.templatetags.forms')) return parser.parse()
def get_library(library_name, app_name=None): """ (Forked from django.template.get_library) Load the template library module with the given name. If library is not already loaded loop over all templatetags modules to locate it. {% load somelib %} and {% load someotherlib %} loops twice. """ #TODO: add in caching. (removed when forked from django.template.get_library). templatetags_modules = get_templatetags_modules() tried_modules = [] best_match_lib = None last_found_lib = None app_name_parts = 0 if app_name: app_name_parts = app_name.count('.') for module in templatetags_modules: taglib_module = '%s.%s' % (module, library_name) tried_modules.append(taglib_module) lib = import_library(taglib_module) if not lib: continue last_found_lib = lib if not app_name: continue module_list = module.split('.') module_list.pop() # remove the last part 'templetags' current_app = '.'.join(module_list) if current_app == app_name: break start = len(module_list) - app_name_parts - 1 if start < 0: continue partial_app = '.'.join(module_list[start:]) if partial_app == app_name: best_match_lib = lib if best_match_lib: last_found_lib = best_match_lib if not last_found_lib: raise InvalidTemplateLibrary("Template library %s not found, tried %s" % (library_name, ','.join(tried_modules))) return last_found_lib
def render_django_template(self, template_path, context=None, i18n_service=None): """ Evaluate a django template by resource path, applying the provided context. """ context = context or {} context['_i18n_service'] = i18n_service libraries = { 'i18n': 'xblockutils.templatetags.i18n', } # For django 1.8, we have to load the libraries manually, and restore them once the template is rendered. _libraries = None if django.VERSION[0] == 1 and django.VERSION[1] == 8: _libraries = TemplateBase.libraries.copy() for library_name in libraries: library = TemplateBase.import_library(libraries[library_name]) if library: TemplateBase.libraries[library_name] = library engine = Engine() else: # Django>1.8 Engine can load the extra templatetag libraries itself # but we have to override the default installed libraries. from django.template.backends.django import get_installed_libraries installed_libraries = get_installed_libraries() installed_libraries.update(libraries) engine = Engine(libraries=installed_libraries) template_str = self.load_unicode(template_path) template = Template(template_str, engine=engine) rendered = template.render(Context(context)) # Restore the original TemplateBase.libraries if _libraries is not None: TemplateBase.libraries = _libraries return rendered
def get_library(library_name): """ An extension to Django's django.template.base.get_library, which allows tags to be loaded from specific apps using the 'app:taglib' syntax. So if in 'app1' you had templatetags/foo.py, you could load it as: {% import app1:foo %} or if you wanted to load a specific tag: {% import mytag from app1:foo %} This uses the same syntax as referencing URL patterns from specific apps, and does not clash with the '.' notation already used for loading modules within templatetags (e.g. templatetags/news/posts.py as news.posts). Without this functionality, Django template tags become a global namespace issue, where different apps can easily clash with one another. """ # Extract the specific information regarding the app and module from # the library name, and calculate the fully qualified name. app_name = "" tagparts = library_name.split(":") if len(tagparts) > 1: app_name = tagparts[0] library_name = tagparts[1] qualified_library_name = "%s:%s" % (app_name, library_name) else: library_name = tagparts[0] qualified_library_name = library_name # See if it exists in the cache lib = libraries.get(qualified_library_name, None) # If it isn't, we're going to look. Even though we know which app we want # to load it from, we're going to loop over the all modules to avoid # introducing significant new code, and since the result is going to be # cached. if not lib: templatetags_modules = get_templatetags_modules() tried_modules = [] for module in templatetags_modules: taglib_module = '%s.%s' % (module, library_name) tried_modules.append(taglib_module) lib = import_library(taglib_module) if lib: # We managed to load a library, but now we need to see if it is # from the right app. We can do that by finding out which app # it came from. if app_name: lib_app = taglib_module[:taglib_module.index('.')] lib_name = "%s:%s" % (lib_app, library_name) else: lib_name = library_name # OK, is it the right one? If so, store it in the cache if lib_name == qualified_library_name: libraries[lib_name] = lib break # Haven't found it yet, keep going. lib = None # If we don't find any, we throw an exception with the ones we've tried. if not lib: raise InvalidTemplateLibrary("Template library %s not found, " "tried %s" % (library_name, ','.join(tried_modules))) return lib
def import_library(module): return tplbase.import_library(module)
def get_library(library_name): """ An extension to Django's django.template.base.get_library, which allows tags to be loaded from specific apps using the 'app:taglib' syntax. So if in 'app1' you had templatetags/foo.py, you could load it as: {% import app1:foo %} or if you wanted to load a specific tag: {% import mytag from app1:foo %} This uses the same syntax as referencing URL patterns from specific apps, and does not clash with the '.' notation already used for loading modules within templatetags (e.g. templatetags/news/posts.py as news.posts). Without this functionality, Django template tags become a global namespace issue, where different apps can easily clash with one another. """ # Extract the specific information regarding the app and module from # the library name, and calculate the fully qualified name. app_name = "" tagparts = library_name.split(":") if len(tagparts) > 1: app_name = tagparts[0] library_name = tagparts[1] qualified_library_name = "%s:%s" % (app_name, library_name) else: library_name = tagparts[0] qualified_library_name = library_name # See if it exists in the cache lib = libraries.get(qualified_library_name, None) # If it isn't, we're going to look. Even though we know which app we want # to load it from, we're going to loop over the all modules to avoid # introducing significant new code, and since the result is going to be # cached. if not lib: templatetags_modules = get_templatetags_modules() tried_modules = [] for module in templatetags_modules: taglib_module = '%s.%s' % (module, library_name) tried_modules.append(taglib_module) lib = import_library(taglib_module) if lib: # We managed to load a library, but now we need to see if it is # from the right app. We can do that by finding out which app # it came from. if app_name: lib_app = taglib_module[:taglib_module.index('.')] lib_name = "%s:%s" % (lib_app, library_name) else: lib_name = library_name # OK, is it the right one? If so, store it in the cache if lib_name == qualified_library_name: libraries[lib_name] = lib break # Haven't found it yet, keep going. lib = None # If we don't find any, we throw an exception with the ones we've tried. if not lib: raise InvalidTemplateLibrary( "Template library %s not found, " "tried %s" % (library_name, ','.join(tried_modules))) return lib