Ticket #6218: 6218.diff

File 6218.diff, 7.0 KB (added by Chris Beaven, 14 years ago)
  • django/conf/__init__.py

    diff --git a/django/conf/__init__.py b/django/conf/__init__.py
    index bda0ef3..7daf227 100644
    a b import re  
    1111import time     # Needed for Windows
    1212
    1313from django.conf import global_settings
     14from django.core.exceptions import ImproperlyConfigured
    1415from django.utils.functional import LazyObject
    1516from django.utils import importlib
    1617
    class LazySettings(LazyObject):  
    5960        return bool(self._wrapped)
    6061    configured = property(configured)
    6162
    62 class Settings(object):
     63
     64class BaseSettings(object):
     65    """
     66    Common logic for settings whether set by a module or by the user.
     67    """
     68    def __setattr__(self, name, value):
     69        if name == "MEDIA_URL" and value and value[-1] != '/':
     70            raise ImproperlyConfigured('If set, MEDIA_URL must end in a '
     71                                       'slash.')
     72        object.__setattr__(self, name, value)
     73
     74
     75class Settings(BaseSettings):
    6376    def __init__(self, settings_module):
    6477        # update this dict from global settings (but only for ALL_CAPS settings)
    6578        for setting in dir(global_settings):
    class Settings(object):  
    114127            os.environ['TZ'] = self.TIME_ZONE
    115128            time.tzset()
    116129
    117 class UserSettingsHolder(object):
     130
     131class UserSettingsHolder(BaseSettings):
    118132    """
    119133    Holder for user configured settings.
    120134    """
    class UserSettingsHolder(object):  
    138152    # For Python < 2.6:
    139153    __members__ = property(lambda self: self.__dir__())
    140154
    141 settings = LazySettings()
    142155
     156settings = LazySettings()
  • django/conf/global_settings.py

    diff --git a/django/conf/global_settings.py b/django/conf/global_settings.py
    index 6033297..4a9b9e2 100644
    a b DEFAULT_FILE_STORAGE = 'django.core.files.storage.FileSystemStorage'  
    263263MEDIA_ROOT = ''
    264264
    265265# URL that handles the media served from MEDIA_ROOT.
    266 # Example: "http://media.lawrence.com"
     266# Example: "http://media.lawrence.com/"
    267267MEDIA_URL = ''
    268268
    269269# List of upload handler classes to be applied in order.
  • django/conf/project_template/settings.py

    diff --git a/django/conf/project_template/settings.py b/django/conf/project_template/settings.py
    index c49df24..4edd67f 100644
    a b USE_L10N = True  
    4848MEDIA_ROOT = ''
    4949
    5050# URL that handles the media served from MEDIA_ROOT. Make sure to use a
    51 # trailing slash if there is a path component (optional in other cases).
    52 # Examples: "http://media.lawrence.com", "http://example.com/media/"
     51# trailing slash.
     52# Examples: "http://media.lawrence.com/", "http://example.com/media/"
    5353MEDIA_URL = ''
    5454
    5555# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
  • docs/ref/settings.txt

    diff --git a/docs/ref/settings.txt b/docs/ref/settings.txt
    index b5556de..58738a5 100644
    a b MEDIA_URL  
    10721072Default: ``''`` (Empty string)
    10731073
    10741074URL that handles the media served from ``MEDIA_ROOT``.
    1075 Example: ``"http://media.lawrence.com"``
     1075Example: ``"http://media.lawrence.com/"``
    10761076
    1077 Note that this should have a trailing slash if it has a path component.
    1078 
    1079 Good: ``"http://www.example.com/static/"``
    1080 Bad: ``"http://www.example.com/static"``
     1077It must end in a slash if set to a non-empty value.
    10811078
    10821079.. setting:: MIDDLEWARE_CLASSES
    10831080
  • docs/releases/1.3.txt

    diff --git a/docs/releases/1.3.txt b/docs/releases/1.3.txt
    index 51de6fd..52f1758 100644
    a b If you are currently using the ``mod_python`` request handler, it is strongly  
    8181encouraged you redeploy your Django instances using :doc:`mod_wsgi
    8282</howto/deployment/modwsgi>`.
    8383
     84Other changes in Django 1.3
     85===========================
     86
     87``MEDIA_URL`` must end in a slash
     88---------------------------------
     89
     90Previously, the ``MEDIA_URL`` setting only required a trailing slash if it
     91contained a suffix beyond the domain name.
     92
     93A trailing slash is now *required* for ``MEDIA_URL`` as long as it is not
     94blank. This ensures there is a consistent way to combine ``MEDIA_URL`` in
     95templates.
     96
     97Project settings modules which provide a ``MEDIA_URL`` without a trailing slash
     98will now raise an ``ImproperlyConfigured`` error.
     99
    84100What's new in Django 1.3
    85101========================
    86102
  • tests/regressiontests/settings_tests/tests.py

    diff --git a/tests/regressiontests/settings_tests/tests.py b/tests/regressiontests/settings_tests/tests.py
    index fa217b1..08d5b0a 100644
    a b  
    11import unittest
    2 from django.conf import settings
     2from django.conf import settings, UserSettingsHolder, global_settings
     3from django.core.exceptions import ImproperlyConfigured
     4
     5   
     6def setattr_settings(settings_module, attr, value):
     7    setattr(settings_module, attr, value)
     8
    39
    410class SettingsTests(unittest.TestCase):
    511
    class SettingsTests(unittest.TestCase):  
    1521
    1622    def test_settings_delete_wrapped(self):
    1723        self.assertRaises(TypeError, delattr, settings, '_wrapped')
     24
     25
     26class MediaURLTests(unittest.TestCase):
     27    settings_module = settings
     28
     29    def setUp(self):
     30        self._original_media_url = self.settings_module.MEDIA_URL
     31
     32    def tearDown(self):
     33        self.settings_module.MEDIA_URL = self._original_media_url
     34
     35    def test_blank(self):
     36        """
     37        If blank, no ImproperlyConfigured error will be raised, even though it
     38        doesn't end in a slash.
     39        """
     40        self.settings_module.MEDIA_URL = ''
     41        self.assertEqual('', self.settings_module.MEDIA_URL)
     42
     43    def test_end_slash(self):
     44        """
     45        MEDIA_URL works if you end in a slash.
     46        """
     47        self.settings_module.MEDIA_URL = '/foo/'
     48        self.assertEqual('/foo/', self.settings_module.MEDIA_URL)
     49
     50        self.settings_module.MEDIA_URL = 'http://media.foo.com/'
     51        self.assertEqual('http://media.foo.com/',
     52                         self.settings_module.MEDIA_URL)
     53
     54    def test_no_end_slash(self):
     55        """
     56        MEDIA_URL raises an ImproperlyConfigured error if it doesn't end in a
     57        slash.
     58        """
     59        self.assertRaises(ImproperlyConfigured, setattr_settings,
     60                          self.settings_module, 'MEDIA_URL', '/foo')
     61
     62        self.assertRaises(ImproperlyConfigured, setattr_settings,
     63                          self.settings_module, 'MEDIA_URL',
     64                          'http://media.foo.com')
     65
     66    def test_double_slash(self):       
     67        """
     68        If a MEDIA_URL ends in more than one slash, presume they know what
     69        they're doing.
     70        """
     71        self.settings_module.MEDIA_URL = '/stupid//'
     72        self.assertEqual('/stupid//', self.settings_module.MEDIA_URL)
     73
     74        self.settings_module.MEDIA_URL = 'http://media.foo.com/stupid//'
     75        self.assertEqual('http://media.foo.com/stupid//',
     76                         self.settings_module.MEDIA_URL)
     77
     78
     79class MediaURLUserSettingsHolderTests(unittest.TestCase):
     80    settings_module = UserSettingsHolder(global_settings)
Back to Top