Ticket #16257: 16257_r16400.diff

File 16257_r16400.diff, 5.1 KB (added by graveyboat, 13 years ago)

Updated the patch to build list_diplay_links outside the invocation of ChangeList

  • docs/ref/contrib/admin/index.txt

     
    10051005    displayed on the changelist view as described above in the
    10061006    :attr:`ModelAdmin.list_display` section.
    10071007
     1008.. method:: ModelAdmin.get_list_display_links(self, request, list_display)
     1009
     1010    .. versionadded:: 1.4
     1011
     1012    The ``get_list_display_links`` method is given the ``HttpRequest`` and
     1013    the ``list`` or ``tuple`` returned from ModelAdmin.get_list_display
     1014    and is expected to return a ``list`` or ``tuple`` of field names that
     1015    will be displayed on the changelist view as described above in the
     1016    :attr:`ModelAdmin.list_display_links` section.
     1017
    10081018.. method:: ModelAdmin.get_urls(self)
    10091019
    10101020    The ``get_urls`` method on a ``ModelAdmin`` returns the URLs to be used for
  • tests/regressiontests/admin_changelist/tests.py

     
    351351        response.render()
    352352        self.assertContains(response, 'Parent object')
    353353
     354    def test_dynamic_list_display_links(self):
     355        """
     356        Regression tests for #16257: dynamic list_display_links support.
     357        """
     358        parent = Parent.objects.create(name='parent')
     359        for i in range(10):
     360            Child.objects.create(name='child %s' % i, parent=parent)
    354361
     362        user_noparents = User.objects.create(
     363            username='noparents',
     364            is_superuser=True)
     365        user_parents = User.objects.create(
     366            username='parents',
     367            is_superuser=True)
     368
     369        def _mocked_authenticated_request(user):
     370            request = self.factory.get('/child/')
     371            request.user = user
     372            return request
     373
     374        # Test with user 'noparents'
     375        m = DynamicListDisplayLinksChildAdmin(Child, admin.site)
     376        request = _mocked_authenticated_request(user_noparents)
     377        response = m.changelist_view(request)
     378        # XXX - Calling render here to avoid ContentNotRenderedError to be
     379        # raised. Ticket #15826 should fix this but it's not yet integrated.
     380        response.render()
     381        self.assertNotContains(response, 'Parent object')
     382
     383        # Test with user 'parents'
     384        m = DynamicListDisplayLinksChildAdmin(Child, admin.site)
     385        request = _mocked_authenticated_request(user_parents)
     386        response = m.changelist_view(request)
     387        # XXX - #15826
     388        response.render()
     389        self.assertContains(response, 'Parent object')
     390
     391        # Test default implementation
     392        m = ChildAdmin(Child, admin.site)
     393        request = _mocked_authenticated_request(user_noparents)
     394        response = m.changelist_view(request)
     395        # XXX - #15826
     396        response.render()
     397        self.assertContains(response, 'Parent object')
     398
     399
    355400class ParentAdmin(admin.ModelAdmin):
    356401    list_filter = ['child__name']
    357402    search_fields = ['child__name']
     
    405450            my_list_display.remove('parent')
    406451
    407452        return my_list_display
     453
     454class DynamicListDisplayLinksChildAdmin(admin.ModelAdmin):
     455    list_display = ('name', 'parent')
     456    list_display_links = ('parent',)
     457
     458    def get_list_display(self, request):
     459        my_list_display = list(self.list_display)
     460        if request.user.username == 'noparents':
     461            my_list_display.remove('parent')
     462
     463        return my_list_display
     464
     465    def get_list_display_links(self, request, list_display):
     466        my_list_display_links = list(set(list_display) & set(self.list_display_links))
     467        if my_list_display_links == []:
     468            my_list_display_links = [list_display[1]]
     469
     470        return my_list_display_links
  • django/contrib/admin/options.py

     
    639639        """
    640640        return self.list_display
    641641
     642    def get_list_display_links(self, request, list_display):
     643        """
     644        Return a sequence containing the fields to be displayed as links
     645        on the changelist.
     646        """
     647        return self.list_display_links
     648
    642649    def construct_change_message(self, request, form, formsets):
    643650        """
    644651        Construct a change message from a changed object.
     
    10731080                list_display.remove('action_checkbox')
    10741081            except ValueError:
    10751082                pass
     1083        list_display_links = list(self.get_list_display_links(request,list_display))
    10761084
    10771085        ChangeList = self.get_changelist(request)
    10781086        try:
    1079             cl = ChangeList(request, self.model, list_display, self.list_display_links,
     1087            cl = ChangeList(request, self.model, list_display, list_display_links),
    10801088                self.list_filter, self.date_hierarchy, self.search_fields,
    10811089                self.list_select_related, self.list_per_page, self.list_editable, self)
    10821090        except IncorrectLookupParameters:
Back to Top