Ticket #6218: t6218b.patch

File t6218b.patch, 7.2 KB (added by Chris Heisel, 14 years ago)

Smiley's patch, but updated to use PendingDeprecationWarning

  • django/conf/__init__.py

    diff --git a/django/conf/__init__.py b/django/conf/__init__.py
    index d94f6e9..e81a3fb 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            import warnings
     71            warnings.warn('If set, MEDIA_URL must end in a slash',
     72                          category=PendingDeprecationWarning)
     73        object.__setattr__(self, name, value)
     74
     75
     76class Settings(BaseSettings):
    6377    def __init__(self, settings_module):
    6478        # update this dict from global settings (but only for ALL_CAPS settings)
    6579        for setting in dir(global_settings):
    class Settings(object):  
    108122            os.environ['TZ'] = self.TIME_ZONE
    109123            time.tzset()
    110124
    111 class UserSettingsHolder(object):
     125
     126class UserSettingsHolder(BaseSettings):
    112127    """
    113128    Holder for user configured settings.
    114129    """
    class UserSettingsHolder(object):  
    132147    # For Python < 2.6:
    133148    __members__ = property(lambda self: self.__dir__())
    134149
    135 settings = LazySettings()
    136150
     151settings = 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..bbbacfc 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 ``PendingDeprecation`` warning.
     99
     100In Django 1.4 this same condition will raise an ``ImproperlyConfigured`` exception.
     101
    84102What's new in Django 1.3
    85103========================
    86104
  • tests/regressiontests/settings_tests/tests.py

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