Django

Code

Ticket #6587: new_template_lib_loader_5.diff

File new_template_lib_loader_5.diff, 5.1 kB (added by oyvind, 9 months ago)

More use of import, tests pass

  • a/django/template/__init__.py

    old new  
    5959from django.utils.translation import ugettext as _ 
    6060from django.utils.safestring import SafeData, EscapeData, mark_safe, mark_for_escaping 
    6161from django.utils.html import escape 
     62from django.templatetags import get_templatetags_modules 
    6263 
    6364__all__ = ('Template', 'Context', 'RequestContext', 'compile_string') 
    6465 
     
    910911            return func 
    911912        return dec 
    912913 
    913 def get_library(module_name): 
    914     lib = libraries.get(module_name, None) 
     914def get_library(library_name, extra_modules=[]): 
     915    lib = libraries.get(library_name, None) 
    915916    if not lib: 
    916         try: 
    917             mod = __import__(module_name, {}, {}, ['']) 
    918         except ImportError, e: 
    919             raise InvalidTemplateLibrary("Could not load template library from %s, %s" % (module_name, e)) 
    920         try: 
    921             lib = mod.register 
    922             libraries[module_name] = lib 
    923         except AttributeError: 
    924             raise InvalidTemplateLibrary("Template library %s does not have a variable named 'register'" % module_name) 
     917        templatetags_modules = get_templatetags_modules() 
     918        search_modules = extra_modules + templatetags_modules 
     919        for m in search_modules:  
     920            app_label = m.split('.')[-1] 
     921            "Allow both library_name and app_label.library_name" 
     922            try: 
     923                app, library = library_name.split('.') 
     924            except: 
     925                app, library = ('', library_name) 
     926            if not app or app == app_label: 
     927                try: 
     928                    module_name = '%s.%s' % (m, library) 
     929                    mod = __import__(module_name, {}, {}, ['']) 
     930                except ImportError: 
     931                    continue 
     932            else: 
     933                continue 
     934            try: 
     935                lib = mod.register 
     936                libraries[library_name] = lib 
     937            except AttributeError: 
     938                raise InvalidTemplateLibrary("Template library %s does not have a variable named 'register'" % module_name) 
     939        if not lib: 
     940            raise InvalidTemplateLibrary("Template library %s not found, tried %s" % (library_name, str(search_modules)))  
    925941    return lib 
    926942 
    927 def add_to_builtins(module_name): 
    928     builtins.append(get_library(module_name)) 
     943def add_to_builtins(module_name, library_name): 
     944    builtins.append(get_library(library_name, extra_modules=[module_name])) 
    929945 
    930 add_to_builtins('django.template.defaulttags') 
    931 add_to_builtins('django.template.defaultfilters') 
     946add_to_builtins('django.template', 'defaulttags') 
     947add_to_builtins('django.template', 'defaultfilters') 
  • a/django/template/defaulttags.py

    old new  
    850850    for taglib in bits[1:]: 
    851851        # add the library to the parser 
    852852        try: 
    853             lib = get_library("django.templatetags.%s" % taglib) 
     853            lib = get_library(taglib) 
    854854            parser.add_library(lib) 
    855855        except InvalidTemplateLibrary, e: 
    856856            raise TemplateSyntaxError("'%s' is not a valid tag library: %s" % 
  • a/django/template/loader.py

    old new  
    115115    # If we get here, none of the templates could be loaded 
    116116    raise TemplateDoesNotExist, ', '.join(template_name_list) 
    117117 
    118 add_to_builtins('django.template.loader_tags') 
     118add_to_builtins('django.template', 'loader_tags') 
  • a/django/templatetags/__init__.py

    old new  
    11from django.conf import settings 
     2import os 
    23 
    3 for a in settings.INSTALLED_APPS: 
    4     try: 
    5         __path__.extend(__import__(a + '.templatetags', {}, {}, ['']).__path__) 
    6     except ImportError: 
    7         pass 
     4templatetags_modules = []  
     5 
     6def get_templatetags_modules(): 
     7    if not templatetags_modules: 
     8        for a in ['django'] + list(settings.INSTALLED_APPS): 
     9            try: 
     10                name = a + '.templatetags' 
     11                mod = __import__(name, {}, {}, ['']) 
     12                templatetags_modules.append(name) 
     13            except ImportError: 
     14                pass 
     15    return templatetags_modules  
  • a/tests/regressiontests/templates/tests.py

    old new  
    4545 
    4646register.tag("echo", do_echo) 
    4747 
    48 template.libraries['django.templatetags.testtags'] = register 
     48template.libraries['testtags'] = register 
    4949 
    5050##################################### 
    5151# Helper objects for template tests #