Django

Code

Ticket #2539: template_04.diff

File template_04.diff, 5.5 kB (added by racter, 10 months ago)

adjusted limodou's patch to apply to trunk as of 6213

  • django/templatetags/__init__.py

    old new  
    22 
    33for a in settings.INSTALLED_APPS: 
    44    try: 
     5        path = __import__(a[:a.rindex('.')], '', '', ['']).__path__ 
     6        for i in path: 
     7            if not i in __path__: 
     8                __path__.append(i) 
    59        __path__.extend(__import__(a + '.templatetags', {}, {}, ['']).__path__) 
    610    except ImportError: 
    711        pass 
  • django/template/__init__.py

    old new  
    6363from django.utils.text import smart_split 
    6464from django.utils.encoding import smart_unicode, force_unicode 
    6565from django.utils.translation import ugettext as _ 
     66from django.utils.datastructures import SortedDict 
    6667 
    6768__all__ = ('Template', 'Context', 'RequestContext', 'compile_string') 
    6869 
     
    261262        self.tokens = tokens 
    262263        self.tags = {} 
    263264        self.filters = {} 
     265        self.libs = {} 
    264266        for lib in builtins: 
    265267            self.add_library(lib) 
    266268 
     
    289291                # execute callback function for this tag and append resulting node 
    290292                self.enter_command(command, token) 
    291293                try: 
    292                     compile_func = self.tags[command] 
     294                    compile_func = self.get_tags(command) 
    293295                except KeyError: 
    294296                    self.invalid_block_tag(token, command) 
    295297                try: 
     
    355357    def add_library(self, lib): 
    356358        self.tags.update(lib.tags) 
    357359        self.filters.update(lib.filters) 
     360        #save index of lib 
     361        if hasattr(lib, 'appname'): 
     362            d = self.libs.setdefault(lib.taglibname, SortedDict({})) 
     363            d[lib.appname] = {'tags':lib.tags, 'filters': lib.filters} 
    358364 
     365    def get_tags(self, tag): 
     366        v = tag.split('.') 
     367        if len(v) == 3: 
     368            appname, taglibname, command = v 
     369            return self.libs[taglibname][appname]['tags'][command] 
     370        elif len(v) == 2: 
     371            taglibname, command = v 
     372            return self.libs[taglibname].values()[0]['tags'][command] 
     373        else: 
     374            return self.tags[tag] 
     375 
     376    def get_filters(self, filter_name): 
     377        v = filter_name.split('.') 
     378        if len(v) == 3: 
     379            appname, taglibname, command = v 
     380            return self.libs[taglibname][appname]['filters'][command] 
     381        elif len(v) == 2: 
     382            taglibname, command = v 
     383            return self.libs[taglibname].values()[0]['filters'][command] 
     384        else: 
     385            return self.filters[filter_name] 
     386 
    359387    def compile_filter(self, token): 
    360388        "Convenient wrapper for FilterExpression" 
    361389        return FilterExpression(token, self) 
    362390 
    363391    def find_filter(self, filter_name): 
    364         if filter_name in self.filters: 
    365             return self.filters[filter_name] 
    366         else: 
     392        try: 
     393            return self.get_filters(filter_name) 
     394        except: 
     395            import traceback 
     396            traceback.print_exc() 
    367397            raise TemplateSyntaxError, "Invalid filter: '%s'" % filter_name 
    368398 
    369399class DebugParser(Parser): 
     
    503533^"(?P<constant>%(str)s)"| 
    504534^(?P<var>[%(var_chars)s]+)| 
    505535 (?:%(filter_sep)s 
    506      (?P<filter_name>\w+) 
     536     (?P<filter_name>[\w.]+) 
    507537         (?:%(arg_sep)s 
    508538             (?: 
    509539              %(i18n_open)s"(?P<i18n_arg>%(str)s)"%(i18n_close)s| 
     
    914944            return func 
    915945        return dec 
    916946 
     947import os.path 
    917948def get_library(module_name): 
    918949    lib = libraries.get(module_name, None) 
    919950    if not lib: 
    920951        try: 
    921952            mod = __import__(module_name, {}, {}, ['']) 
     953            #get the appname and tag module name from the __file__ 
     954            v = mod.__file__.replace('\\', '/').split('/') 
     955            if v[-2] == 'templatetags': 
     956                appname, taglibname = v[-3], os.path.splitext(v[-1])[0] 
     957            else: 
     958                appname, taglibname = None, None 
    922959        except ImportError, e: 
     960            import traceback 
     961            traceback.print_exc() 
    923962            raise InvalidTemplateLibrary, "Could not load template library from %s, %s" % (module_name, e) 
    924963        try: 
    925964            lib = mod.register 
     965 
     966            # assign appname and taglibname to lib 
     967            lib.appname = appname 
     968            lib.taglibname = taglibname 
    926969            libraries[module_name] = lib 
    927970        except AttributeError: 
    928971            raise InvalidTemplateLibrary, "Template library %s does not have a variable named 'register'" % module_name 
  • django/template/defaulttags.py

    old new  
    795795    for taglib in bits[1:]: 
    796796        # add the library to the parser 
    797797        try: 
    798             lib = get_library("django.templatetags.%s" % taglib.split('.')[-1]) 
     798            if taglib.find('.') > -1: 
     799                appname, module = taglib.split('.') 
     800                taglib = '.'.join([appname, 'templatetags', module]) 
     801            lib = get_library("django.templatetags.%s" % taglib) 
    799802            parser.add_library(lib) 
    800803        except InvalidTemplateLibrary, e: 
     804            import traceback 
     805            traceback.print_exc() 
    801806            raise TemplateSyntaxError, "'%s' is not a valid tag library: %s" % (taglib, e) 
    802807    return LoadNode() 
    803808load = register.tag(load)