From 1c4868f4c15f41c7840129d0f04c87f65aa94367 Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Thu, 21 Oct 2010 03:16:41 +0000 Subject: [PATCH] Fixed #14507 -- Corrected the logic of the URL helpers and view of staticfiles to actual work like documented (only when settings.DEBUG is True). Thanks for the report and initial patch, mbi. git-svn-id: http://code.djangoproject.com/svn/django/trunk@14306 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/staticfiles/urls.py | 4 ++-- django/contrib/staticfiles/views.py | 2 +- .../staticfiles_tests/tests.py | 22 +++++++++++++++++-- tests/regressiontests/views/tests/static.py | 8 +++++++ tests/urls.py | 3 --- 5 files changed, 31 insertions(+), 8 deletions(-) diff --git a/django/contrib/staticfiles/urls.py b/django/contrib/staticfiles/urls.py index 131b10266b..efe459b879 100644 --- a/django/contrib/staticfiles/urls.py +++ b/django/contrib/staticfiles/urls.py @@ -6,7 +6,7 @@ from django.core.exceptions import ImproperlyConfigured urlpatterns = [] # only serve non-fqdn URLs -if not settings.DEBUG: +if settings.DEBUG: urlpatterns += patterns('', url(r'^(?P.*)$', 'django.contrib.staticfiles.views.serve'), ) @@ -15,7 +15,7 @@ def staticfiles_urlpatterns(prefix=None): """ Helper function to return a URL pattern for serving static files. """ - if settings.DEBUG: + if not settings.DEBUG: return [] if prefix is None: prefix = settings.STATICFILES_URL diff --git a/django/contrib/staticfiles/views.py b/django/contrib/staticfiles/views.py index 27d499df87..b2c47d64f1 100644 --- a/django/contrib/staticfiles/views.py +++ b/django/contrib/staticfiles/views.py @@ -41,7 +41,7 @@ def serve(request, path, document_root=None, show_indexes=False): template hardcoded below, but if you'd like to override it, you can create a template called ``static/directory_index.html``. """ - if settings.DEBUG: + if not settings.DEBUG: raise ImproperlyConfigured("The view to serve static files can only " "be used if the DEBUG setting is True") if not document_root: diff --git a/tests/regressiontests/staticfiles_tests/tests.py b/tests/regressiontests/staticfiles_tests/tests.py index 2f39354627..83e41b7e14 100644 --- a/tests/regressiontests/staticfiles_tests/tests.py +++ b/tests/regressiontests/staticfiles_tests/tests.py @@ -29,11 +29,13 @@ class StaticFilesTestCase(TestCase): self.old_media_root = settings.MEDIA_ROOT self.old_media_url = settings.MEDIA_URL self.old_admin_media_prefix = settings.ADMIN_MEDIA_PREFIX + self.old_debug = settings.DEBUG # We have to load these apps to test staticfiles. load_app('regressiontests.staticfiles_tests.apps.test') load_app('regressiontests.staticfiles_tests.apps.no_label') site_media = os.path.join(TEST_ROOT, 'project', 'site_media') + settings.DEBUG = True settings.MEDIA_ROOT = os.path.join(site_media, 'media') settings.MEDIA_URL = '/media/' settings.STATICFILES_ROOT = os.path.join(site_media, 'static') @@ -49,6 +51,7 @@ class StaticFilesTestCase(TestCase): ) def tearDown(self): + settings.DEBUG = self.old_debug settings.MEDIA_ROOT = self.old_media_root settings.MEDIA_URL = self.old_media_url settings.ADMIN_MEDIA_PREFIX = self.old_admin_media_prefix @@ -208,6 +211,8 @@ class TestServeStatic(StaticFilesTestCase): """ Test static asset serving view. """ + urls = "regressiontests.staticfiles_tests.urls.default" + def _response(self, filepath): return self.client.get( posixpath.join(settings.STATICFILES_URL, filepath)) @@ -219,12 +224,25 @@ class TestServeStatic(StaticFilesTestCase): self.assertEquals(self._response(filepath).status_code, 404) +class TestServeDisabled(TestServeStatic): + """ + Test serving media from django.contrib.admin. + """ + def setUp(self): + super(TestServeDisabled, self).setUp() + settings.DEBUG = False + + def test_disabled_serving(self): + self.assertRaisesRegexp(ImproperlyConfigured, "The view to serve " + "static files can only be used if the DEBUG setting is True", + self._response, 'test.txt') + + class TestServeStaticWithDefaultURL(TestServeStatic, TestDefaults): """ Test static asset serving view with staticfiles_urlpatterns helper. """ - urls = "regressiontests.staticfiles_tests.urls.default" - + pass class TestServeStaticWithURLHelper(TestServeStatic, TestDefaults): """ diff --git a/tests/regressiontests/views/tests/static.py b/tests/regressiontests/views/tests/static.py index 25153e86b0..d1aa029bc5 100644 --- a/tests/regressiontests/views/tests/static.py +++ b/tests/regressiontests/views/tests/static.py @@ -1,6 +1,7 @@ import mimetypes from os import path +from django.conf import settings from django.test import TestCase from django.http import HttpResponseNotModified from regressiontests.views.urls import media_dir @@ -8,6 +9,13 @@ from regressiontests.views.urls import media_dir class StaticTests(TestCase): """Tests django views in django/views/static.py""" + def setUp(self): + self.old_debug = settings.DEBUG + settings.DEBUG = True + + def tearDown(self): + settings.DEBUG = self.old_debug + def test_serve(self): "The static view can serve static media" media_files = ['file.txt', 'file.txt.gz'] diff --git a/tests/urls.py b/tests/urls.py index 573a0aafb7..01d6408c5a 100644 --- a/tests/urls.py +++ b/tests/urls.py @@ -41,7 +41,4 @@ urlpatterns = patterns('', # special headers views (r'special_headers/', include('regressiontests.special_headers.urls')), - - # static files handling - (r'^', include('regressiontests.staticfiles_tests.urls.default')), )