Ticket #14206: 14206_r16204.diff

File 14206_r16204.diff, 4.7 KB (added by cyrus, 13 years ago)
  • django/contrib/admin/options.py

     
    608608            description = capfirst(action.replace('_', ' '))
    609609        return func, action, description
    610610
     611    def get_list_display(self, request):
     612        """
     613        Return a sequence containing the fields to be displayed on the
     614        changelist.
     615        """
     616        return self.list_display
     617
    611618    def construct_change_message(self, request, form, formsets):
    612619        """
    613620        Construct a change message from a changed object.
     
    10261033        actions = self.get_actions(request)
    10271034
    10281035        # Remove action checkboxes if there aren't any actions available.
    1029         list_display = list(self.list_display)
     1036        list_display = list(self.get_list_display(request))
    10301037        if not actions:
    10311038            try:
    10321039                list_display.remove('action_checkbox')
  • tests/regressiontests/admin_changelist/tests.py

     
     1from django.conf import settings
    12from django.contrib import admin
    23from django.contrib.admin.options import IncorrectLookupParameters
    34from django.contrib.admin.views.main import ChangeList
     5from django.contrib.auth.models import User
     6from django.http import QueryDict
    47from django.template import Context, Template
    58from django.test import TransactionTestCase
    69from regressiontests.admin_changelist.models import Child, Parent
     
    143146        self.assertEqual(cl.paginator.count, 30)
    144147        self.assertEqual(cl.paginator.page_range, [1, 2, 3])
    145148
     149    def test_list_display(self):
     150        """
     151        Regression tests for #14206: dynamic list_display support.
     152        """
     153        parent = Parent.objects.create(name='parent')
     154        for i in range(10):
     155            Child.objects.create(name='child %s' % i, parent=parent)
    146156
     157        user_noparents = User.objects.create(
     158            username='noparents',
     159            is_superuser=True)
     160        user_parents = User.objects.create(
     161            username='parents',
     162            is_superuser=True)
     163
     164        # Test with user 'noparents'
     165        m = DynamicListDisplayChildAdmin(Child, admin.site)
     166        request = ViewMockRequest(user_noparents)
     167        response = m.changelist_view(request)
     168        self.assertNotContains(response, 'Parent object')
     169
     170        # Test with user 'parents'
     171        m = DynamicListDisplayChildAdmin(Child, admin.site)
     172        request = ViewMockRequest(user_parents)
     173        response = m.changelist_view(request)
     174        self.assertContains(response, 'Parent object')
     175
     176        # Test default implementation
     177        m = ChildAdmin(Child, admin.site)
     178        request = ViewMockRequest(user_noparents)
     179        response = m.changelist_view(request)
     180        self.assertContains(response, 'Parent object')
     181
     182
    147183class ChildAdmin(admin.ModelAdmin):
    148184    list_display = ['name', 'parent']
    149185    list_per_page = 10
     
    157193        return super(FilteredChildAdmin, self).queryset(request).filter(
    158194            name__contains='filtered')
    159195
     196class DynamicListDisplayChildAdmin(admin.ModelAdmin):
     197    list_display = ['name', 'parent']
     198
     199    def get_list_display(self, request):
     200        list_display = self.list_display
     201        if request.user.username == 'noparents':
     202            list_display.remove('parent')
     203
     204        return list_display
     205
    160206class MockRequest(object):
    161207    GET = {}
     208
     209
     210class ViewMockRequest(object):
     211    def __init__(self, user):
     212        self.user = user
     213        self.COOKIES = {settings.CSRF_COOKIE_NAME: ''}
     214        self.GET = QueryDict('')
     215        self.POST = QueryDict('')
     216        self.META = {}
     217        self.method = 'GET'
  • docs/ref/contrib/admin/index.txt

     
    807807    a ``list`` or ``tuple`` of field names that will be displayed as read-only,
    808808    as described above in the :attr:`ModelAdmin.readonly_fields` section.
    809809
     810.. method:: ModelAdmin.get_list_display(self, request)
     811
     812    .. versionadded:: 1.2.6
     813
     814    The ``get_list_display`` method is given the ``HttpRequest`` and is
     815    expected to return a ``list`` or ``tuple`` of field names that will be
     816    displayed on the changelist view as described above in the
     817    :attr:`ModelAdmin.list_display` section.
     818
    810819.. method:: ModelAdmin.get_urls(self)
    811820
    812821    .. versionadded:: 1.1
Back to Top