Ticket #15997: max_show_all_in_model_admin.v2.diff
File max_show_all_in_model_admin.v2.diff, 14.7 KB (added by , 13 years ago) |
---|
-
django/contrib/admin/options.py
diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py index cf7ea83..c9f2562 100644
a b class ModelAdmin(BaseModelAdmin): 278 278 list_filter = () 279 279 list_select_related = False 280 280 list_per_page = 100 281 list_max_show_all = 200 281 282 list_editable = () 282 283 search_fields = () 283 284 date_hierarchy = None … … class ModelAdmin(BaseModelAdmin): 1087 1088 try: 1088 1089 cl = ChangeList(request, self.model, list_display, self.list_display_links, 1089 1090 self.list_filter, self.date_hierarchy, self.search_fields, 1090 self.list_select_related, self.list_per_page, self.list_ editable, self)1091 self.list_select_related, self.list_per_page, self.list_max_show_all, self.list_editable, self) 1091 1092 except IncorrectLookupParameters: 1092 1093 # Wacky lookup parameters were given, so redirect to the main 1093 1094 # changelist page, without parameters, and pass an 'invalid=1' -
django/contrib/admin/validation.py
diff --git a/django/contrib/admin/validation.py b/django/contrib/admin/validation.py index 9a0c948..53049ae 100644
a b def validate(cls, model): 95 95 if hasattr(cls, 'list_per_page') and not isinstance(cls.list_per_page, int): 96 96 raise ImproperlyConfigured("'%s.list_per_page' should be a integer." 97 97 % cls.__name__) 98 99 # list_max_show_all 100 if hasattr(cls, 'list_max_show_all') and not isinstance(cls.list_max_show_all, int): 101 raise ImproperlyConfigured("'%s.list_max_show_all' should be an integer." 102 % cls.__name__) 98 103 99 104 # list_editable 100 105 if hasattr(cls, 'list_editable') and cls.list_editable: -
django/contrib/admin/views/main.py
diff --git a/django/contrib/admin/views/main.py b/django/contrib/admin/views/main.py index 090b238..a076d74 100644
a b from django.contrib.admin import FieldListFilter 12 12 from django.contrib.admin.options import IncorrectLookupParameters 13 13 from django.contrib.admin.util import quote, get_fields_from_path 14 14 15 # The system will display a "Show all" link on the change list only if the16 # total result count is less than or equal to this setting.17 MAX_SHOW_ALL_ALLOWED = 20018 19 15 # Changelist settings 20 16 ALL_VAR = 'all' 21 17 ORDER_VAR = 'o' … … def field_needs_distinct(field): 44 40 class ChangeList(object): 45 41 def __init__(self, request, model, list_display, list_display_links, 46 42 list_filter, date_hierarchy, search_fields, list_select_related, 47 list_per_page, list_ editable, model_admin):43 list_per_page, list_max_show_all, list_editable, model_admin): 48 44 self.model = model 49 45 self.opts = model._meta 50 46 self.lookup_opts = self.opts … … class ChangeList(object): 56 52 self.search_fields = search_fields 57 53 self.list_select_related = list_select_related 58 54 self.list_per_page = list_per_page 55 self.list_max_show_all = list_max_show_all 59 56 self.model_admin = model_admin 60 57 61 58 # Get search parameters from the query string. … … class ChangeList(object): 145 142 else: 146 143 full_result_count = self.root_query_set.count() 147 144 148 can_show_all = result_count <= MAX_SHOW_ALL_ALLOWED145 can_show_all = result_count <= self.list_max_show_all 149 146 multi_page = result_count > self.list_per_page 150 147 151 148 # Get the list of objects to display on this page. -
docs/ref/contrib/admin/index.txt
diff --git a/docs/ref/contrib/admin/index.txt b/docs/ref/contrib/admin/index.txt index 8ba41aa..1b4c821 100644
a b subclass:: 695 695 The ``FieldListFilter`` API is currently considered internal 696 696 and prone to refactoring. 697 697 698 .. attribute:: ModelAdmin.list_max_show_all 699 700 .. versionadded:: 1.4 701 702 Set ``list_max_show_all`` to control how many items can appear on a "Show 703 all" admin change list page. The admin will display a "Show all" link on the 704 change list only if the total result count is less than or equal to this 705 setting. By default, this is set to ``200``. 706 698 707 .. attribute:: ModelAdmin.list_per_page 699 708 700 709 Set ``list_per_page`` to control how many items appear on each paginated -
tests/regressiontests/admin_changelist/tests.py
diff --git a/tests/regressiontests/admin_changelist/tests.py b/tests/regressiontests/admin_changelist/tests.py index 709fa76..4f71002 100644
a b 1 1 from django.contrib import admin 2 2 from django.contrib.admin.options import IncorrectLookupParameters 3 from django.contrib.admin.views.main import ChangeList, SEARCH_VAR 3 from django.contrib.admin.views.main import ChangeList, SEARCH_VAR, ALL_VAR 4 4 from django.core.paginator import Paginator 5 5 from django.template import Context, Template 6 6 from django.test import TestCase … … class ChangeListTests(TestCase): 24 24 request = self.factory.get('/child/') 25 25 cl = ChangeList(request, Child, m.list_display, m.list_display_links, 26 26 m.list_filter, m.date_hierarchy, m.search_fields, 27 m.list_select_related, m.list_per_page, m.list_ editable, m)27 m.list_select_related, m.list_per_page, m.list_max_show_all, m.list_editable, m) 28 28 self.assertEqual(cl.query_set.query.select_related, {'parent': {'name': {}}}) 29 29 30 30 def test_result_list_empty_changelist_value(self): … … class ChangeListTests(TestCase): 37 37 m = ChildAdmin(Child, admin.site) 38 38 cl = ChangeList(request, Child, m.list_display, m.list_display_links, 39 39 m.list_filter, m.date_hierarchy, m.search_fields, 40 m.list_select_related, m.list_per_page, m.list_ editable, m)40 m.list_select_related, m.list_per_page, m.list_max_show_all, m.list_editable, m) 41 41 cl.formset = None 42 42 template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}') 43 43 context = Context({'cl': cl}) … … class ChangeListTests(TestCase): 57 57 m = ChildAdmin(Child, admin.site) 58 58 cl = ChangeList(request, Child, m.list_display, m.list_display_links, 59 59 m.list_filter, m.date_hierarchy, m.search_fields, 60 m.list_select_related, m.list_per_page, m.list_ editable, m)60 m.list_select_related, m.list_per_page, m.list_max_show_all, m.list_editable, m) 61 61 cl.formset = None 62 62 template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}') 63 63 context = Context({'cl': cl}) … … class ChangeListTests(TestCase): 86 86 m.list_editable = ['name'] 87 87 cl = ChangeList(request, Child, m.list_display, m.list_display_links, 88 88 m.list_filter, m.date_hierarchy, m.search_fields, 89 m.list_select_related, m.list_per_page, m.list_ editable, m)89 m.list_select_related, m.list_per_page, m.list_max_show_all, m.list_editable, m) 90 90 FormSet = m.get_changelist_formset(request) 91 91 cl.formset = FormSet(queryset=cl.result_list) 92 92 template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}') … … class ChangeListTests(TestCase): 119 119 self.assertRaises(IncorrectLookupParameters, lambda: \ 120 120 ChangeList(request, Child, m.list_display, m.list_display_links, 121 121 m.list_filter, m.date_hierarchy, m.search_fields, 122 m.list_select_related, m.list_per_page, m.list_ editable, m))122 m.list_select_related, m.list_per_page, m.list_max_show_all, m.list_editable, m)) 123 123 124 124 def test_custom_paginator(self): 125 125 new_parent = Parent.objects.create(name='parent') … … class ChangeListTests(TestCase): 135 135 136 136 cl = ChangeList(request, Child, m.list_display, m.list_display_links, 137 137 m.list_filter, m.date_hierarchy, m.search_fields, 138 m.list_select_related, m.list_per_page, m.list_ editable, m)138 m.list_select_related, m.list_per_page, m.list_max_show_all, m.list_editable, m) 139 139 140 140 cl.get_results(request) 141 141 self.assertIsInstance(cl.paginator, CustomPaginator) … … class ChangeListTests(TestCase): 157 157 cl = ChangeList(request, Band, m.list_display, 158 158 m.list_display_links, m.list_filter, m.date_hierarchy, 159 159 m.search_fields, m.list_select_related, m.list_per_page, 160 m.list_ editable, m)160 m.list_max_show_all, m.list_editable, m) 161 161 162 162 cl.get_results(request) 163 163 … … class ChangeListTests(TestCase): 180 180 cl = ChangeList(request, Group, m.list_display, 181 181 m.list_display_links, m.list_filter, m.date_hierarchy, 182 182 m.search_fields, m.list_select_related, m.list_per_page, 183 m.list_ editable, m)183 m.list_max_show_all, m.list_editable, m) 184 184 185 185 cl.get_results(request) 186 186 … … class ChangeListTests(TestCase): 204 204 cl = ChangeList(request, Quartet, m.list_display, 205 205 m.list_display_links, m.list_filter, m.date_hierarchy, 206 206 m.search_fields, m.list_select_related, m.list_per_page, 207 m.list_ editable, m)207 m.list_max_show_all, m.list_editable, m) 208 208 209 209 cl.get_results(request) 210 210 … … class ChangeListTests(TestCase): 228 228 cl = ChangeList(request, ChordsBand, m.list_display, 229 229 m.list_display_links, m.list_filter, m.date_hierarchy, 230 230 m.search_fields, m.list_select_related, m.list_per_page, 231 m.list_ editable, m)231 m.list_max_show_all, m.list_editable, m) 232 232 233 233 cl.get_results(request) 234 234 … … class ChangeListTests(TestCase): 251 251 cl = ChangeList(request, Parent, m.list_display, m.list_display_links, 252 252 m.list_filter, m.date_hierarchy, m.search_fields, 253 253 m.list_select_related, m.list_per_page, 254 m.list_ editable, m)254 m.list_max_show_all, m.list_editable, m) 255 255 256 256 # Make sure distinct() was called 257 257 self.assertEqual(cl.query_set.count(), 1) … … class ChangeListTests(TestCase): 271 271 cl = ChangeList(request, Parent, m.list_display, m.list_display_links, 272 272 m.list_filter, m.date_hierarchy, m.search_fields, 273 273 m.list_select_related, m.list_per_page, 274 m.list_ editable, m)274 m.list_max_show_all, m.list_editable, m) 275 275 276 276 # Make sure distinct() was called 277 277 self.assertEqual(cl.query_set.count(), 1) … … class ChangeListTests(TestCase): 292 292 m = ChildAdmin(Child, admin.site) 293 293 cl = ChangeList(request, Child, m.list_display, m.list_display_links, 294 294 m.list_filter, m.date_hierarchy, m.search_fields, 295 m.list_select_related, m.list_per_page, m.list_ editable, m)295 m.list_select_related, m.list_per_page, m.list_max_show_all, m.list_editable, m) 296 296 self.assertEqual(cl.query_set.count(), 60) 297 297 self.assertEqual(cl.paginator.count, 60) 298 298 self.assertEqual(cl.paginator.page_range, [1, 2, 3, 4, 5, 6]) … … class ChangeListTests(TestCase): 351 351 response.render() 352 352 self.assertContains(response, 'Parent object') 353 353 354 def test_show_all(self): 355 parent = Parent.objects.create(name='anything') 356 for i in range(30): 357 Child.objects.create(name='name %s' % i, parent=parent) 358 Child.objects.create(name='filtered %s' % i, parent=parent) 359 360 request = MockRequest() 361 # Add "show all" parameter to request 362 request.GET[ALL_VAR] = [] 363 364 # Test valid "show all" request (number of total objects is under max) 365 m = ChildAdmin(Child, admin.site) 366 # 200 is the max we'll pass to ChangeList 367 cl = ChangeList(request, Child, m.list_display, m.list_display_links, 368 m.list_filter, m.date_hierarchy, m.search_fields, 369 m.list_select_related, m.list_per_page, 200, m.list_editable, m) 370 cl.get_results(request) 371 self.assertEqual(len(cl.result_list), 60) 372 373 # Test invalid "show all" request (number of total objects over max) raises exception 374 m = ChildAdmin(Child, admin.site) 375 # 30 is the max we'll pass to ChangeList for this test 376 with self.assertRaises(IncorrectLookupParameters): 377 cl = ChangeList(request, Child, m.list_display, m.list_display_links, 378 m.list_filter, m.date_hierarchy, m.search_fields, 379 m.list_select_related, m.list_per_page, 30, m.list_editable, m) 380 354 381 355 382 class ParentAdmin(admin.ModelAdmin): 356 383 list_filter = ['child__name'] -
tests/regressiontests/admin_filters/tests.py
diff --git a/tests/regressiontests/admin_filters/tests.py b/tests/regressiontests/admin_filters/tests.py index b4c4f34..7f3afd7 100644
a b class ListFiltersTests(TestCase): 107 107 def get_changelist(self, request, model, modeladmin): 108 108 return ChangeList(request, model, modeladmin.list_display, modeladmin.list_display_links, 109 109 modeladmin.list_filter, modeladmin.date_hierarchy, modeladmin.search_fields, 110 modeladmin.list_select_related, modeladmin.list_per_page, modeladmin.list_ editable, modeladmin)110 modeladmin.list_select_related, modeladmin.list_per_page, modeladmin.list_max_show_all, modeladmin.list_editable, modeladmin) 111 111 112 112 def test_datefieldlistfilter(self): 113 113 modeladmin = BookAdmin(Book, site) -
tests/regressiontests/modeladmin/tests.py
diff --git a/tests/regressiontests/modeladmin/tests.py b/tests/regressiontests/modeladmin/tests.py index ad86f85..21fda22 100644
a b class ValidationTests(unittest.TestCase): 1114 1114 list_per_page = 100 1115 1115 1116 1116 validate(ValidationTestModelAdmin, ValidationTestModel) 1117 1118 def test_max_show_all_allowed_validation(self): 1119 1120 class ValidationTestModelAdmin(ModelAdmin): 1121 list_max_show_all = 'hello' 1122 1123 self.assertRaisesRegexp( 1124 ImproperlyConfigured, 1125 "'ValidationTestModelAdmin.list_max_show_all' should be an integer.", 1126 validate, 1127 ValidationTestModelAdmin, 1128 ValidationTestModel, 1129 ) 1130 1131 class ValidationTestModelAdmin(ModelAdmin): 1132 list_max_show_all = 200 1133 1134 validate(ValidationTestModelAdmin, ValidationTestModel) 1117 1135 1118 1136 def test_search_fields_validation(self): 1119 1137