Ticket #15997: max_show_all_in_model_admin.v2.diff

File max_show_all_in_model_admin.v2.diff, 14.7 KB (added by Jim Dalton, 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):  
    278278    list_filter = ()
    279279    list_select_related = False
    280280    list_per_page = 100
     281    list_max_show_all = 200
    281282    list_editable = ()
    282283    search_fields = ()
    283284    date_hierarchy = None
    class ModelAdmin(BaseModelAdmin):  
    10871088        try:
    10881089            cl = ChangeList(request, self.model, list_display, self.list_display_links,
    10891090                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)
    10911092        except IncorrectLookupParameters:
    10921093            # Wacky lookup parameters were given, so redirect to the main
    10931094            # 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):  
    9595    if hasattr(cls, 'list_per_page') and not isinstance(cls.list_per_page, int):
    9696        raise ImproperlyConfigured("'%s.list_per_page' should be a integer."
    9797                % 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__)
    98103
    99104    # list_editable
    100105    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  
    1212from django.contrib.admin.options import IncorrectLookupParameters
    1313from django.contrib.admin.util import quote, get_fields_from_path
    1414
    15 # The system will display a "Show all" link on the change list only if the
    16 # total result count is less than or equal to this setting.
    17 MAX_SHOW_ALL_ALLOWED = 200
    18 
    1915# Changelist settings
    2016ALL_VAR = 'all'
    2117ORDER_VAR = 'o'
    def field_needs_distinct(field):  
    4440class ChangeList(object):
    4541    def __init__(self, request, model, list_display, list_display_links,
    4642            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):
    4844        self.model = model
    4945        self.opts = model._meta
    5046        self.lookup_opts = self.opts
    class ChangeList(object):  
    5652        self.search_fields = search_fields
    5753        self.list_select_related = list_select_related
    5854        self.list_per_page = list_per_page
     55        self.list_max_show_all = list_max_show_all
    5956        self.model_admin = model_admin
    6057
    6158        # Get search parameters from the query string.
    class ChangeList(object):  
    145142        else:
    146143            full_result_count = self.root_query_set.count()
    147144
    148         can_show_all = result_count <= MAX_SHOW_ALL_ALLOWED
     145        can_show_all = result_count <= self.list_max_show_all
    149146        multi_page = result_count > self.list_per_page
    150147
    151148        # 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::  
    695695              The ``FieldListFilter`` API is currently considered internal
    696696              and prone to refactoring.
    697697
     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
    698707.. attribute:: ModelAdmin.list_per_page
    699708
    700709    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  
    11from django.contrib import admin
    22from django.contrib.admin.options import IncorrectLookupParameters
    3 from django.contrib.admin.views.main import ChangeList, SEARCH_VAR
     3from django.contrib.admin.views.main import ChangeList, SEARCH_VAR, ALL_VAR
    44from django.core.paginator import Paginator
    55from django.template import Context, Template
    66from django.test import TestCase
    class ChangeListTests(TestCase):  
    2424        request = self.factory.get('/child/')
    2525        cl = ChangeList(request, Child, m.list_display, m.list_display_links,
    2626                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)
    2828        self.assertEqual(cl.query_set.query.select_related, {'parent': {'name': {}}})
    2929
    3030    def test_result_list_empty_changelist_value(self):
    class ChangeListTests(TestCase):  
    3737        m = ChildAdmin(Child, admin.site)
    3838        cl = ChangeList(request, Child, m.list_display, m.list_display_links,
    3939                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)
    4141        cl.formset = None
    4242        template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}')
    4343        context = Context({'cl': cl})
    class ChangeListTests(TestCase):  
    5757        m = ChildAdmin(Child, admin.site)
    5858        cl = ChangeList(request, Child, m.list_display, m.list_display_links,
    5959                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)
    6161        cl.formset = None
    6262        template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}')
    6363        context = Context({'cl': cl})
    class ChangeListTests(TestCase):  
    8686        m.list_editable = ['name']
    8787        cl = ChangeList(request, Child, m.list_display, m.list_display_links,
    8888                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)
    9090        FormSet = m.get_changelist_formset(request)
    9191        cl.formset = FormSet(queryset=cl.result_list)
    9292        template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}')
    class ChangeListTests(TestCase):  
    119119        self.assertRaises(IncorrectLookupParameters, lambda: \
    120120            ChangeList(request, Child, m.list_display, m.list_display_links,
    121121                    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))
    123123
    124124    def test_custom_paginator(self):
    125125        new_parent = Parent.objects.create(name='parent')
    class ChangeListTests(TestCase):  
    135135
    136136        cl = ChangeList(request, Child, m.list_display, m.list_display_links,
    137137                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)
    139139
    140140        cl.get_results(request)
    141141        self.assertIsInstance(cl.paginator, CustomPaginator)
    class ChangeListTests(TestCase):  
    157157        cl = ChangeList(request, Band, m.list_display,
    158158                m.list_display_links, m.list_filter, m.date_hierarchy,
    159159                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)
    161161
    162162        cl.get_results(request)
    163163
    class ChangeListTests(TestCase):  
    180180        cl = ChangeList(request, Group, m.list_display,
    181181                m.list_display_links, m.list_filter, m.date_hierarchy,
    182182                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)
    184184
    185185        cl.get_results(request)
    186186
    class ChangeListTests(TestCase):  
    204204        cl = ChangeList(request, Quartet, m.list_display,
    205205                m.list_display_links, m.list_filter, m.date_hierarchy,
    206206                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)
    208208
    209209        cl.get_results(request)
    210210
    class ChangeListTests(TestCase):  
    228228        cl = ChangeList(request, ChordsBand, m.list_display,
    229229                m.list_display_links, m.list_filter, m.date_hierarchy,
    230230                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)
    232232
    233233        cl.get_results(request)
    234234
    class ChangeListTests(TestCase):  
    251251        cl = ChangeList(request, Parent, m.list_display, m.list_display_links,
    252252                        m.list_filter, m.date_hierarchy, m.search_fields,
    253253                        m.list_select_related, m.list_per_page,
    254                         m.list_editable, m)
     254                        m.list_max_show_all, m.list_editable, m)
    255255
    256256        # Make sure distinct() was called
    257257        self.assertEqual(cl.query_set.count(), 1)
    class ChangeListTests(TestCase):  
    271271        cl = ChangeList(request, Parent, m.list_display, m.list_display_links,
    272272                        m.list_filter, m.date_hierarchy, m.search_fields,
    273273                        m.list_select_related, m.list_per_page,
    274                         m.list_editable, m)
     274                        m.list_max_show_all, m.list_editable, m)
    275275
    276276        # Make sure distinct() was called
    277277        self.assertEqual(cl.query_set.count(), 1)
    class ChangeListTests(TestCase):  
    292292        m = ChildAdmin(Child, admin.site)
    293293        cl = ChangeList(request, Child, m.list_display, m.list_display_links,
    294294                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)
    296296        self.assertEqual(cl.query_set.count(), 60)
    297297        self.assertEqual(cl.paginator.count, 60)
    298298        self.assertEqual(cl.paginator.page_range, [1, 2, 3, 4, 5, 6])
    class ChangeListTests(TestCase):  
    351351        response.render()
    352352        self.assertContains(response, 'Parent object')
    353353
     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
    354381
    355382class ParentAdmin(admin.ModelAdmin):
    356383    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):  
    107107    def get_changelist(self, request, model, modeladmin):
    108108        return ChangeList(request, model, modeladmin.list_display, modeladmin.list_display_links,
    109109            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)
    111111
    112112    def test_datefieldlistfilter(self):
    113113        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):  
    11141114            list_per_page = 100
    11151115
    11161116        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)
    11171135
    11181136    def test_search_fields_validation(self):
    11191137
Back to Top