Fixed #17521 -- Made the minified JS files be used in the admin only when `DEBUG` is `False`. Thanks to Travis Swicegood for the suggestion and patch, and to viciu for the tests.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17454 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Julien Phalip 2012-02-05 08:35:08 +00:00
parent 3732c82f59
commit a90aad3511
2 changed files with 68 additions and 7 deletions

View File

@ -1,5 +1,6 @@
from functools import update_wrapper, partial
from django import forms
from django.conf import settings
from django.forms.formsets import all_valid
from django.forms.models import (modelform_factory, modelformset_factory,
inlineformset_factory, BaseInlineFormSet)
@ -394,16 +395,17 @@ class ModelAdmin(BaseModelAdmin):
@property
def media(self):
extra = "" if settings.DEBUG else ".min"
js = [
'core.js',
'admin/RelatedObjectLookups.js',
'jquery.min.js',
'jquery%s.js' % extra,
'jquery.init.js'
]
if self.actions is not None:
js.append('actions.min.js')
js.append('actions%s.js' % extra)
if self.prepopulated_fields:
js.extend(['urlify.js', 'prepopulate.min.js'])
js.extend(['urlify.js', 'prepopulate%s.js' % extra])
if self.opts.get_ordered_objects():
js.extend(['getElementsBySelector.js', 'dom-drag.js' , 'admin/ordering.js'])
return forms.Media(js=[static('admin/js/%s' % url) for url in js])
@ -1371,9 +1373,10 @@ class InlineModelAdmin(BaseModelAdmin):
@property
def media(self):
js = ['jquery.min.js', 'jquery.init.js', 'inlines.min.js']
extra = "" if settings.DEBUG else ".min"
js = ['jquery%s.js' % extra, 'jquery.init.js', "inlines%s.js" % extra]
if self.prepopulated_fields:
js.extend(['urlify.js', 'prepopulate.min.js'])
js.extend(['urlify.js', 'prepopulate%s.js' % extra])
if self.filter_vertical or self.filter_horizontal:
js.extend(['SelectBox.js', 'SelectFilter2.js'])
return forms.Media(js=[static('admin/js/%s' % url) for url in js])

View File

@ -1,11 +1,11 @@
from __future__ import absolute_import
from __future__ import absolute_import, with_statement
from datetime import date
from django import forms
from django.conf import settings
from django.contrib.admin.options import (ModelAdmin, TabularInline,
HORIZONTAL, VERTICAL)
InlineModelAdmin, HORIZONTAL, VERTICAL)
from django.contrib.admin.sites import AdminSite
from django.contrib.admin.validation import validate
from django.contrib.admin.widgets import AdminDateWidget, AdminRadioSelect
@ -15,6 +15,7 @@ from django.core.exceptions import ImproperlyConfigured
from django.forms.models import BaseModelFormSet
from django.forms.widgets import Select
from django.test import TestCase
from django.test.utils import override_settings
from django.utils import unittest
from .models import Band, Concert, ValidationTestModel, ValidationTestInlineModel
@ -493,6 +494,63 @@ class ModelAdminTests(TestCase):
list(ma.get_formsets(request))[0]().forms[0].fields.keys(),
['extra', 'transport', 'id', 'DELETE', 'main_band'])
def test_media_minified_only_if_debug_is_false(self):
"""
Ensure that, with ModelAdmin, the minified versions of the JS files are
only used when DEBUG is False.
Refs #17521.
"""
ma = ModelAdmin(Band, self.site)
ma.prepopulated_fields = ['something']
ma.actions = ['some action']
with override_settings(DEBUG=False):
media_js = str(ma.media['js'])
self.assertFalse('jquery.js' in media_js)
self.assertTrue('jquery.min.js' in media_js)
self.assertFalse('prepopulate.js' in media_js)
self.assertTrue('prepopulate.min.js' in media_js)
self.assertFalse('actions.js' in media_js)
self.assertTrue('actions.min.js' in media_js)
with override_settings(DEBUG=True):
media_js = str(ma.media['js'])
self.assertTrue('jquery.js' in media_js)
self.assertFalse('jquery.min.js' in media_js)
self.assertTrue('prepopulate.js' in media_js)
self.assertFalse('prepopulate.min.js' in media_js)
self.assertTrue('actions.js' in media_js)
self.assertFalse('actions.min.js' in media_js)
def test_inlines_media_minified_only_if_debug_is_false(self):
"""
Ensure that, with InlineModelAdmin, the minified versions of the JS
files are only used when DEBUG is False.
Refs #17521.
"""
class InlineBandAdmin(InlineModelAdmin):
model = Band
ma = InlineBandAdmin(Band, self.site)
ma.prepopulated_fields = ['something']
ma.actions = ['some action']
with override_settings(DEBUG=False):
media_js = str(ma.media['js'])
self.assertFalse('jquery.js' in media_js)
self.assertTrue('jquery.min.js' in media_js)
self.assertFalse('prepopulate.js' in media_js)
self.assertTrue('prepopulate.min.js' in media_js)
self.assertFalse('inlines.js' in media_js)
self.assertTrue('inlines.min.js' in media_js)
with override_settings(DEBUG=True):
media_js = str(ma.media['js'])
self.assertTrue('jquery.js' in media_js)
self.assertFalse('jquery.min.js' in media_js)
self.assertTrue('prepopulate.js' in media_js)
self.assertFalse('prepopulate.min.js' in media_js)
self.assertTrue('inlines.js' in media_js)
self.assertFalse('inlines.min.js' in media_js)
class ValidationTests(unittest.TestCase):
def test_validation_only_runs_in_debug(self):