Ticket #6218: ticket-6218.patch

File ticket-6218.patch, 2.1 KB (added by Chris Heisel, 14 years ago)

Ensures MEDIA_URL always ends with a / when set to a non-empty string

  • django/conf/__init__.py

    diff --git a/django/conf/__init__.py b/django/conf/__init__.py
    index d94f6e9..bde2101 100644
    a b class Settings(object):  
    108108            os.environ['TZ'] = self.TIME_ZONE
    109109            time.tzset()
    110110
     111    def __setattr__(self, name, value):
     112        #Protecting people from themselves if they set MEDIA_URL to a value that
     113        #doesn't end in a trailing slash
     114        if name == "MEDIA_URL" and value and value[-1] != '/':
     115            value = "%s/" % value
     116        object.__setattr__(self, name, value)
     117
    111118class UserSettingsHolder(object):
    112119    """
    113120    Holder for user configured settings.
  • tests/regressiontests/settings_tests/tests.py

    diff --git a/tests/regressiontests/settings_tests/tests.py b/tests/regressiontests/settings_tests/tests.py
    index fa217b1..751db8f 100644
    a b 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 No newline at end of file
Back to Top