Django

Code

Ticket #1321: modularised_templates.diff

File modularised_templates.diff, 11.3 kB (added by lukeplant, 3 years ago)

Patch to begin implementation of this ticket

  • django/utils/datastructures.py

    old new  
    224224                current[bits[-1]] = v 
    225225            except TypeError: # Special-case if current isn't a dict. 
    226226                current = {bits[-1] : v} 
     227 
     228class LazySettings: 
     229    """ 
     230    A lazy proxy for either global django settings or a custom settings object. 
     231    """ 
     232    def __init__(self): 
     233        self.__dict__['_target'] = None 
     234 
     235    def __getattr__(self, name): 
     236        if self._target is None: 
     237            self.import_settings() 
     238        return getattr(self._target, name) 
     239 
     240    def __setattr__(self, name, value): 
     241        if name == '_target': 
     242            self.__dict__[name] = value 
     243        else: 
     244            setattr(self._target, name, value) 
     245 
     246    def import_settings(self): 
     247        from django.conf import settings 
     248        self.__dict__['_target'] = settings 
  • django/template/defaultfilters.py

    old new  
    11"Default variable filters" 
    22 
    3 from django.template import resolve_variable, Library 
    4 from django.conf import settings 
     3from django.template import resolve_variable, Library, settings 
    54from django.utils.translation import gettext 
    65import re 
    76import random as random_module 
     
    327326# DATES           # 
    328327################### 
    329328 
    330 def date(value, arg=settings.DATE_FORMAT): 
     329def date(value, arg=None): 
    331330    "Formats a date according to the given format" 
    332331    from django.utils.dateformat import format 
     332    if arg is None: 
     333        arg = settings.DATE_FORMAT 
    333334    return format(value, arg) 
    334335 
    335 def time(value, arg=settings.TIME_FORMAT): 
     336def time(value, arg=None): 
    336337    "Formats a time according to the given format" 
    337338    from django.utils.dateformat import time_format 
     339    if arg is None: 
     340        arg = settings.TIME_FORMAT 
    338341    return time_format(value, arg) 
    339342 
    340343def timesince(value): 
  • django/template/__init__.py

    old new  
    5757import re 
    5858from inspect import getargspec 
    5959from django.utils.functional import curry 
    60 from django.conf import settings 
     60from django.utils.datastructures import LazySettings 
     61settings = LazySettings() 
    6162from django.template.context import Context, RequestContext, ContextPopException 
    6263 
    6364__all__ = ('Template', 'Context', 'RequestContext', 'compile_string') 
     
    9091# global list of libraries to load by default for a new parser 
    9192builtins = [] 
    9293 
     94################# 
     95# CONFIGURATION # 
     96################# 
     97class TemplateSettings: 
     98    # TODO - eliminate duplication of global_settings? 
     99    TEMPLATE_DIRS = ()  
     100    DEFAULT_CHARSET = "utf-8" 
     101    DEBUG = False 
     102    DATE_FORMAT = 'N j, Y' 
     103    DATETIME_FORMAT = 'N j, Y, P' 
     104    TEMPLATE_LOADERS = ( 
     105        'django.template.loaders.filesystem.load_template_source', 
     106        'django.template.loaders.app_directories.load_template_source', 
     107#        'django.template.loaders.eggs.load_template_source', 
     108    ) 
     109    TEMPLATE_FILE_EXTENSION = '.html' 
     110    TEMPLATE_DEBUG = False 
     111    TEMPLATE_STRING_IF_INVALID = '' 
     112 
     113def configure(**options): 
     114    """Set configuration options for the template package.""" 
     115    global settings  
     116    new_settings = TemplateSettings() 
     117    for k, v in options.items(): 
     118        if not hasattr(new_settings, k): 
     119            raise Exception('"%s" is not a valid template config setting' % k) 
     120        setattr(new_settings, k, v) 
     121    settings._target = new_settings 
     122 
     123####################### 
     124# TEMPLATE EXCEPTIONS # 
     125####################### 
    93126class TemplateSyntaxError(Exception): 
    94127    pass 
    95128 
  • django/template/loaders/app_directories.py

    old new  
    11# Wrapper for loading templates from "template" directories in installed app packages. 
    22 
    3 from django.conf import settings 
    43from django.core.exceptions import ImproperlyConfigured 
    5 from django.template import TemplateDoesNotExist 
     4from django.template import TemplateDoesNotExist, settings 
    65import os 
    76 
    8 # At compile time, cache the directories to search. 
    9 app_template_dirs = [] 
    10 for app in settings.INSTALLED_APPS: 
    11     i = app.rfind('.') 
    12     if i == -1: 
    13         m, a = app, None 
    14     else: 
    15         m, a = app[:i], app[i+1:] 
    16     try: 
    17         if a is None: 
    18             mod = __import__(m, '', '', []) 
    19         else: 
    20             mod = getattr(__import__(m, '', '', [a]), a) 
    21     except ImportError, e: 
    22         raise ImproperlyConfigured, 'ImportError %s: %s' % (app, e.args[0]) 
    23     template_dir = os.path.join(os.path.dirname(mod.__file__), 'templates') 
    24     if os.path.isdir(template_dir): 
    25         app_template_dirs.append(template_dir) 
     7app_template_dirs = None 
    268 
    27 # It won't change, so convert it to a tuple to save memory. 
    28 app_template_dirs = tuple(app_template_dirs) 
     9def _ensure_setup(): 
     10    # cache the directories to search. 
     11    global app_template_dirs 
     12    if app_template_dirs is None: 
     13        app_template_dirs = [] 
     14        for app in settings.INSTALLED_APPS: 
     15            i = app.rfind('.') 
     16            if i == -1: 
     17                m, a = app, None 
     18            else: 
     19                m, a = app[:i], app[i+1:] 
     20            try: 
     21                if a is None: 
     22                    mod = __import__(m, '', '', []) 
     23                else: 
     24                    mod = getattr(__import__(m, '', '', [a]), a) 
     25            except ImportError, e: 
     26                raise ImproperlyConfigured, 'ImportError %s: %s' % (app, e.args[0]) 
     27            template_dir = os.path.join(os.path.dirname(mod.__file__), 'templates') 
     28            if os.path.isdir(template_dir): 
     29                app_template_dirs.append(template_dir) 
     30     
     31        # It won't change, so convert it to a tuple to save memory. 
     32        app_template_dirs = tuple(app_template_dirs) 
    2933 
    3034def get_template_sources(template_name, template_dirs=None): 
     35    _ensure_setup() 
    3136    for template_dir in app_template_dirs: 
    3237        yield os.path.join(template_dir, template_name) + settings.TEMPLATE_FILE_EXTENSION 
    3338 
  • django/template/loaders/filesystem.py

    old new  
    11# Wrapper for loading templates from the filesystem. 
    22 
    3 from django.conf import settings 
    4 from django.template import TemplateDoesNotExist 
     3from django.template import TemplateDoesNotExist, settings 
    54import os 
    65 
    76def get_template_sources(template_name, template_dirs=None): 
  • django/template/defaulttags.py

    old new  
    22 
    33from django.template import Node, NodeList, Template, Context, resolve_variable 
    44from django.template import TemplateSyntaxError, VariableDoesNotExist, BLOCK_TAG_START, BLOCK_TAG_END, VARIABLE_TAG_START, VARIABLE_TAG_END 
    5 from django.template import get_library, Library, InvalidTemplateLibrary 
    6 from django.conf import settings 
     5from django.template import get_library, Library, InvalidTemplateLibrary, settings 
    76import sys 
    87 
    98register = Library() 
  • django/template/context.py

    old new  
    1 from django.conf import settings 
     1from django.template import settings 
    22from django.core.exceptions import ImproperlyConfigured 
    33 
    44_standard_context_processors = None 
  • django/template/loader_tags.py

    old new  
    11from django.template import TemplateSyntaxError, TemplateDoesNotExist, resolve_variable 
    22from django.template import Library, Context, Node 
    33from django.template.loader import get_template, get_template_from_string, find_template_source 
    4 from django.conf import settings 
     4from django.template import settings 
    55 
    66register = Library() 
    77 
  • django/template/loader.py

    old new  
    2222 
    2323from django.core.exceptions import ImproperlyConfigured 
    2424from django.template import Origin, StringOrigin, Template, Context, TemplateDoesNotExist, add_to_builtins 
    25 from django.conf import settings 
     25from django.template import settings 
    2626 
    2727template_source_loaders = None 
    2828 
  • tests/standalone_tests.py

    old new  
     1#!/usr/bin/env python 
     2 
     3# Runs the test scripts in the 'standalone' dir 
     4import os 
     5STANDALONE_TESTS_DIR = "standalone" 
     6 
     7standalone_tests_dir = os.path.join(os.path.dirname(__file__), STANDALONE_TESTS_DIR) 
     8for f in os.listdir(standalone_tests_dir): 
     9    if f.endswith('.py') and not f.startswith('__init__'): 
     10        # run script 
     11        # TODO - What's the correct cross platform way to do this? 
     12        print "Running test script %s:" % f 
     13        print "---------------------" + "-" * len(f) 
     14        os.system('"%s"' % os.path.join(standalone_tests_dir, f)) 
  • tests/standalone/components.py

    old new  
     1#!/usr/bin/env python 
     2 
     3# Tests to ensure that Django components are usable without  
     4# a DJANGO_SETTINGS_MODULE.  This must be executed as a standalone  
     5# script to make sure django.settings hasn't already been imported 
     6 
     7import os 
     8try: 
     9    del os.environ['DJANGO_SETTINGS_MODULE'] 
     10except KeyError: 
     11    pass 
     12 
     13import unittest 
     14 
     15class TestTemplate(unittest.TestCase): 
     16 
     17    def test_import(self): 
     18        # make sure we can import django.template without exceptions 
     19        from django import template 
     20 
     21    def test_loader(self): 
     22        from django import template 
     23        from django.template import loader 
     24 
     25        template.configure(TEMPLATE_DIRS=(os.path.dirname(__file__),)) 
     26        s = loader.render_to_string('template_test') 
     27        self.assertEqual(s, 'Hello world') 
     28 
     29if __name__ == '__main__': 
     30    unittest.main() 
     31 
  • tests/standalone/template_test.html

    old new