Fixed #17128 -- Fixed a Python 2.5 incompatibility. Thanks, Simon Meers.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17102 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jannis Leidel 2011-11-17 21:30:07 +00:00
parent c20d33201c
commit 773d91ead2
4 changed files with 63 additions and 40 deletions

View File

@ -221,25 +221,25 @@ class ChangeList(object):
# for ordering specified on ModelAdmin or model Meta, we don't know # for ordering specified on ModelAdmin or model Meta, we don't know
# the right column numbers absolutely, because there might be more # the right column numbers absolutely, because there might be more
# than one column associated with that ordering, so we guess. # than one column associated with that ordering, so we guess.
for f in ordering: for field in ordering:
if f.startswith('-'): if field.startswith('-'):
f = f[1:] field = field[1:]
order_type = 'desc' order_type = 'desc'
else: else:
order_type = 'asc' order_type = 'asc'
i = None index = None
try: try:
# Search for simply field name first # Search for simply field name first
i = self.list_display.index(f) index = list(self.list_display).index(field)
except ValueError: except ValueError:
# No match, but their might be a match if we take into account # No match, but there might be a match if we take into
# 'admin_order_field' # account 'admin_order_field'
for j, attr in enumerate(self.list_display): for _index, attr in enumerate(self.list_display):
if getattr(attr, 'admin_order_field', '') == f: if getattr(attr, 'admin_order_field', '') == field:
i = j index = _index
break break
if i is not None: if index is not None:
ordering_fields[i] = order_type ordering_fields[index] = order_type
else: else:
for p in self.params[ORDER_VAR].split('.'): for p in self.params[ORDER_VAR].split('.'):
none, pfx, idx = p.rpartition('-') none, pfx, idx = p.rpartition('-')

View File

@ -4,7 +4,7 @@ from django.contrib import admin
from django.core.paginator import Paginator from django.core.paginator import Paginator
from .models import (Child, Parent, Genre, Band, Musician, Group, Quartet, from .models import (Child, Parent, Genre, Band, Musician, Group, Quartet,
Membership, ChordsMusician, ChordsBand, Invitation) Membership, ChordsMusician, ChordsBand, Invitation, Swallow)
site = admin.AdminSite(name="admin") site = admin.AdminSite(name="admin")
@ -75,3 +75,9 @@ class DynamicListDisplayLinksChildAdmin(admin.ModelAdmin):
return ['age'] return ['age']
site.register(Child, DynamicListDisplayChildAdmin) site.register(Child, DynamicListDisplayChildAdmin)
class SwallowAdmin(admin.ModelAdmin):
actions = None # prevent ['action_checkbox'] + list(list_display)
list_display = ('origin', 'load', 'speed')
site.register(Swallow, SwallowAdmin)

View File

@ -49,3 +49,11 @@ class Invitation(models.Model):
player = models.ForeignKey(ChordsMusician) player = models.ForeignKey(ChordsMusician)
band = models.ForeignKey(ChordsBand) band = models.ForeignKey(ChordsBand)
instrument = models.CharField(max_length=15) instrument = models.CharField(max_length=15)
class Swallow(models.Model):
origin = models.CharField(max_length=255)
load = models.FloatField()
speed = models.FloatField()
class Meta:
ordering = ('speed', 'load')

View File

@ -11,9 +11,10 @@ from django.test.client import RequestFactory
from .admin import (ChildAdmin, QuartetAdmin, BandAdmin, ChordsBandAdmin, from .admin import (ChildAdmin, QuartetAdmin, BandAdmin, ChordsBandAdmin,
GroupAdmin, ParentAdmin, DynamicListDisplayChildAdmin, GroupAdmin, ParentAdmin, DynamicListDisplayChildAdmin,
DynamicListDisplayLinksChildAdmin, CustomPaginationAdmin, DynamicListDisplayLinksChildAdmin, CustomPaginationAdmin,
FilteredChildAdmin, CustomPaginator, site as custom_site) FilteredChildAdmin, CustomPaginator, site as custom_site,
SwallowAdmin)
from .models import (Child, Parent, Genre, Band, Musician, Group, Quartet, from .models import (Child, Parent, Genre, Band, Musician, Group, Quartet,
Membership, ChordsMusician, ChordsBand, Invitation) Membership, ChordsMusician, ChordsBand, Invitation, Swallow)
class ChangeListTests(TestCase): class ChangeListTests(TestCase):
@ -22,6 +23,14 @@ class ChangeListTests(TestCase):
def setUp(self): def setUp(self):
self.factory = RequestFactory() self.factory = RequestFactory()
def _create_superuser(self, username):
return User.objects.create(username=username, is_superuser=True)
def _mocked_authenticated_request(self, url, user):
request = self.factory.get(url)
request.user = user
return request
def test_select_related_preserved(self): def test_select_related_preserved(self):
""" """
Regression test for #10348: ChangeList.get_query_set() shouldn't Regression test for #10348: ChangeList.get_query_set() shouldn't
@ -323,21 +332,12 @@ class ChangeListTests(TestCase):
for i in range(10): for i in range(10):
Child.objects.create(name='child %s' % i, parent=parent) Child.objects.create(name='child %s' % i, parent=parent)
user_noparents = User.objects.create( user_noparents = self._create_superuser('noparents')
username='noparents', user_parents = self._create_superuser('parents')
is_superuser=True)
user_parents = User.objects.create(
username='parents',
is_superuser=True)
def _mocked_authenticated_request(user):
request = self.factory.get('/child/')
request.user = user
return request
# Test with user 'noparents' # Test with user 'noparents'
m = custom_site._registry[Child] m = custom_site._registry[Child]
request = _mocked_authenticated_request(user_noparents) request = self._mocked_authenticated_request('/child/', user_noparents)
response = m.changelist_view(request) response = m.changelist_view(request)
self.assertNotContains(response, 'Parent object') self.assertNotContains(response, 'Parent object')
@ -348,7 +348,7 @@ class ChangeListTests(TestCase):
# Test with user 'parents' # Test with user 'parents'
m = DynamicListDisplayChildAdmin(Child, admin.site) m = DynamicListDisplayChildAdmin(Child, admin.site)
request = _mocked_authenticated_request(user_parents) request = self._mocked_authenticated_request('/child/', user_parents)
response = m.changelist_view(request) response = m.changelist_view(request)
self.assertContains(response, 'Parent object') self.assertContains(response, 'Parent object')
@ -362,7 +362,7 @@ class ChangeListTests(TestCase):
# Test default implementation # Test default implementation
custom_site.register(Child, ChildAdmin) custom_site.register(Child, ChildAdmin)
m = custom_site._registry[Child] m = custom_site._registry[Child]
request = _mocked_authenticated_request(user_noparents) request = self._mocked_authenticated_request('/child/', user_noparents)
response = m.changelist_view(request) response = m.changelist_view(request)
self.assertContains(response, 'Parent object') self.assertContains(response, 'Parent object')
@ -402,17 +402,9 @@ class ChangeListTests(TestCase):
for i in range(1, 10): for i in range(1, 10):
Child.objects.create(id=i, name='child %s' % i, parent=parent, age=i) Child.objects.create(id=i, name='child %s' % i, parent=parent, age=i)
superuser = User.objects.create(
username='superuser',
is_superuser=True)
def _mocked_authenticated_request(user):
request = self.factory.get('/child/')
request.user = user
return request
m = DynamicListDisplayLinksChildAdmin(Child, admin.site) m = DynamicListDisplayLinksChildAdmin(Child, admin.site)
request = _mocked_authenticated_request(superuser) superuser = self._create_superuser('superuser')
request = self._mocked_authenticated_request('/child/', superuser)
response = m.changelist_view(request) response = m.changelist_view(request)
for i in range(1, 10): for i in range(1, 10):
self.assertContains(response, '<a href="%s/">%s</a>' % (i, i)) self.assertContains(response, '<a href="%s/">%s</a>' % (i, i))
@ -421,3 +413,20 @@ class ChangeListTests(TestCase):
list_display_links = m.get_list_display_links(request, list_display) list_display_links = m.get_list_display_links(request, list_display)
self.assertEqual(list_display, ('parent', 'name', 'age')) self.assertEqual(list_display, ('parent', 'name', 'age'))
self.assertEqual(list_display_links, ['age']) self.assertEqual(list_display_links, ['age'])
def test_tuple_list_display(self):
"""
Regression test for #17128
(ChangeList failing under Python 2.5 after r16319)
"""
swallow = Swallow.objects.create(
origin='Africa', load='12.34', speed='22.2')
model_admin = SwallowAdmin(Swallow, admin.site)
superuser = self._create_superuser('superuser')
request = self._mocked_authenticated_request('/swallow/', superuser)
response = model_admin.changelist_view(request)
# just want to ensure it doesn't blow up during rendering
self.assertContains(response, unicode(swallow.origin))
self.assertContains(response, unicode(swallow.load))
self.assertContains(response, unicode(swallow.speed))