Ticket #15997: max_show_all_in_model_admin.diff
File max_show_all_in_model_admin.diff, 14.6 KB (added by , 13 years ago) |
---|
-
docs/ref/contrib/admin/index.txt
649 649 The ``FieldListFilter`` API is currently considered internal 650 650 and prone to refactoring. 651 651 652 .. attribute:: ModelAdmin.list_max_show_all 653 654 .. versionadded:: 1.4 655 656 Set ``list_max_show_all`` to control how many items can appear on a "Show 657 all" admin change list page. The admin will display a "Show all" link on the 658 change list only if the total result count is less than or equal to this 659 setting. By default, this is set to ``200``. 660 652 661 .. attribute:: ModelAdmin.list_per_page 653 662 654 663 Set ``list_per_page`` to control how many items appear on each paginated -
django/contrib/admin/validation.py
97 97 if hasattr(cls, 'list_per_page') and not isinstance(cls.list_per_page, int): 98 98 raise ImproperlyConfigured("'%s.list_per_page' should be a integer." 99 99 % cls.__name__) 100 101 # list_max_show_all 102 if hasattr(cls, 'list_max_show_all') and not isinstance(cls.list_max_show_all, int): 103 raise ImproperlyConfigured("'%s.list_max_show_all' should be an integer." 104 % cls.__name__) 100 105 101 106 # list_editable 102 107 if hasattr(cls, 'list_editable') and cls.list_editable: -
django/contrib/admin/options.py
268 268 list_filter = () 269 269 list_select_related = False 270 270 list_per_page = 100 271 list_max_show_all = 200 271 272 list_editable = () 272 273 search_fields = () 273 274 date_hierarchy = None … … 1064 1065 try: 1065 1066 cl = ChangeList(request, self.model, list_display, self.list_display_links, 1066 1067 self.list_filter, self.date_hierarchy, self.search_fields, 1067 self.list_select_related, self.list_per_page, self.list_ editable, self)1068 self.list_select_related, self.list_per_page, self.list_max_show_all, self.list_editable, self) 1068 1069 except IncorrectLookupParameters: 1069 1070 # Wacky lookup parameters were given, so redirect to the main 1070 1071 # changelist page, without parameters, and pass an 'invalid=1' -
django/contrib/admin/views/main.py
11 11 from django.contrib.admin.options import IncorrectLookupParameters 12 12 from django.contrib.admin.util import quote, get_fields_from_path 13 13 14 # The system will display a "Show all" link on the change list only if the15 # total result count is less than or equal to this setting.16 MAX_SHOW_ALL_ALLOWED = 20017 18 14 # Changelist settings 19 15 ALL_VAR = 'all' 20 16 ORDER_VAR = 'o' … … 43 39 class ChangeList(object): 44 40 def __init__(self, request, model, list_display, list_display_links, 45 41 list_filter, date_hierarchy, search_fields, list_select_related, 46 list_per_page, list_ editable, model_admin):42 list_per_page, list_max_show_all, list_editable, model_admin): 47 43 self.model = model 48 44 self.opts = model._meta 49 45 self.lookup_opts = self.opts … … 55 51 self.search_fields = search_fields 56 52 self.list_select_related = list_select_related 57 53 self.list_per_page = list_per_page 54 self.list_max_show_all = list_max_show_all 58 55 self.model_admin = model_admin 59 56 60 57 # Get search parameters from the query string. … … 144 141 else: 145 142 full_result_count = self.root_query_set.count() 146 143 147 can_show_all = result_count <= MAX_SHOW_ALL_ALLOWED144 can_show_all = result_count <= self.list_max_show_all 148 145 multi_page = result_count > self.list_per_page 149 146 150 147 # Get the list of objects to display on this page. -
tests/regressiontests/admin_filters/tests.py
109 109 def get_changelist(self, request, model, modeladmin): 110 110 return ChangeList(request, model, modeladmin.list_display, modeladmin.list_display_links, 111 111 modeladmin.list_filter, modeladmin.date_hierarchy, modeladmin.search_fields, 112 modeladmin.list_select_related, modeladmin.list_per_page, modeladmin.list_ editable, modeladmin)112 modeladmin.list_select_related, modeladmin.list_per_page, modeladmin.list_max_show_all, modeladmin.list_editable, modeladmin) 113 113 114 114 def test_datefieldlistfilter(self): 115 115 modeladmin = BookAdmin(Book, site) -
tests/regressiontests/admin_changelist/tests.py
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 TransactionTestCase … … 18 18 m = ChildAdmin(Child, admin.site) 19 19 cl = ChangeList(MockRequest(), Child, m.list_display, m.list_display_links, 20 20 m.list_filter, m.date_hierarchy, m.search_fields, 21 m.list_select_related, m.list_per_page, m.list_ editable, m)21 m.list_select_related, m.list_per_page, m.list_max_show_all, m.list_editable, m) 22 22 self.assertEqual(cl.query_set.query.select_related, {'parent': {'name': {}}}) 23 23 24 24 def test_result_list_empty_changelist_value(self): … … 31 31 m = ChildAdmin(Child, admin.site) 32 32 cl = ChangeList(request, Child, m.list_display, m.list_display_links, 33 33 m.list_filter, m.date_hierarchy, m.search_fields, 34 m.list_select_related, m.list_per_page, m.list_ editable, m)34 m.list_select_related, m.list_per_page, m.list_max_show_all, m.list_editable, m) 35 35 cl.formset = None 36 36 template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}') 37 37 context = Context({'cl': cl}) … … 52 52 m = ChildAdmin(Child, admin.site) 53 53 cl = ChangeList(request, Child, m.list_display, m.list_display_links, 54 54 m.list_filter, m.date_hierarchy, m.search_fields, 55 m.list_select_related, m.list_per_page, m.list_ editable, m)55 m.list_select_related, m.list_per_page, m.list_max_show_all, m.list_editable, m) 56 56 cl.formset = None 57 57 template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}') 58 58 context = Context({'cl': cl}) … … 81 81 m.list_editable = ['name'] 82 82 cl = ChangeList(request, Child, m.list_display, m.list_display_links, 83 83 m.list_filter, m.date_hierarchy, m.search_fields, 84 m.list_select_related, m.list_per_page, m.list_ editable, m)84 m.list_select_related, m.list_per_page, m.list_max_show_all, m.list_editable, m) 85 85 FormSet = m.get_changelist_formset(request) 86 86 cl.formset = FormSet(queryset=cl.result_list) 87 87 template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}') … … 115 115 self.assertRaises(IncorrectLookupParameters, lambda: \ 116 116 ChangeList(request, Child, m.list_display, m.list_display_links, 117 117 m.list_filter, m.date_hierarchy, m.search_fields, 118 m.list_select_related, m.list_per_page, m.list_ editable, m))118 m.list_select_related, m.list_per_page, m.list_max_show_all, m.list_editable, m)) 119 119 120 120 def test_custom_paginator(self): 121 121 new_parent = Parent.objects.create(name='parent') … … 131 131 132 132 cl = ChangeList(request, Child, m.list_display, m.list_display_links, 133 133 m.list_filter, m.date_hierarchy, m.search_fields, 134 m.list_select_related, m.list_per_page, m.list_ editable, m)134 m.list_select_related, m.list_per_page, m.list_max_show_all, m.list_editable, m) 135 135 136 136 cl.get_results(request) 137 137 self.assertIsInstance(cl.paginator, CustomPaginator) … … 153 153 cl = ChangeList(request, Band, m.list_display, 154 154 m.list_display_links, m.list_filter, m.date_hierarchy, 155 155 m.search_fields, m.list_select_related, m.list_per_page, 156 m.list_ editable, m)156 m.list_max_show_all, m.list_editable, m) 157 157 158 158 cl.get_results(request) 159 159 … … 176 176 cl = ChangeList(request, Group, m.list_display, 177 177 m.list_display_links, m.list_filter, m.date_hierarchy, 178 178 m.search_fields, m.list_select_related, m.list_per_page, 179 m.list_ editable, m)179 m.list_max_show_all, m.list_editable, m) 180 180 181 181 cl.get_results(request) 182 182 … … 200 200 cl = ChangeList(request, Quartet, m.list_display, 201 201 m.list_display_links, m.list_filter, m.date_hierarchy, 202 202 m.search_fields, m.list_select_related, m.list_per_page, 203 m.list_ editable, m)203 m.list_max_show_all, m.list_editable, m) 204 204 205 205 cl.get_results(request) 206 206 … … 224 224 cl = ChangeList(request, ChordsBand, m.list_display, 225 225 m.list_display_links, m.list_filter, m.date_hierarchy, 226 226 m.search_fields, m.list_select_related, m.list_per_page, 227 m.list_ editable, m)227 m.list_max_show_all, m.list_editable, m) 228 228 229 229 cl.get_results(request) 230 230 … … 247 247 cl = ChangeList(request, Parent, m.list_display, m.list_display_links, 248 248 m.list_filter, m.date_hierarchy, m.search_fields, 249 249 m.list_select_related, m.list_per_page, 250 m.list_ editable, m)250 m.list_max_show_all, m.list_editable, m) 251 251 252 252 # Make sure distinct() was called 253 253 self.assertEqual(cl.query_set.count(), 1) … … 267 267 cl = ChangeList(request, Parent, m.list_display, m.list_display_links, 268 268 m.list_filter, m.date_hierarchy, m.search_fields, 269 269 m.list_select_related, m.list_per_page, 270 m.list_ editable, m)270 m.list_max_show_all, m.list_editable, m) 271 271 272 272 # Make sure distinct() was called 273 273 self.assertEqual(cl.query_set.count(), 1) … … 288 288 m = ChildAdmin(Child, admin.site) 289 289 cl = ChangeList(request, Child, m.list_display, m.list_display_links, 290 290 m.list_filter, m.date_hierarchy, m.search_fields, 291 m.list_select_related, m.list_per_page, m.list_ editable, m)291 m.list_select_related, m.list_per_page, m.list_max_show_all, m.list_editable, m) 292 292 self.assertEqual(cl.query_set.count(), 60) 293 293 self.assertEqual(cl.paginator.count, 60) 294 294 self.assertEqual(cl.paginator.page_range, [1, 2, 3, 4, 5, 6]) … … 297 297 m = FilteredChildAdmin(Child, admin.site) 298 298 cl = ChangeList(request, Child, m.list_display, m.list_display_links, 299 299 m.list_filter, m.date_hierarchy, m.search_fields, 300 m.list_select_related, m.list_per_page, m.list_ editable, m)300 m.list_select_related, m.list_per_page, m.list_max_show_all, m.list_editable, m) 301 301 self.assertEqual(cl.query_set.count(), 30) 302 302 self.assertEqual(cl.paginator.count, 30) 303 303 self.assertEqual(cl.paginator.page_range, [1, 2, 3]) 304 305 def test_show_all(self): 306 parent = Parent.objects.create(name='anything') 307 for i in range(30): 308 Child.objects.create(name='name %s' % i, parent=parent) 309 Child.objects.create(name='filtered %s' % i, parent=parent) 304 310 311 request = MockRequest() 312 # Add "show all" parameter to request 313 request.GET[ALL_VAR] = [] 314 315 # Test valid "show all" request (number of total objects is under max) 316 m = ChildAdmin(Child, admin.site) 317 # 200 is the max we'll pass to ChangeList 318 cl = ChangeList(request, Child, m.list_display, m.list_display_links, 319 m.list_filter, m.date_hierarchy, m.search_fields, 320 m.list_select_related, m.list_per_page, 200, m.list_editable, m) 321 cl.get_results(request) 322 self.assertEqual(len(cl.result_list), 60) 323 324 # Test invalid "show all" request (number of total objects over max) raises exception 325 m = ChildAdmin(Child, admin.site) 326 # 30 is the max we'll pass to ChangeList for this test 327 with self.assertRaises(IncorrectLookupParameters): 328 cl = ChangeList(request, Child, m.list_display, m.list_display_links, 329 m.list_filter, m.date_hierarchy, m.search_fields, 330 m.list_select_related, m.list_per_page, 30, m.list_editable, m) 305 331 332 306 333 class ParentAdmin(admin.ModelAdmin): 307 334 list_filter = ['child__name'] 308 335 search_fields = ['child__name'] -
tests/regressiontests/modeladmin/tests.py
932 932 list_per_page = 100 933 933 934 934 validate(ValidationTestModelAdmin, ValidationTestModel) 935 936 def test_max_show_all_allowed_validation(self): 935 937 938 class ValidationTestModelAdmin(ModelAdmin): 939 list_max_show_all = 'hello' 940 941 self.assertRaisesRegexp( 942 ImproperlyConfigured, 943 "'ValidationTestModelAdmin.list_max_show_all' should be an integer.", 944 validate, 945 ValidationTestModelAdmin, 946 ValidationTestModel, 947 ) 948 949 class ValidationTestModelAdmin(ModelAdmin): 950 list_max_show_all = 200 951 952 validate(ValidationTestModelAdmin, ValidationTestModel) 953 936 954 def test_search_fields_validation(self): 937 955 938 956 class ValidationTestModelAdmin(ModelAdmin):