Ticket #17128: 17128.diff

File 17128.diff, 8.7 KB (added by Simon Meers, 13 years ago)
  • django/contrib/admin/views/main.py

    diff --git a/django/contrib/admin/views/main.py b/django/contrib/admin/views/main.py
    index 616b249..4eed016 100644
    a b class ChangeList(object):  
    221221            # for ordering specified on ModelAdmin or model Meta, we don't know
    222222            # the right column numbers absolutely, because there might be more
    223223            # than one column associated with that ordering, so we guess.
    224             for f in ordering:
    225                 if f.startswith('-'):
    226                     f = f[1:]
     224            for field in ordering:
     225                if field.startswith('-'):
     226                    field = field[1:]
    227227                    order_type = 'desc'
    228228                else:
    229229                    order_type = 'asc'
    230                 i = None
     230                index = None
    231231                try:
    232232                    # Search for simply field name first
    233                     i = self.list_display.index(f)
     233                    index = list(self.list_display).index(field)
    234234                except ValueError:
    235                     # No match, but their might be a match if we take into account
    236                     # 'admin_order_field'
    237                     for j, attr in enumerate(self.list_display):
    238                         if getattr(attr, 'admin_order_field', '') == f:
    239                             i = j
     235                    # No match, but there might be a match if we take into
     236                    # account 'admin_order_field'
     237                    for _index, attr in enumerate(self.list_display):
     238                        if getattr(attr, 'admin_order_field', '') == field:
     239                            index = _index
    240240                            break
    241                 if i is not None:
    242                     ordering_fields[i] = order_type
     241                if index is not None:
     242                    ordering_fields[index] = order_type
    243243        else:
    244244            for p in self.params[ORDER_VAR].split('.'):
    245245                none, pfx, idx = p.rpartition('-')
  • tests/regressiontests/admin_changelist/admin.py

    diff --git a/tests/regressiontests/admin_changelist/admin.py b/tests/regressiontests/admin_changelist/admin.py
    index d1d8e0f..80bb732 100644
    a b from django.contrib import admin  
    44from django.core.paginator import Paginator
    55
    66from .models import (Child, Parent, Genre, Band, Musician, Group, Quartet,
    7     Membership, ChordsMusician, ChordsBand, Invitation)
     7    Membership, ChordsMusician, ChordsBand, Invitation, Swallow)
    88
    99
    1010site = admin.AdminSite(name="admin")
    class DynamicListDisplayLinksChildAdmin(admin.ModelAdmin):  
    7575        return ['age']
    7676
    7777site.register(Child, DynamicListDisplayChildAdmin)
     78
     79class SwallowAdmin(admin.ModelAdmin):
     80    actions = None # prevent ['action_checkbox'] + list(list_display)
     81    list_display = ('origin', 'load', 'speed')
     82
     83site.register(Swallow, SwallowAdmin)
  • tests/regressiontests/admin_changelist/models.py

    diff --git a/tests/regressiontests/admin_changelist/models.py b/tests/regressiontests/admin_changelist/models.py
    index eed3f68..97080fb 100644
    a b class Invitation(models.Model):  
    4949    player = models.ForeignKey(ChordsMusician)
    5050    band = models.ForeignKey(ChordsBand)
    5151    instrument = models.CharField(max_length=15)
     52
     53class Swallow(models.Model):
     54    origin = models.CharField(max_length=255)
     55    load = models.FloatField()
     56    speed = models.FloatField()
     57
     58    class Meta:
     59        ordering = ('speed', 'load')
  • tests/regressiontests/admin_changelist/tests.py

    diff --git a/tests/regressiontests/admin_changelist/tests.py b/tests/regressiontests/admin_changelist/tests.py
    index 9292895..b422519 100644
    a b from django.test.client import RequestFactory  
    1111from .admin import (ChildAdmin, QuartetAdmin, BandAdmin, ChordsBandAdmin,
    1212    GroupAdmin, ParentAdmin, DynamicListDisplayChildAdmin,
    1313    DynamicListDisplayLinksChildAdmin, CustomPaginationAdmin,
    14     FilteredChildAdmin, CustomPaginator, site as custom_site)
     14    FilteredChildAdmin, CustomPaginator, site as custom_site,
     15    SwallowAdmin)
    1516from .models import (Child, Parent, Genre, Band, Musician, Group, Quartet,
    16     Membership, ChordsMusician, ChordsBand, Invitation)
     17    Membership, ChordsMusician, ChordsBand, Invitation, Swallow)
    1718
    1819
    1920class ChangeListTests(TestCase):
    class ChangeListTests(TestCase):  
    2223    def setUp(self):
    2324        self.factory = RequestFactory()
    2425
     26    def _create_superuser(self, username):
     27        return User.objects.create(username=username, is_superuser=True)
     28
     29    def _mocked_authenticated_request(self, url, user):
     30        request = self.factory.get(url)
     31        request.user = user
     32        return request
     33
    2534    def test_select_related_preserved(self):
    2635        """
    2736        Regression test for #10348: ChangeList.get_query_set() shouldn't
    class ChangeListTests(TestCase):  
    323332        for i in range(10):
    324333            Child.objects.create(name='child %s' % i, parent=parent)
    325334
    326         user_noparents = User.objects.create(
    327             username='noparents',
    328             is_superuser=True)
    329         user_parents = User.objects.create(
    330             username='parents',
    331             is_superuser=True)
    332 
    333         def _mocked_authenticated_request(user):
    334             request = self.factory.get('/child/')
    335             request.user = user
    336             return request
     335        user_noparents = self._create_superuser('noparents')
     336        user_parents = self._create_superuser('parents')
    337337
    338338        # Test with user 'noparents'
    339339        m = custom_site._registry[Child]
    340         request = _mocked_authenticated_request(user_noparents)
     340        request = self._mocked_authenticated_request('/child/', user_noparents)
    341341        response = m.changelist_view(request)
    342342        self.assertNotContains(response, 'Parent object')
    343343
    class ChangeListTests(TestCase):  
    348348
    349349        # Test with user 'parents'
    350350        m = DynamicListDisplayChildAdmin(Child, admin.site)
    351         request = _mocked_authenticated_request(user_parents)
     351        request = self._mocked_authenticated_request('/child/', user_parents)
    352352        response = m.changelist_view(request)
    353353        self.assertContains(response, 'Parent object')
    354354
    class ChangeListTests(TestCase):  
    362362        # Test default implementation
    363363        custom_site.register(Child, ChildAdmin)
    364364        m = custom_site._registry[Child]
    365         request = _mocked_authenticated_request(user_noparents)
     365        request = self._mocked_authenticated_request('/child/', user_noparents)
    366366        response = m.changelist_view(request)
    367367        self.assertContains(response, 'Parent object')
    368368
    class ChangeListTests(TestCase):  
    402402        for i in range(1, 10):
    403403            Child.objects.create(id=i, name='child %s' % i, parent=parent, age=i)
    404404
    405         superuser = User.objects.create(
    406             username='superuser',
    407             is_superuser=True)
    408 
    409         def _mocked_authenticated_request(user):
    410             request = self.factory.get('/child/')
    411             request.user = user
    412             return request
    413 
    414405        m = DynamicListDisplayLinksChildAdmin(Child, admin.site)
    415         request = _mocked_authenticated_request(superuser)
     406        superuser = self._create_superuser('superuser')
     407        request = self._mocked_authenticated_request('/child/', superuser)
    416408        response = m.changelist_view(request)
    417409        for i in range(1, 10):
    418410            self.assertContains(response, '<a href="%s/">%s</a>' % (i, i))
    class ChangeListTests(TestCase):  
    420412        list_display = m.get_list_display(request)
    421413        list_display_links = m.get_list_display_links(request, list_display)
    422414        self.assertEqual(list_display, ('parent', 'name', 'age'))
    423         self.assertEqual(list_display_links, ['age'])
    424  No newline at end of file
     415        self.assertEqual(list_display_links, ['age'])
     416
     417    def test_tuple_list_display(self):
     418        """
     419        Regression test for #17128
     420        (ChangeList failing under Python 2.5 after r16319)
     421        """
     422        swallow = Swallow.objects.create(
     423            origin='Africa', load='12.34', speed='22.2')
     424        model_admin = SwallowAdmin(Swallow, admin.site)
     425        superuser = self._create_superuser('superuser')
     426        request = self._mocked_authenticated_request('/swallow/', superuser)
     427        response = model_admin.changelist_view(request)
     428        # just want to ensure it doesn't blow up during rendering
     429        self.assertContains(response, unicode(swallow.origin))
     430        self.assertContains(response, unicode(swallow.load))
     431        self.assertContains(response, unicode(swallow.speed))
     432
Back to Top