From b387189d86aa6f0b8dd5e9ff2facb725e6beb7a9 Mon Sep 17 00:00:00 2001 From: Stanislav Karpov Date: Wed, 20 Jul 2016 15:38:07 +0400 Subject: [PATCH] Fixed #26919 -- Added the action form's media in the changelist view. --- django/contrib/admin/options.py | 1 + tests/admin_views/admin.py | 2 ++ tests/admin_views/forms.py | 6 ++++++ tests/admin_views/tests.py | 12 ++++++++++++ 4 files changed, 21 insertions(+) diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py index eaf6452707f..53801a981b4 100644 --- a/django/contrib/admin/options.py +++ b/django/contrib/admin/options.py @@ -1642,6 +1642,7 @@ class ModelAdmin(BaseModelAdmin): if actions: action_form = self.action_form(auto_id=None) action_form.fields['action'].choices = self.get_action_choices(request) + media += action_form.media else: action_form = None diff --git a/tests/admin_views/admin.py b/tests/admin_views/admin.py index 81971c9043d..5a9923848f5 100644 --- a/tests/admin_views/admin.py +++ b/tests/admin_views/admin.py @@ -22,6 +22,7 @@ from django.utils.html import format_html from django.utils.safestring import mark_safe from django.utils.six import StringIO +from .forms import MediaActionForm from .models import ( Actor, AdminOrderedAdminMethod, AdminOrderedCallable, AdminOrderedField, AdminOrderedModelMethod, Album, Answer, Article, BarAccount, Book, @@ -235,6 +236,7 @@ class PersonaAdmin(admin.ModelAdmin): class SubscriberAdmin(admin.ModelAdmin): actions = ['mail_admin'] + action_form = MediaActionForm def mail_admin(self, request, selected): EmailMessage( diff --git a/tests/admin_views/forms.py b/tests/admin_views/forms.py index 06c99f4bc34..cceb543c41a 100644 --- a/tests/admin_views/forms.py +++ b/tests/admin_views/forms.py @@ -1,5 +1,6 @@ from django import forms from django.contrib.admin.forms import AdminAuthenticationForm +from django.contrib.admin.helpers import ActionForm class CustomAdminAuthenticationForm(AdminAuthenticationForm): @@ -12,3 +13,8 @@ class CustomAdminAuthenticationForm(AdminAuthenticationForm): if username == 'customform': raise forms.ValidationError('custom form error') return username + + +class MediaActionForm(ActionForm): + class Media: + js = ['path/to/media.js'] diff --git a/tests/admin_views/tests.py b/tests/admin_views/tests.py index b37535d5c86..42cae7f01d0 100644 --- a/tests/admin_views/tests.py +++ b/tests/admin_views/tests.py @@ -43,6 +43,7 @@ from django.utils.six.moves.urllib.parse import parse_qsl, urljoin, urlparse from . import customadmin from .admin import CityAdmin, site, site2 +from .forms import MediaActionForm from .models import ( Actor, AdminOrderedAdminMethod, AdminOrderedCallable, AdminOrderedField, AdminOrderedModelMethod, Answer, Article, BarAccount, Book, Bookmark, @@ -3374,6 +3375,17 @@ action) self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].subject, 'Greetings from a function action') + def test_media_from_actions_form(self): + """ + The action form's media is included in changelist view's media. + """ + response = self.client.get(reverse('admin:admin_views_subscriber_changelist')) + media_path = MediaActionForm.Media.js[0] + self.assertIsInstance(response.context['action_form'], MediaActionForm) + self.assertIn('media', response.context) + self.assertIn(media_path, response.context['media']._js) + self.assertContains(response, media_path) + def test_user_message_on_none_selected(self): """ User should see a warning when 'Go' is pressed and no items are selected.