Ticket #6218: ticket-6218.2.patch

File ticket-6218.2.patch, 4.9 KB (added by Chris Heisel, 14 years ago)
  • django/conf/__init__.py

    diff --git a/django/conf/__init__.py b/django/conf/__init__.py
    index d94f6e9..82e0c66 100644
    a b class LazySettings(LazyObject):  
    5959        return bool(self._wrapped)
    6060    configured = property(configured)
    6161
    62 class Settings(object):
     62class BaseSettings(object):
     63    """
     64        Common logic for settings wether set by a module or by the user.
     65    """
     66    def __setattr__(self, name, value):
     67        #Protecting people from themselves if they set MEDIA_URL to a value that
     68        #doesn't end in a trailing slash
     69        if name == "MEDIA_URL" and value and value[-1] != '/':
     70            value = "%s/" % value
     71        object.__setattr__(self, name, value)
     72
     73class Settings(BaseSettings):
    6374    def __init__(self, settings_module):
    6475        # update this dict from global settings (but only for ALL_CAPS settings)
    6576        for setting in dir(global_settings):
    class Settings(object):  
    108119            os.environ['TZ'] = self.TIME_ZONE
    109120            time.tzset()
    110121
    111 class UserSettingsHolder(object):
     122class UserSettingsHolder(BaseSettings):
    112123    """
    113124    Holder for user configured settings.
    114125    """
  • django/conf/project_template/settings.py

    diff --git a/django/conf/project_template/settings.py b/django/conf/project_template/settings.py
    index c49df24..b77a99e 100644
    a b MEDIA_ROOT = ''  
    4949
    5050# URL that handles the media served from MEDIA_ROOT. Make sure to use a
    5151# trailing slash if there is a path component (optional in other cases).
    52 # Examples: "http://media.lawrence.com", "http://example.com/media/"
     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..fb38aa9 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"``
     1077Note that a trailing slash will be added if this is set to a non-empty value.
    10811078
    10821079.. setting:: MIDDLEWARE_CLASSES
    10831080
  • tests/regressiontests/settings_tests/tests.py

    diff --git a/tests/regressiontests/settings_tests/tests.py b/tests/regressiontests/settings_tests/tests.py
    index fa217b1..ab6e105 100644
    a b  
    11import unittest
    2 from django.conf import settings
     2from django.conf import settings, UserSettingsHolder, global_settings
    33
    44class SettingsTests(unittest.TestCase):
    55
    class SettingsTests(unittest.TestCase):  
    1515
    1616    def test_settings_delete_wrapped(self):
    1717        self.assertRaises(TypeError, delattr, settings, '_wrapped')
     18
     19    def test_settings_media_url_trailing_slash(self):
     20        """
     21            settings.MEDIA_URL should always end with a slash if it's not blank
     22        """
     23        settings.MEDIA_URL = ''
     24        self.assertEqual('', settings.MEDIA_URL)
     25        del settings.MEDIA_URL
     26
     27        settings.MEDIA_URL = '/foo/'
     28        self.assertEqual('/foo/', settings.MEDIA_URL)
     29        del settings.MEDIA_URL
     30
     31        settings.MEDIA_URL = '/foo'
     32        self.assertEqual('/foo/', settings.MEDIA_URL)
     33        del settings.MEDIA_URL
     34
     35        settings.MEDIA_URL = 'http://media.foo.com'
     36        self.assertEqual('http://media.foo.com/', settings.MEDIA_URL)
     37        del settings.MEDIA_URL
     38
     39        settings.MEDIA_URL = 'http://media.foo.com/'
     40        self.assertEqual('http://media.foo.com/', settings.MEDIA_URL)
     41        del settings.MEDIA_URL
     42
     43        #If someone does something stupid, presume they know what they're doing
     44        settings.MEDIA_URL = 'http://media.foo.com/stupid//'
     45        self.assertEqual('http://media.foo.com/stupid//', settings.MEDIA_URL)
     46
     47        s = UserSettingsHolder(global_settings)
     48        s.MEDIA_URL = ''
     49        self.assertEqual('', s.MEDIA_URL)
     50
     51        s = UserSettingsHolder(global_settings)
     52        s.MEDIA_URL = '/foo/'
     53        self.assertEqual('/foo/', s.MEDIA_URL)
     54
     55        s = UserSettingsHolder(global_settings)
     56        s.MEDIA_URL = '/foo'
     57        self.assertEqual('/foo/', s.MEDIA_URL)
     58
     59        s = UserSettingsHolder(global_settings)
     60        s.MEDIA_URL = 'http://media.foo.com/'
     61        self.assertEqual('http://media.foo.com/', s.MEDIA_URL)
     62
     63        s = UserSettingsHolder(global_settings)
     64        s.MEDIA_URL = 'http://media.foo.com'
     65        self.assertEqual('http://media.foo.com/', s.MEDIA_URL)
     66
     67        s = UserSettingsHolder(global_settings)
     68        s.MEDIA_URL = 'http://media.foo.com/stupid//'
     69        self.assertEqual('http://media.foo.com/stupid//', s.MEDIA_URL)
     70 No newline at end of file
Back to Top