Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit a90aad35116d1a796a301626db5abff5f361da8a 1 parent 3732c82
Julien Phalip authored
13  django/contrib/admin/options.py
... ...
@@ -1,5 +1,6 @@
1 1
 from functools import update_wrapper, partial
2 2
 from django import forms
  3
+from django.conf import settings
3 4
 from django.forms.formsets import all_valid
4 5
 from django.forms.models import (modelform_factory, modelformset_factory,
5 6
     inlineformset_factory, BaseInlineFormSet)
@@ -394,16 +395,17 @@ def urls(self):
394 395
 
395 396
     @property
396 397
     def media(self):
  398
+        extra = "" if settings.DEBUG else ".min"
397 399
         js = [
398 400
             'core.js',
399 401
             'admin/RelatedObjectLookups.js',
400  
-            'jquery.min.js',
  402
+            'jquery%s.js' % extra,
401 403
             'jquery.init.js'
402 404
         ]
403 405
         if self.actions is not None:
404  
-            js.append('actions.min.js')
  406
+            js.append('actions%s.js' % extra)
405 407
         if self.prepopulated_fields:
406  
-            js.extend(['urlify.js', 'prepopulate.min.js'])
  408
+            js.extend(['urlify.js', 'prepopulate%s.js' % extra])
407 409
         if self.opts.get_ordered_objects():
408 410
             js.extend(['getElementsBySelector.js', 'dom-drag.js' , 'admin/ordering.js'])
409 411
         return forms.Media(js=[static('admin/js/%s' % url) for url in js])
@@ -1371,9 +1373,10 @@ def __init__(self, parent_model, admin_site):
1371 1373
 
1372 1374
     @property
1373 1375
     def media(self):
1374  
-        js = ['jquery.min.js', 'jquery.init.js', 'inlines.min.js']
  1376
+        extra = "" if settings.DEBUG else ".min"
  1377
+        js = ['jquery%s.js' % extra, 'jquery.init.js', "inlines%s.js" % extra]
1375 1378
         if self.prepopulated_fields:
1376  
-            js.extend(['urlify.js', 'prepopulate.min.js'])
  1379
+            js.extend(['urlify.js', 'prepopulate%s.js' % extra])
1377 1380
         if self.filter_vertical or self.filter_horizontal:
1378 1381
             js.extend(['SelectBox.js', 'SelectFilter2.js'])
1379 1382
         return forms.Media(js=[static('admin/js/%s' % url) for url in js])
62  tests/regressiontests/modeladmin/tests.py
... ...
@@ -1,11 +1,11 @@
1  
-from __future__ import absolute_import
  1
+from __future__ import absolute_import, with_statement
2 2
 
3 3
 from datetime import date
4 4
 
5 5
 from django import forms
6 6
 from django.conf import settings
7 7
 from django.contrib.admin.options import (ModelAdmin, TabularInline,
8  
-    HORIZONTAL, VERTICAL)
  8
+     InlineModelAdmin, HORIZONTAL, VERTICAL)
9 9
 from django.contrib.admin.sites import AdminSite
10 10
 from django.contrib.admin.validation import validate
11 11
 from django.contrib.admin.widgets import AdminDateWidget, AdminRadioSelect
@@ -15,6 +15,7 @@
15 15
 from django.forms.models import BaseModelFormSet
16 16
 from django.forms.widgets import Select
17 17
 from django.test import TestCase
  18
+from django.test.utils import override_settings
18 19
 from django.utils import unittest
19 20
 
20 21
 from .models import Band, Concert, ValidationTestModel, ValidationTestInlineModel
@@ -493,6 +494,63 @@ class BandAdmin(ModelAdmin):
493 494
             list(ma.get_formsets(request))[0]().forms[0].fields.keys(),
494 495
             ['extra', 'transport', 'id', 'DELETE', 'main_band'])
495 496
 
  497
+    def test_media_minified_only_if_debug_is_false(self):
  498
+        """
  499
+        Ensure that, with ModelAdmin, the minified versions of the JS files are
  500
+        only used when DEBUG is False.
  501
+        Refs #17521.
  502
+        """
  503
+        ma = ModelAdmin(Band, self.site)
  504
+        ma.prepopulated_fields = ['something']
  505
+        ma.actions = ['some action']
  506
+
  507
+        with override_settings(DEBUG=False):
  508
+            media_js = str(ma.media['js'])
  509
+            self.assertFalse('jquery.js' in media_js)
  510
+            self.assertTrue('jquery.min.js' in media_js)
  511
+            self.assertFalse('prepopulate.js' in media_js)
  512
+            self.assertTrue('prepopulate.min.js' in media_js)
  513
+            self.assertFalse('actions.js' in media_js)
  514
+            self.assertTrue('actions.min.js' in media_js)
  515
+        with override_settings(DEBUG=True):
  516
+            media_js = str(ma.media['js'])
  517
+            self.assertTrue('jquery.js' in media_js)
  518
+            self.assertFalse('jquery.min.js' in media_js)
  519
+            self.assertTrue('prepopulate.js' in media_js)
  520
+            self.assertFalse('prepopulate.min.js' in media_js)
  521
+            self.assertTrue('actions.js' in media_js)
  522
+            self.assertFalse('actions.min.js' in media_js)
  523
+
  524
+    def test_inlines_media_minified_only_if_debug_is_false(self):
  525
+        """
  526
+        Ensure that, with InlineModelAdmin, the minified versions of the JS
  527
+        files are only used when DEBUG is False.
  528
+        Refs #17521.
  529
+        """
  530
+        class InlineBandAdmin(InlineModelAdmin):
  531
+            model = Band
  532
+
  533
+        ma = InlineBandAdmin(Band, self.site)
  534
+        ma.prepopulated_fields = ['something']
  535
+        ma.actions = ['some action']
  536
+
  537
+        with override_settings(DEBUG=False):
  538
+            media_js = str(ma.media['js'])
  539
+            self.assertFalse('jquery.js' in media_js)
  540
+            self.assertTrue('jquery.min.js' in media_js)
  541
+            self.assertFalse('prepopulate.js' in media_js)
  542
+            self.assertTrue('prepopulate.min.js' in media_js)
  543
+            self.assertFalse('inlines.js' in media_js)
  544
+            self.assertTrue('inlines.min.js' in media_js)
  545
+        with override_settings(DEBUG=True):
  546
+            media_js = str(ma.media['js'])
  547
+            self.assertTrue('jquery.js' in media_js)
  548
+            self.assertFalse('jquery.min.js' in media_js)
  549
+            self.assertTrue('prepopulate.js' in media_js)
  550
+            self.assertFalse('prepopulate.min.js' in media_js)
  551
+            self.assertTrue('inlines.js' in media_js)
  552
+            self.assertFalse('inlines.min.js' in media_js)
  553
+
496 554
 
497 555
 class ValidationTests(unittest.TestCase):
498 556
     def test_validation_only_runs_in_debug(self):

0 notes on commit a90aad3

Please sign in to comment.
Something went wrong with that request. Please try again.