From 0f6cf4aba1d3db4be9a0d593becfc10bc4aa1fbe Mon Sep 17 00:00:00 2001 From: Julien Phalip Date: Sun, 5 Feb 2012 10:26:05 +0000 Subject: [PATCH] Improved on r17454 to ensure that `collapse.js` only gets minified when `DEBUG` is `False`. git-svn-id: http://code.djangoproject.com/svn/django/trunk@17455 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/admin/helpers.py | 6 ++- django/contrib/admin/options.py | 6 +-- tests/regressiontests/admin_views/admin.py | 15 +++++- tests/regressiontests/admin_views/models.py | 1 - tests/regressiontests/admin_views/tests.py | 35 +++++++++++++ tests/regressiontests/modeladmin/tests.py | 57 --------------------- 6 files changed, 57 insertions(+), 63 deletions(-) diff --git a/django/contrib/admin/helpers.py b/django/contrib/admin/helpers.py index b7c65e175f..b08d1c84a5 100644 --- a/django/contrib/admin/helpers.py +++ b/django/contrib/admin/helpers.py @@ -11,6 +11,7 @@ from django.utils.encoding import force_unicode, smart_unicode from django.utils.html import escape, conditional_escape from django.utils.safestring import mark_safe from django.utils.translation import ugettext_lazy as _ +from django.conf import settings ACTION_CHECKBOX_NAME = '_selected_action' @@ -75,7 +76,10 @@ class Fieldset(object): def _media(self): if 'collapse' in self.classes: - js = ['jquery.min.js', 'jquery.init.js', 'collapse.min.js'] + extra = '' if settings.DEBUG else '.min' + js = ['jquery%s.js' % extra, + 'jquery.init.js', + 'collapse%s.js' % extra] return forms.Media(js=[static('admin/js/%s' % url) for url in js]) return forms.Media() media = property(_media) diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py index ee4ff976a3..5bfa891a69 100644 --- a/django/contrib/admin/options.py +++ b/django/contrib/admin/options.py @@ -395,7 +395,7 @@ class ModelAdmin(BaseModelAdmin): @property def media(self): - extra = "" if settings.DEBUG else ".min" + extra = '' if settings.DEBUG else '.min' js = [ 'core.js', 'admin/RelatedObjectLookups.js', @@ -1373,8 +1373,8 @@ class InlineModelAdmin(BaseModelAdmin): @property def media(self): - extra = "" if settings.DEBUG else ".min" - js = ['jquery%s.js' % extra, 'jquery.init.js', "inlines%s.js" % extra] + 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%s.js' % extra]) if self.filter_vertical or self.filter_horizontal: diff --git a/tests/regressiontests/admin_views/admin.py b/tests/regressiontests/admin_views/admin.py index 8c5b511d1e..e541a4265e 100644 --- a/tests/regressiontests/admin_views/admin.py +++ b/tests/regressiontests/admin_views/admin.py @@ -35,7 +35,19 @@ callable_year.admin_order_field = 'date' class ArticleInline(admin.TabularInline): model = Article - + prepopulated_fields = { + 'title' : ('content',) + } + fieldsets=( + ('Some fields', { + 'classes': ('collapse',), + 'fields': ('title', 'content') + }), + ('Some other fields', { + 'classes': ('wide',), + 'fields': ('date', 'section') + }) + ) class ChapterInline(admin.TabularInline): model = Chapter @@ -512,6 +524,7 @@ class ReportAdmin(admin.ModelAdmin): name='cable_extra'), ) + site = admin.AdminSite(name="admin") site.register(Article, ArticleAdmin) site.register(CustomArticle, CustomArticleAdmin) diff --git a/tests/regressiontests/admin_views/models.py b/tests/regressiontests/admin_views/models.py index 0029fcc152..b3426b4ec3 100644 --- a/tests/regressiontests/admin_views/models.py +++ b/tests/regressiontests/admin_views/models.py @@ -3,7 +3,6 @@ import datetime import tempfile import os -from django import forms from django.contrib.auth.models import User from django.contrib.contenttypes import generic from django.contrib.contenttypes.models import ContentType diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py index b0120cef1d..ab40c698de 100644 --- a/tests/regressiontests/admin_views/tests.py +++ b/tests/regressiontests/admin_views/tests.py @@ -573,6 +573,7 @@ class AdminViewBasicTest(TestCase): except SuspiciousOperation: self.fail("Filters should be allowed if they are defined on a ForeignKey pointing to this model") + class AdminJavaScriptTest(AdminViewBasicTest): urls = "regressiontests.admin_views.urls" @@ -598,6 +599,40 @@ class AdminJavaScriptTest(AdminViewBasicTest): ) + def test_js_minified_only_if_debug_is_false(self): + """ + Ensure that the minified versions of the JS files are only used when + DEBUG is False. + Refs #17521. + """ + with override_settings(DEBUG=False): + response = self.client.get( + '/test_admin/%s/admin_views/section/add/' % self.urlbit) + self.assertNotContains(response, 'jquery.js') + self.assertContains(response, 'jquery.min.js') + self.assertNotContains(response, 'prepopulate.js') + self.assertContains(response, 'prepopulate.min.js') + self.assertNotContains(response, 'actions.js') + self.assertContains(response, 'actions.min.js') + self.assertNotContains(response, 'collapse.js') + self.assertContains(response, 'collapse.min.js') + self.assertNotContains(response, 'inlines.js') + self.assertContains(response, 'inlines.min.js') + with override_settings(DEBUG=True): + response = self.client.get( + '/test_admin/%s/admin_views/section/add/' % self.urlbit) + self.assertContains(response, 'jquery.js') + self.assertNotContains(response, 'jquery.min.js') + self.assertContains(response, 'prepopulate.js') + self.assertNotContains(response, 'prepopulate.min.js') + self.assertContains(response, 'actions.js') + self.assertNotContains(response, 'actions.min.js') + self.assertContains(response, 'collapse.js') + self.assertNotContains(response, 'collapse.min.js') + self.assertContains(response, 'inlines.js') + self.assertNotContains(response, 'inlines.min.js') + + class SaveAsTests(TestCase): urls = "regressiontests.admin_views.urls" fixtures = ['admin-views-users.xml','admin-views-person.xml'] diff --git a/tests/regressiontests/modeladmin/tests.py b/tests/regressiontests/modeladmin/tests.py index b5da138c62..f9dec69ae8 100644 --- a/tests/regressiontests/modeladmin/tests.py +++ b/tests/regressiontests/modeladmin/tests.py @@ -494,63 +494,6 @@ 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):