Ticket #17521: ticket17521.diff

File ticket17521.diff, 5.1 KB (added by Wiktor, 13 years ago)

Patch + tests

  • django/contrib/admin/options.py

    diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
    index 3a0ad74..ee4ff97 100644
    a b  
    11from functools import update_wrapper, partial
    22from django import forms
     3from django.conf import settings
    34from django.forms.formsets import all_valid
    45from django.forms.models import (modelform_factory, modelformset_factory,
    56    inlineformset_factory, BaseInlineFormSet)
    class ModelAdmin(BaseModelAdmin):  
    394395
    395396    @property
    396397    def media(self):
     398        extra = "" if settings.DEBUG else ".min"
    397399        js = [
    398400            'core.js',
    399401            'admin/RelatedObjectLookups.js',
    400             'jquery.min.js',
     402            'jquery%s.js' % extra,
    401403            'jquery.init.js'
    402404        ]
    403405        if self.actions is not None:
    404             js.append('actions.min.js')
     406            js.append('actions%s.js' % extra)
    405407        if self.prepopulated_fields:
    406             js.extend(['urlify.js', 'prepopulate.min.js'])
     408            js.extend(['urlify.js', 'prepopulate%s.js' % extra])
    407409        if self.opts.get_ordered_objects():
    408410            js.extend(['getElementsBySelector.js', 'dom-drag.js' , 'admin/ordering.js'])
    409411        return forms.Media(js=[static('admin/js/%s' % url) for url in js])
    class InlineModelAdmin(BaseModelAdmin):  
    13711373
    13721374    @property
    13731375    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]
    13751378        if self.prepopulated_fields:
    1376             js.extend(['urlify.js', 'prepopulate.min.js'])
     1379            js.extend(['urlify.js', 'prepopulate%s.js' % extra])
    13771380        if self.filter_vertical or self.filter_horizontal:
    13781381            js.extend(['SelectBox.js', 'SelectFilter2.js'])
    13791382        return forms.Media(js=[static('admin/js/%s' % url) for url in js])
  • tests/regressiontests/modeladmin/tests.py

    diff --git a/tests/regressiontests/modeladmin/tests.py b/tests/regressiontests/modeladmin/tests.py
    index 799d4f5..6c7394c 100644
    a b from datetime import date  
    55from django import forms
    66from django.conf import settings
    77from django.contrib.admin.options import (ModelAdmin, TabularInline,
    8     HORIZONTAL, VERTICAL)
     8     InlineModelAdmin, HORIZONTAL, VERTICAL)
    99from django.contrib.admin.sites import AdminSite
    1010from django.contrib.admin.validation import validate
    1111from django.contrib.admin.widgets import AdminDateWidget, AdminRadioSelect
    from django.core.exceptions import ImproperlyConfigured  
    1515from django.forms.models import BaseModelFormSet
    1616from django.forms.widgets import Select
    1717from django.test import TestCase
     18from django.test.utils import override_settings
    1819from django.utils import unittest
    1920
    2021from .models import Band, Concert, ValidationTestModel, ValidationTestInlineModel
    class ModelAdminTests(TestCase):  
    493494            list(ma.get_formsets(request))[0]().forms[0].fields.keys(),
    494495            ['extra', 'transport', 'id', 'DELETE', 'main_band'])
    495496
     497    def test_media(self):
     498        ma = ModelAdmin(Band, self.site)
     499        ma.prepopulated_fields = ['something']
     500        ma.actions = ['some action']
     501
     502        with override_settings(DEBUG=False):
     503            media_js = str(ma.media['js'])
     504            self.assertTrue('jquery.min.js' in media_js)
     505            self.assertTrue('prepopulate.min.js' in media_js)
     506            self.assertTrue('actions.min.js' in media_js)
     507        with override_settings(DEBUG=True):
     508            media_js = str(ma.media['js'])
     509            self.assertFalse('jquery.min.js' in media_js)
     510            self.assertTrue('jquery.js' in media_js)
     511            self.assertTrue('prepopulate.js' in media_js)
     512            self.assertFalse('prepopulate.min.js' in media_js)
     513            self.assertTrue('actions.js' in media_js)
     514            self.assertFalse('actions.min.js' in media_js)
     515
     516
     517class InlineModelAdminTest(TestCase):
     518    def setUp(self):
     519        self.site = AdminSite()
     520
     521    def test_media(self):
     522        class InlineBandAdmin(InlineModelAdmin):
     523            model = Band
     524
     525        ma = InlineBandAdmin(Band, self.site)
     526        ma.prepopulated_fields = ['something']
     527        ma.actions = ['some action']
     528
     529        with override_settings(DEBUG=False):
     530            media_js = str(ma.media['js'])
     531            self.assertTrue('jquery.min.js' in media_js)
     532            self.assertTrue('prepopulate.min.js' in media_js)
     533            self.assertTrue('inlines.min.js' in media_js)
     534        with override_settings(DEBUG=True):
     535            media_js = str(ma.media['js'])
     536            self.assertFalse('jquery.min.js' in media_js)
     537            self.assertTrue('jquery.js' in media_js)
     538            self.assertTrue('prepopulate.js' in media_js)
     539            self.assertFalse('prepopulate.min.js' in media_js)
     540            self.assertTrue('inlines.js' in media_js)
     541            self.assertFalse('inlines.min.js' in media_js)
     542
    496543
    497544class ValidationTests(unittest.TestCase):
    498545    def test_validation_only_runs_in_debug(self):
Back to Top